Painter代码

2019-04-14  本文已影响0人  醉酒青牛_fa4e

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 阴阳师画符
/// </summary>
public class PainterUI : MonoBehaviour {

static Material lineMaterial;
List<Vector2> allPoint = new List<Vector2>();

static void CreatLineMaterial()
{
    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);
    }
}
public void OnRenderObject()
{
    CreatLineMaterial();

    GL.LoadOrtho();
    GL.Begin(GL.LINES);

    GL.Color(Color.red);
    for(int i = 1; i < allPoint.Count; i++)
    {
        GL.Vertex3(allPoint[i - 1].x, allPoint[i - 1].y, 0);

        GL.Vertex3(allPoint[i].x, allPoint[i].y, 0);
    }
    //for (int i = 1; i < allPoint.Count; i++)
    //{
    //    GL.Vertex3(allPoint[i - 1].x, allPoint[i].y, 0);

    //    GL.Vertex3(allPoint[i].x, allPoint[i+1].y, 0);
    //}
    //for (int i = 1; i < allPoint.Count; i++)
    //{
    //    GL.Vertex3(allPoint[i - 1].x, allPoint[i - 2].y, 0);

    //    GL.Vertex3(allPoint[i].x, allPoint[i-1].y, 0);
    //}
    //for (int i = 1; i < allPoint.Count; i++)
    //{
    //    GL.Vertex3(allPoint[i ].x, allPoint[i - 1].y, 0);

    //    GL.Vertex3(allPoint[i+1].x, allPoint[i].y, 0);
    //}
    //for (int i = 1; i < allPoint.Count; i++)
    //{
    //    GL.Vertex3(allPoint[i - 2].x, allPoint[i - 1].y, 0);

    //    GL.Vertex3(allPoint[i-1].x, allPoint[i].y, 0);
    //}
    //z正交

    GL.End();
}
Texture2D tmpTexture;
private void Start()
{

     tmpTexture = new Texture2D(300, 400);

    //for(int i = 0; i < 100; i++)
    //{
    //    for(int j = 0; j < 5; j++)
    //    {
    //        tmpTexture.SetPixel(i, j, Color.red);
    //    }
       
    //}
    tmpTexture.Apply();
    transform.GetComponent<Renderer>().material.mainTexture = tmpTexture;
}
void Update () {
    if (Input.GetMouseButton(0))
    {
        Vector2 viewPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        allPoint.Add(viewPos);
    }
    if (Input.GetMouseButtonUp(0))
    {
        GenerateTexture();
        allPoint.Clear();
    }
}

public void GenerateTexture()
{
    for(int i = 1; i < allPoint.Count; i++)
    {
        //前面点
        Vector2 tmpPos = allPoint[i-1];
        float xx1 = (tmpTexture.width * tmpPos.x);
        float yy1 = (tmpTexture.height * tmpPos.y);

        //后面点
         tmpPos = allPoint[i];
        float xx = (tmpTexture.width * tmpPos.x);
        float yy =(tmpTexture.height * tmpPos.y);

        for(int j = 0; j < 100; j++)
        {
            int tmpXX=(int) Mathf.Lerp(xx1, xx, j / 100.0f);
            int tmpYY= (int)Mathf.Lerp(yy1, yy, j / 100.0f);
            tmpTexture.SetPixel(tmpXX, tmpYY, Color.red);
        }
    }
    tmpTexture.Apply();
}

}

上一篇 下一篇

猜你喜欢

热点阅读