用unity实现tilt brush绘制面片功能(三)

发表于2017-05-06
评论2 2.1k浏览

本篇文章将是unity实现tilt brush绘制面片功能系列的最后一篇,在本篇文章中,我将修改bug,并填一些坑。

先展示一下最终效果

上篇文章留了一个小尾巴,我们能透过贴图看到贴图的背景。这是因为我们的贴图是半透明的,当且仅当alpha值为255时贴图才“不”透明。
解决方法很简单,就是对alpha值做一些运算,使alpha值变为最大值。
先上完整的实现
我们将alpha值减去一个数再乘以100便可。注意,在这里alpha值最大值为1,而非255!
示意见下图
在做的时候会发现,减去的那个值很难控制,所以我通过一个slider来调节大小,由于slider是从小到达,所以我用1减去他来获得我们需要的数据
示意见下图
代码修改为在每次加入新点后对mesh进行重绘
设置uv的代码有个bug,右边的点值应该是(1, (float)i / (float)m_point1.Count),而不是(0, (float)i / (float)m_point1.Count)
修改如下
1
2
3
4
5
6
for (int i = 0; i < m_point1.Count; i++)
        {
            //设置uv,从左下到右上从0到1
            m_uv[i * 2] = new Vector2(0, (float)i / (float)m_point1.Count);
            m_uv[i * 2 + 1] = new Vector2(1, (float)i / (float)m_point1.Count);//此处笔误,右下值应为(1,0)
        }
附上修改后的完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class demo : MonoBehaviour
{
    public float speed = 0.1f;
    public float rotat = 1;
    public Vector3[] m_vertices;
    public Vector2[] m_uv;
    public Color[] m_color;
    public Vector3[] m_normals;
    public int[] m_triangles;
    private List m_point1 = new List();
    private List m_point2 = new List();
    private Mesh mesh;
    private bool isprint = false;
    private Vector3 pos = Vector3.zero;
    private Transform point1;
    private Transform point2;
 
    void Start()
    {
        mesh = transform.GetComponent().mesh;
        point1 = transform.GetChild(0);
        point2 = transform.GetChild(1);
        m_point1.Add(point1.transform.position);
        m_point2.Add(point2.transform.position);
    }
 
    void FixedUpdate()
    {
        Move();
        if(Input.GetKeyDown("p"))
            transform.position = Vector3.zero;//为了使绘制后的物体仍在原地,将移动后的物体归位
    }
 
    void Move()
    {
        transform.Translate(new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0)*speed,Space.World);
        if (Input.GetKey("q"))
        {
            transform.Rotate(new Vector3(0, 0, -rotat));
        }
        if (Input.GetKey("e"))
        {
            transform.Rotate(new Vector3(0, 0, rotat));
        }
        if (!isprint)
        {
            if (Vector3.Distance(transform.position, pos) > 0.2f)
            {
                //将2个小球位置保存
                m_point1.Add(point1.transform.position);
                m_point2.Add(point2.transform.position);
                pos = transform.position;
                DrawMesh();
            }
        }
    }
 
    void DrawMesh()
    {
        m_vertices = new Vector3[m_point1.Count * 2];
        for(int i=0;i< m_point1.Count; i++)
        {
            //逐个添加顶点
            m_vertices[i * 2] = m_point1[i];
            m_vertices[i * 2 + 1] = m_point2[i];
        }
        m_uv = new Vector2[m_point1.Count * 2];
        for (int i = 0; i < m_point1.Count; i++)
        {
            //设置uv,从左下到右上从0到1
            m_uv[i * 2] = new Vector2(0, (float)i / (float)m_point1.Count);
            m_uv[i * 2 + 1] = new Vector2(1, (float)i / (float)m_point1.Count);//此处笔误,右下值应为(1,0)
        }
        m_triangles = new int[m_point1.Count * 3];
        for (int i = 0; i < m_point1.Count; i++)
        {
            //设置三角面
            m_triangles[i * 3] = i;
            if (i % 2 == 0)
            {
                m_triangles[i * 3 + 1] = i + 1;
                m_triangles[i * 3 + 2] = i + 2;
            }
            else
            {
                m_triangles[i * 3 + 1] = i + 2;
                m_triangles[i * 3 + 2] = i + 1;
            }
        }
        ChangeMesh();
    }
 
    void ChangeMesh()
    {
        mesh.Clear();//将原来的网格信息清除
        mesh.vertices = m_vertices;
        mesh.uv = m_uv;
        mesh.colors = m_color;
        mesh.normals = m_normals;
        mesh.triangles = m_triangles;
        mesh.RecalculateNormals();//重绘网格
    }
}
工程文件见附录(项目有些乱。。。我承认,我就是懒。。。)

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引