Ejemplo n.º 1
0
        private void UpdateSlant(List <Vector3> vertices, List <Vector2> uvs, List <List <int> > indices)
        {
            if (mesh == null)
            {
                mesh = new FreeformMesh();
            }
            else
            {
                mesh.Clear();
            }

            int v1 = mesh.AddVertex(new Vector3(0, size.y, 0));
            int v2 = mesh.AddVertex(new Vector3(size.x, size.y, 0));
            int v3 = mesh.AddVertex(new Vector3(size.x, 0, size.z));
            int v4 = mesh.AddVertex(new Vector3(0, 0, size.z));
            int v5 = mesh.AddVertex(new Vector3(0, 0, 0));
            int v6 = mesh.AddVertex(new Vector3(size.x, 0, 0));

            mesh.AddFace(v1, v2, v6, v5);
            mesh.AddFace(v5, v6, v3, v4);
            mesh.AddFace(v2, v1, v4, v3); // Slant
            mesh.AddFace(v5, v4, v1);
            mesh.AddFace(v6, v2, v3);

            indices.Add(new List <int>());

            mesh.GetMeshData(vertices, uvs, indices[0], textureWorldScale, textureLocalScale);
        }
Ejemplo n.º 2
0
        private void UpdateFreeform(List <Vector3> vertices, List <Vector2> uvs, List <List <int> > indices)
        {
            if (mesh == null)
            {
                mesh = new FreeformMesh();
            }

            indices.Add(new List <int>());

            mesh.GetMeshData(vertices, uvs, indices[0], textureWorldScale, textureLocalScale);
        }
Ejemplo n.º 3
0
        private void UpdateSeparateStair(List <Vector3> vertices, List <Vector2> uvs, List <List <int> > indices)
        {
            if (mesh == null)
            {
                mesh = new FreeformMesh();
            }
            else
            {
                mesh.Clear();
            }

            int numSteps = Mathf.CeilToInt(size.y / stepSeparation);

            float stepLength = size.z / numSteps;

            for (int i = 0; i < numSteps; i++)
            {
                float shAct  = size.y / numSteps;
                float height = size.y - (i * shAct);
                int   v1     = mesh.AddVertex(new Vector3(0, height, i * stepLength));
                int   v2     = mesh.AddVertex(new Vector3(size.x, height, i * stepLength));
                int   v3     = mesh.AddVertex(new Vector3(size.x, height, (i + 1) * stepLength));
                int   v4     = mesh.AddVertex(new Vector3(0, height, (i + 1) * stepLength));

                int v7  = mesh.AddVertex(new Vector3(0, height - stepHeight, i * stepLength));
                int v8  = mesh.AddVertex(new Vector3(size.x, height - stepHeight, i * stepLength));
                int v9  = mesh.AddVertex(new Vector3(size.x, height - stepHeight, (i + 1) * stepLength));
                int v10 = mesh.AddVertex(new Vector3(0, height - stepHeight, (i + 1) * stepLength));

                mesh.AddFace(v4, v3, v2, v1);  // Step
                mesh.AddFace(v3, v4, v10, v9); // Back
                mesh.AddFace(v7, v10, v4, v1);
                mesh.AddFace(v9, v8, v2, v3);
                mesh.AddFace(v8, v7, v1, v2);
                mesh.AddFace(v7, v8, v9, v10);
            }

            if (addBottomStep)
            {
                int v1 = mesh.AddVertex(new Vector3(0, 0, size.z + stepLength));
                int v2 = mesh.AddVertex(new Vector3(size.x, 0, size.z + stepLength));

                mesh.AddFace(v1, v2, v2, v1);
            }

            indices.Add(new List <int>());

            mesh.GetMeshData(vertices, uvs, indices[0], textureWorldScale, textureLocalScale);
        }
Ejemplo n.º 4
0
        public void Update()
        {
            if (!dirty && !undone &&
                (lastType == type &&
                 lastSize == size &&
                 lastWorldScale == textureWorldScale &&
                 lastLocalScale == textureLocalScale &&
                 lastComplexity == complexity &&
                 lastSmooth == smooth &&
                 lastStepSep == stepSeparation &&
                 lastStepHeight == stepHeight &&
                 lastBottomStep == addBottomStep &&
                 lastIsStatic == gameObject.isStatic))
            {
                return;
            }

            Mesh m = filter.sharedMesh;

            if (m == null)
            {
                m      = new Mesh();
                m.name = "Brush Mesh";
            }

            List <Vector3>     vertices = new List <Vector3>();
            List <Vector2>     uvs      = new List <Vector2>();
            List <Vector3>     normals  = new List <Vector3>();
            List <List <int> > indices  = new List <List <int> >();

            if (type == Type.Box)
            {
                UpdateBox(vertices, uvs, indices);
                col.convex = true;
            }
            else if (type == Type.Slant)
            {
                UpdateSlant(vertices, uvs, indices);
                col.convex = true;
            }
            else if (type == Type.Cyllinder)
            {
                UpdateCyllinder(vertices, uvs, normals, indices);
                col.convex = true;
            }
            else if (type == Type.BlockStair)
            {
                UpdateBlockStair(vertices, uvs, indices);
            }
            else if (type == Type.SlantStair)
            {
                UpdateSlantStair(vertices, uvs, indices);
            }
            else if (type == Type.SeparateStair)
            {
                UpdateSeparateStair(vertices, uvs, indices);
            }
            else
            {
                if (lastType != Type.Freeform || mesh == null)
                {
                    mesh = new FreeformMesh();

                    UpdateBox(vertices, uvs, indices);
                }

                UpdateFreeform(vertices, uvs, indices);
            }

            m.subMeshCount = 0;
            m.vertices     = null;
            m.uv           = null;

            m.vertices     = vertices.ToArray();
            m.uv           = uvs.ToArray();
            m.subMeshCount = indices.Count;

            if (type == Type.Cyllinder && smooth)
            {
                m.normals = normals.ToArray();
            }

            for (int i = 0; i < indices.Count; i++)
            {
                m.SetIndices(indices[i].ToArray(), MeshTopology.Triangles, i);
            }

            m.RecalculateBounds();

            if (type != Type.Cyllinder || !smooth)
            {
                m.RecalculateNormals();
            }

            if (gameObject.isStatic)
#if UNITY_EDITOR
            { UnityEditor.Unwrapping.GenerateSecondaryUVSet(m); }
#endif

            { filter.sharedMesh = m; }
            col.sharedMesh = m;

            Material[] mats = render.sharedMaterials;
            if (m.subMeshCount != mats.Length && m.subMeshCount != 0)
            {
                Material[] newMats = new Material[m.subMeshCount];

                for (int i = 0; i < newMats.Length; i++)
                {
                    newMats[i] = mats[Mathf.Min(i, mats.Length - 1)];
                }

                render.sharedMaterials = newMats;
            }

            if (undone)
            {
                mesh.ForceGraphUpdate();
            }

            undone         = false;
            dirty          = false;
            lastType       = type;
            lastSize       = size;
            lastWorldScale = textureWorldScale;
            lastLocalScale = textureLocalScale;
            lastComplexity = complexity;
            lastSmooth     = smooth;
            lastStepSep    = stepSeparation;
            lastStepHeight = stepHeight;
            lastBottomStep = addBottomStep;
            lastIsStatic   = gameObject.isStatic;
        }
Ejemplo n.º 5
0
        private void UpdateSlantStair(List <Vector3> vertices, List <Vector2> uvs, List <List <int> > indices)
        {
            if (mesh == null)
            {
                mesh = new FreeformMesh();
            }
            else
            {
                mesh.Clear();
            }

            int numSteps = Mathf.CeilToInt(size.y / stepSeparation);

            float stepLength = size.z / numSteps;

            int last1 = -1, last2 = -1, back1 = -1, back2 = -1;

            for (int i = 0; i < numSteps; i++)
            {
                float shAct  = size.y / numSteps;
                float height = size.y - (i * shAct);
                int   v1     = i > 0 ? last1 : mesh.AddVertex(new Vector3(0, height, i * stepLength));
                int   v2     = i > 0 ? last2 : mesh.AddVertex(new Vector3(size.x, height, i * stepLength));
                int   v3     = mesh.AddVertex(new Vector3(size.x, height, (i + 1) * stepLength));
                int   v4     = mesh.AddVertex(new Vector3(0, height, (i + 1) * stepLength));

                int v5 = mesh.AddVertex(new Vector3(0, height - shAct, (i + 1) * stepLength));
                int v6 = mesh.AddVertex(new Vector3(size.x, height - shAct, (i + 1) * stepLength));

                int v7  = mesh.AddVertex(new Vector3(0, i > numSteps - 2 ? 0 : height - 2 * shAct, i * stepLength));
                int v8  = mesh.AddVertex(new Vector3(size.x, i > numSteps - 2 ? 0 : height - 2 * shAct, i * stepLength));
                int v9  = mesh.AddVertex(new Vector3(size.x, i > numSteps - 3 ? 0 : height - 3 * shAct, (i + 1) * stepLength));
                int v10 = mesh.AddVertex(new Vector3(0, i > numSteps - 3 ? 0 : height - 3 * shAct, (i + 1) * stepLength));

                mesh.AddFace(v4, v3, v2, v1); // Step
                mesh.AddFace(v3, v4, v5, v6); // Back
                mesh.AddFace(v7, v10, v4, v1);
                mesh.AddFace(v9, v8, v2, v3);

                if (i == 0)
                {
                    mesh.AddFace(v8, v7, v1, v2);
                    back1 = v7;
                    back2 = v8;
                }
                else if (i == numSteps - 2)
                {
                    mesh.AddFace(v8, v7, back1, back2);
                    back1 = v7;
                    back2 = v8;
                }
                else if (i == numSteps - 1)
                {
                    mesh.AddFace(v9, v10, back1, back2);
                }

                last1 = v5;
                last2 = v6;
            }

            if (addBottomStep)
            {
                int v1 = mesh.AddVertex(new Vector3(0, 0, size.z + stepLength));
                int v2 = mesh.AddVertex(new Vector3(size.x, 0, size.z + stepLength));

                mesh.AddFace(v1, v2, v2, v1);
            }

            indices.Add(new List <int>());

            mesh.GetMeshData(vertices, uvs, indices[0], textureWorldScale, textureLocalScale);
        }