unity GL画图之美

思路:

1、手指头画的东西显示在屏幕上。

      获取鼠标或者手指的坐标。

       记录手指在屏幕的比例。

        世界坐标系:以(0,0)坐标为参考物的记录的相对位置。

        物体坐标系:以父类坐标为参考物的记录的相对位置。

        相机坐标系:以相机坐标为参考物的记录的相对位置。

        

         视图坐标系:左下角(0,0),右上角(1,1)。

          屏幕坐标系:Input.mousePosition左下角(0,0),右上角(屏幕宽,屏幕高)。

         

2、屏幕上面画的东西要显示到图片上或者3D物体上。

     1、截屏 Application.CaptureScreenshot();

      2、点在图片上作画。

 

屏幕画图有以下两种方法:

1、linerender。

2、GL。

 

实现在屏幕上画图代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Painter : MonoBehaviour
{
    List allPoints;

    private void Start()
    {
        allPoints = new List();
    }

    private void Update()
    {
        if(Input.GetMouseButton(0))
        {
            Vector3 tempView = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            allPoints.Add(tempView);
        }
        if(Input.GetMouseButtonUp(0))
        {
            allPoints.Clear();
        }
    }
    static Material lineMaterial;
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

   
        //正交投影,右上角坐标(1,1)
        GL.LoadOrtho();
        // Draw lines
        GL.Begin(GL.LINES);
        GL.Color(Color.red);
        for (int i = 1; i < allPoints.Count; i++)
        {
            Vector3 tmpFront = allPoints[i - 1];
            Vector3 tmpBack = allPoints[i];
            GL.Vertex3(tmpFront.x, tmpFront.y, tmpFront.z);
            GL.Vertex3(tmpBack.x, tmpBack.y, tmpBack.z);
        }
        GL.End();

    }
}

在场景当中创建一个Quad面片,把下面代码挂上去,就能实现。

实现屏幕画图映射到三维物体上面,代码如下:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LitMap : MonoBehaviour
{
    List allPoints;

    Texture2D painText;

    private void Start()
    {
        allPoints = new List();
       //transform.GetComponent().material.mainTexture=painText;
    }

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 tempView = Camera.main.ScreenToViewportPoint(Input.mousePosition);//视点坐标就是一个比例
            allPoints.Add(tempView);
        }
        if (Input.GetMouseButtonUp(0))
        {
            TestPainter();
            allPoints.Clear();
        }
    }
    /// 
    /// 实现屏幕坐标映射到三维物体上
    /// 
    public void TestPainter()
    {
        painText = new Texture2D(300, 400);
      
        for(int i=1;i().material.mainTexture = painText;
    }

    /// 
    /// 设置材质球什么的
    /// 
    static Material lineMaterial;
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    /// 
    /// 实现屏幕画线功能
    /// 
    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

   
        //正交投影,右上角坐标(1,1)
        GL.LoadOrtho();
        // Draw lines
        GL.Begin(GL.LINES);
        GL.Color(Color.red);
        for (int i = 1; i < allPoints.Count; i++)
        {
            Vector3 tmpFront = allPoints[i - 1];
            Vector3 tmpBack = allPoints[i];
            GL.Vertex3(tmpFront.x, tmpFront.y, tmpFront.z);
            GL.Vertex3(tmpBack.x, tmpBack.y, tmpBack.z);
        }
        GL.End();

    }
}

 

 

效果如下:

unity GL画图之美_第1张图片

 

 

你可能感兴趣的:(unity)