Beispiel #1
0
 /// <summary>
 /// Get a point on the spline in world space.
 /// </summary>
 /// <param name="pos">Position on the spline, in the range [0, 1].</param>
 /// <returns>The world space point on the spline.</returns>
 public Vector3 GetWorldPoint(float pos)
 {
     return(transform.TransformPoint(spline.GetPoint(pos)));
 }
Beispiel #2
0
        private void AddVertexDataDeformed(
            SplineData spline, Mesh mesh, StretchMode stretch, AlignMode align,
            float stretchFactor, float meshStart, float minZ, Vector3 center,
            List <Vector3> vertices, List <Vector3> normals, List <Vector2> uvs)
        {
            Vector3[] inVerts   = mesh.vertices;
            Vector3[] inNormals = mesh.normals;
            Vector2[] inUvs     = uvs != null ? mesh.uv : null;

            float centerPos      = Mathf.Clamp01(meshStart + (center.z - minZ) / spline.length);
            var   centerRotation = spline.GetRotation(centerPos);

            if (align == AlignMode.ForceUpright)
            {
                centerRotation = Quaternion.Euler(0, centerRotation.eulerAngles.y, 0);
            }
            else if (align == AlignMode.NoRotation)
            {
                centerRotation = Quaternion.identity;
            }

            Vector3 centerOffset = spline.GetPoint(centerPos) + (centerRotation * Vector3.forward * minZ);

            Matrix4x4 alignMatrix = Matrix4x4.TRS(centerOffset, centerRotation, Vector3.one);

            for (int i = 0; i < inVerts.Length; i++)
            {
                var vert   = inVerts[i];
                var normal = inNormals[i];
                var uv     = Vector2.zero;
                if (inUvs != null && inUvs.Length > i)
                {
                    uv = inUvs[i];
                }

                vert.z -= minZ;
                if ((stretch & StretchMode.Mesh) == StretchMode.Mesh)
                {
                    vert.z *= stretchFactor;
                }

                if (align == AlignMode.Deform)
                {
                    float pos  = Mathf.Clamp01(meshStart + (vert.z / spline.length));
                    var   srot = spline.GetRotation(pos);
                    var   spos = spline.GetPoint(pos);

                    vert   = spos + (srot * Vector3.ProjectOnPlane(vert, Vector3.forward));
                    normal = srot * normal;
                }
                else
                {
                    vert = alignMatrix.MultiplyPoint(vert);

                    normal = alignMatrix.MultiplyVector(normal);
                }

                vertices.Add(vert);
                normals.Add(normal);
                if (uvs != null)
                {
                    uvs.Add(uv);
                }
            }
        }