Unity实现在3D模型上涂鸦

作者: 2b75747cf703 | 来源:发表于2016-04-09 15:31 被阅读2699次

http://docs.unity3d.com/ScriptReference/GL.html

Paste_Image.png
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DrawOnModel : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public Color color = Color.red;

    public float thickness = 0.02f;

    private List<Vector3> points = new List<Vector3>();
    private List<Vector3> normals = new List<Vector3>();

    private List<int> splits = new List<int>();//分割索引

    static Material lineMaterial;
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            var 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()
    {
        CreateLineMaterial();

        lineMaterial.SetPass(0);

        GL.PushMatrix();
        GL.MultMatrix(transform.localToWorldMatrix);

        GL.Begin(GL.LINES);

        int index = 0;
        for(int i=1; i<points.Count; i++)
        {
            if (index < splits.Count && i == splits[index])
            {
                index++;
                continue;
            }

            GL.Color(color);

            var from = points[i-1] + normals[i-1] * thickness;

            GL.Vertex3(from.x, from.y, from.z);

            var worldNormal = transform.TransformDirection(normals[i]);
            var to = points[i] + worldNormal * thickness;
            GL.Vertex3(to.x, to.y, to.z);
        }

        GL.End();
        GL.PopMatrix();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {

    }

    public void OnDrag(PointerEventData eventData)
    {
        if (!eventData.pointerCurrentRaycast.isValid)
        {
            SplitPoints();
            return;
        }

        var localPosition = transform.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
        points.Add(localPosition);
        var localNormal = transform.InverseTransformDirection(eventData.pointerCurrentRaycast.worldNormal);
        normals.Add(localNormal);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        SplitPoints();
    }

    void SplitPoints()
    {
        if (splits.Count == 0 && points.Count != 0 || splits[splits.Count - 1] != points.Count)
            splits.Add(points.Count);
    }
}

相关文章

网友评论

  • 雨落随风:有图片看都很好了
  • deaa37c1a9aa:大神 涂鸦完的贴图和材质球能保存出来吗
    2b75747cf703:@鈡鵬 你保存一下涂的轨迹不就好了
    2b75747cf703:@鈡鵬 这个算法不是涂在贴图上的
  • 提拉米木有苏:大神,想问下能修改画笔的粗细吗?
    2b75747cf703:@提拉米木有苏 你可以用其他shader,我用的那个是不行的
    2b75747cf703:@提拉米木有苏 GL的API试试
  • c2adc220bd1d:大哥 能给个demo不
    2b75747cf703:@绝影_151b 是的,只要能被点击到就行
    c2adc220bd1d:你这个是脚本挂到哪个物体上 哪个就可以涂鸦?
    2b75747cf703:相机加一下投射器PhysicsRaycaster,场景中再加个UI的EventSystem,脚本随便找个物体挂就好了
  • c44a6bdc6fd0:Hidden/Internal-Colored写了红色吗
    2b75747cf703:GL.Color(color);设置的颜色

本文标题:Unity实现在3D模型上涂鸦

本文链接:https://www.haomeiwen.com/subject/uycblttx.html