Unity实现在3D模型上涂鸦

10 篇文章 0 订阅
4 篇文章 0 订阅

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);
    }
}
  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值