Example #1
0
        private void BuildData()
        {
            // if the mesh is reversed by scale, we must change the culling of the faces by inversing all triangles.
            // the mesh is reverse only if the number of resersing axes is impair.
            bool reversed = scale.x < 0;

            if (scale.y < 0)
            {
                reversed = !reversed;
            }
            if (scale.z < 0)
            {
                reversed = !reversed;
            }
            triangles = reversed ? MeshUtility.GetReversedTriangles(Mesh) : Mesh.triangles;

            // we transform the source mesh vertices according to rotation/translation/scale
            int i = 0;

            vertices = new List <MeshVertex>(Mesh.vertexCount);
            foreach (Vector3 vert in Mesh.vertices)
            {
                var transformed = new MeshVertex(vert, Mesh.normals[i++]);
                //  application of rotation
                if (rotation != Quaternion.identity)
                {
                    transformed.position = rotation * transformed.position;
                    transformed.normal   = rotation * transformed.normal;
                }
                if (scale != Vector3.one)
                {
                    transformed.position = Vector3.Scale(transformed.position, scale);
                    transformed.normal   = Vector3.Scale(transformed.normal, scale);
                }
                if (translation != Vector3.zero)
                {
                    transformed.position += translation;
                }
                vertices.Add(transformed);
            }

            // find the bounds along x
            minX = float.MaxValue;
            float maxX = float.MinValue;

            foreach (var vert in vertices)
            {
                Vector3 p = vert.position;
                maxX = Math.Max(maxX, p.x);
                minX = Math.Min(minX, p.x);
            }
            length = Math.Abs(maxX - minX);
        }
Example #2
0
        /// <summary>
        /// Build data that are consistent between computations if no property has been changed.
        /// This method allows the computation due to curve changes to be faster.
        /// </summary>
        private void BuildData()
        {
            if (source == null)
            {
                throw new Exception(GetType().Name + " can't compute because there is no source mesh.");
            }

            // find the bounds along x
            minX = float.MaxValue;
            float maxX = float.MinValue;

            foreach (Vertex vert in vertices)
            {
                Vector3 p = vert.v;
                if (rotation != Quaternion.identity)
                {
                    p = rotation * p;
                }
                if (translation != Vector3.zero)
                {
                    p += translation;
                }
                maxX = Math.Max(maxX, p.x);
                minX = Math.Min(minX, p.x);
            }
            length = Math.Abs(maxX - minX);

            // if the mesh is reversed by scale, we must change the culling of the faces by inversing all triangles.
            // the mesh is reverse only if the number of resersing axes is impair.
            bool reversed = scale.x < 0;

            if (scale.y < 0)
            {
                reversed = !reversed;
            }
            if (scale.z < 0)
            {
                reversed = !reversed;
            }
            result.triangles = reversed ? MeshUtility.GetReversedTriangles(source) : source.triangles;

            // we transform the source mesh vertices according to rotation/translation/scale
            transformedVertices.Clear();
            foreach (Vertex vert in vertices)
            {
                Vertex transformed = new Vertex()
                {
                    v = vert.v,
                    n = vert.n
                };
                //  application of rotation
                if (rotation != Quaternion.identity)
                {
                    transformed.v = rotation * transformed.v;
                    transformed.n = rotation * transformed.n;
                }
                if (scale != Vector3.one)
                {
                    transformed.v = Vector3.Scale(transformed.v, scale);
                    transformed.n = Vector3.Scale(transformed.n, scale);
                }
                if (translation != Vector3.zero)
                {
                    transformed.v += translation;
                }
                transformedVertices.Add(transformed);
            }
        }