예제 #1
0
        private void RenderMesh()
        {
            if (advancedParameters.nbSegmentToParametrize == 0)
            {
                spline.Parametrize();
            }
            else
            {
                spline.Parametrize(spline.NbSegments - advancedParameters.nbSegmentToParametrize, spline.NbSegments);
            }

            float length = Mathf.Max(spline.Length() - 0.1f, 0);

            int nbQuad = ((int)(1f / width * length)) + 1 - quadOffset;

            if (allocatedNbQuad < nbQuad)    //allocate more memory for the mesh
            {
                Reallocate(nbQuad);
                length = Mathf.Max(spline.Length() - 0.1f, 0);
                nbQuad = ((int)(1f / width * length)) + 1 - quadOffset;
            }

            int   startingQuad = lastStartingQuad;
            float lastDistance = startingQuad * width + quadOffset * width;

            maxInstanciedTriCount = System.Math.Max(maxInstanciedTriCount, (nbQuad - 1) * NbTriIndexPerQuad);

            Vector3 n = normal;

            if (dynamicNormalUpdate)
            {
                if (n == Vector3.zero)
                {
                    n = (transform.position - Camera.main.transform.position).normalized;
                }

                for (int i = 0; i < normals.Length; i++)
                {
                    normals[i] = n;
                }
            }

            CatmullRomSpline.Marker marker = new CatmullRomSpline.Marker();
            spline.PlaceMarker(marker, lastDistance);

            Vector3 lastPosition = spline.GetPosition(marker);
            Vector3 lastTangent  = spline.GetTangent(marker);
            Vector3 lastBinormal = CatmullRomSpline.ComputeBinormal(lastTangent, n);

            int drawingEnd = meshDisposition == MeshDisposition.Fragmented ? nbQuad - 1 : nbQuad - 1;



            float startingDist = lastDistance;

            for (int i = startingQuad; i < drawingEnd; i++)
            {
                float distance         = lastDistance + width;
                int   firstVertexIndex = i * NbVertexPerQuad;
                int   firstTriIndex    = i * NbTriIndexPerQuad;

                spline.MoveMarker(marker, distance);

                Vector3 position = spline.GetPosition(marker);
                Vector3 tangent  = spline.GetTangent(marker);
                Vector3 binormal = CatmullRomSpline.ComputeBinormal(tangent, n);

                float h = FadeMultiplier(lastDistance, length);
                float h2 = FadeMultiplier(distance, length);
                float rh = h * height, rh2 = h2 * height;

                if (fadeType == FadeType.Alpha || fadeType == FadeType.None)
                {
                    rh  = h > 0 ? height : 0;
                    rh2 = h2 > 0 ? height : 0;
                }

                if (meshDisposition == MeshDisposition.Continuous)
                {
                    vertices[firstVertexIndex]     = transform.InverseTransformPoint(lastPosition - origin + (lastBinormal * (rh * 0.5f)));
                    vertices[firstVertexIndex + 1] = transform.InverseTransformPoint(lastPosition - origin + (-lastBinormal * (rh * 0.5f)));
                    vertices[firstVertexIndex + 2] = transform.InverseTransformPoint(position - origin + (binormal * (rh2 * 0.5f)));
                    vertices[firstVertexIndex + 3] = transform.InverseTransformPoint(position - origin + (-binormal * (rh2 * 0.5f)));

                    uv[firstVertexIndex]     = new Vector2(lastDistance / height, 1);
                    uv[firstVertexIndex + 1] = new Vector2(lastDistance / height, 0);
                    uv[firstVertexIndex + 2] = new Vector2(distance / height, 1);
                    uv[firstVertexIndex + 3] = new Vector2(distance / height, 0);
                }
                else
                {
                    Vector3 pos = lastPosition + (lastTangent * width * -0.5f) - origin;

                    vertices[firstVertexIndex]     = transform.InverseTransformPoint(pos + (lastBinormal * (rh * 0.5f)));
                    vertices[firstVertexIndex + 1] = transform.InverseTransformPoint(pos + (-lastBinormal * (rh * 0.5f)));
                    vertices[firstVertexIndex + 2] = transform.InverseTransformPoint(pos + (lastTangent * width) + (lastBinormal * (rh * 0.5f)));
                    vertices[firstVertexIndex + 3] = transform.InverseTransformPoint(pos + (lastTangent * width) + (-lastBinormal * (rh * 0.5f)));

                    uv[firstVertexIndex]     = new Vector2(0, 1);
                    uv[firstVertexIndex + 1] = new Vector2(0, 0);
                    uv[firstVertexIndex + 2] = new Vector2(1, 1);
                    uv[firstVertexIndex + 3] = new Vector2(1, 0);
                }

                float relLength = length - startingDist;
                uv2[firstVertexIndex]     = new Vector2((lastDistance - startingDist) / relLength, 1);
                uv2[firstVertexIndex + 1] = new Vector2((lastDistance - startingDist) / relLength, 0);
                uv2[firstVertexIndex + 2] = new Vector2((distance - startingDist) / relLength, 1);
                uv2[firstVertexIndex + 3] = new Vector2((distance - startingDist) / relLength, 0);

                triangles[firstTriIndex]     = firstVertexIndex;
                triangles[firstTriIndex + 1] = firstVertexIndex + 1;
                triangles[firstTriIndex + 2] = firstVertexIndex + 2;
                triangles[firstTriIndex + 3] = firstVertexIndex + 2;
                triangles[firstTriIndex + 4] = firstVertexIndex + 1;
                triangles[firstTriIndex + 5] = firstVertexIndex + 3;

                colors[firstVertexIndex]     = vertexColor;
                colors[firstVertexIndex + 1] = vertexColor;
                colors[firstVertexIndex + 2] = vertexColor;
                colors[firstVertexIndex + 3] = vertexColor;

                if (fadeType == FadeType.Alpha || fadeType == FadeType.Both)
                {
                    colors[firstVertexIndex].a     *= h;
                    colors[firstVertexIndex + 1].a *= h;
                    colors[firstVertexIndex + 2].a *= h2;
                    colors[firstVertexIndex + 3].a *= h2;
                }

                lastPosition = position;
                lastTangent  = tangent;
                lastBinormal = binormal;
                lastDistance = distance;
            }

            for (int i = (nbQuad - 1) * NbTriIndexPerQuad; i < maxInstanciedTriCount; i++) //clear a few tri ahead
            {
                triangles[i] = 0;
            }

            lastStartingQuad = advancedParameters.lengthToRedraw == 0 ?
                               System.Math.Max(0, nbQuad - ((int)(maxLength / width) + 5)) :
                               System.Math.Max(0, nbQuad - ((int)(advancedParameters.lengthToRedraw / width) + 5));

            mesh.Clear();
            mesh.vertices  = vertices;
            mesh.uv        = uv;
            mesh.uv2       = uv2;
            mesh.triangles = triangles;
            mesh.colors    = colors;
            mesh.normals   = normals;
        }
예제 #2
0
    private void RenderMesh()
    {
        if (this.advancedParameters.nbSegmentToParametrize == 0)
        {
            this.spline.Parametrize();
        }
        else
        {
            this.spline.Parametrize(this.spline.NbSegments - this.advancedParameters.nbSegmentToParametrize, this.spline.NbSegments);
        }
        float num  = Mathf.Max(this.spline.Length() - 0.1f, 0f);
        int   num2 = (int)(1f / this.width * num) + 1 - this.quadOffset;

        if (this.allocatedNbQuad < num2)
        {
            this.Reallocate(num2);
            num  = Mathf.Max(this.spline.Length() - 0.1f, 0f);
            num2 = (int)(1f / this.width * num) + 1 - this.quadOffset;
        }
        int   num3 = this.lastStartingQuad;
        float num4 = (float)num3 * this.width + (float)this.quadOffset * this.width;

        this.maxInstanciedTriCount = Math.Max(this.maxInstanciedTriCount, (num2 - 1) * 6);
        CatmullRomSpline.Marker marker = new CatmullRomSpline.Marker();
        this.spline.PlaceMarker(marker, num4, null);
        Vector3 a      = this.spline.GetPosition(marker);
        Vector3 vector = this.spline.GetTangent(marker);
        Vector3 a2     = CatmullRomSpline.ComputeBinormal(vector, this.normal);
        int     num5   = (this.meshDisposition != SplineTrailRenderer.MeshDisposition.Fragmented) ? (num2 - 1) : (num2 - 1);
        int     num6   = this.vertices.Length;
        int     num7   = this.uv.Length;
        int     num8   = this.triangles.Length;
        int     num9   = this.colors.Length;

        for (int i = num3; i < num5; i++)
        {
            float num10 = num4 + this.width;
            int   num11 = i * 4;
            int   num12 = i * 6;
            this.spline.MoveMarker(marker, num10);
            Vector3 position = this.spline.GetPosition(marker);
            Vector3 tangent  = this.spline.GetTangent(marker);
            Vector3 vector2  = CatmullRomSpline.ComputeBinormal(tangent, this.normal);
            float   num13    = this.FadeMultiplier(num4, num);
            float   num14    = this.FadeMultiplier(num10, num);
            float   num15    = num13 * this.height;
            float   num16    = num14 * this.height;
            if (this.fadeType == SplineTrailRenderer.FadeType.Alpha || this.fadeType == SplineTrailRenderer.FadeType.None)
            {
                num15 = ((num13 <= 0f) ? 0f : this.height);
                num16 = ((num14 <= 0f) ? 0f : this.height);
            }
            if (this.meshDisposition == SplineTrailRenderer.MeshDisposition.Continuous)
            {
                if (num6 > num11 + 4)
                {
                    this.vertices[num11]     = base.transform.InverseTransformPoint(a - this.origin + a2 * (num15 * 0.5f));
                    this.vertices[num11 + 1] = base.transform.InverseTransformPoint(a - this.origin + -a2 * (num15 * 0.5f));
                    this.vertices[num11 + 2] = base.transform.InverseTransformPoint(position - this.origin + vector2 * (num16 * 0.5f));
                    this.vertices[num11 + 3] = base.transform.InverseTransformPoint(position - this.origin + -vector2 * (num16 * 0.5f));
                }
                if (num7 > num11 + 4)
                {
                    this.uv[num11]     = new Vector2(num4 / this.height, 1f);
                    this.uv[num11 + 1] = new Vector2(num4 / this.height, 0f);
                    this.uv[num11 + 2] = new Vector2(num10 / this.height, 1f);
                    this.uv[num11 + 3] = new Vector2(num10 / this.height, 0f);
                }
            }
            else
            {
                Vector3 a3 = a + vector * this.width * -0.5f - this.origin;
                if (num6 > num11 + 4)
                {
                    this.vertices[num11]     = base.transform.InverseTransformPoint(a3 + a2 * (num15 * 0.5f));
                    this.vertices[num11 + 1] = base.transform.InverseTransformPoint(a3 + -a2 * (num15 * 0.5f));
                    this.vertices[num11 + 2] = base.transform.InverseTransformPoint(a3 + vector * this.width + a2 * (num15 * 0.5f));
                    this.vertices[num11 + 3] = base.transform.InverseTransformPoint(a3 + vector * this.width + -a2 * (num15 * 0.5f));
                }
                if (num7 > num11 + 4)
                {
                    this.uv[num11]     = new Vector2(0f, 1f);
                    this.uv[num11 + 1] = new Vector2(0f, 0f);
                    this.uv[num11 + 2] = new Vector2(1f, 1f);
                    this.uv[num11 + 3] = new Vector2(1f, 0f);
                }
            }
            if (num8 > num12 + 6)
            {
                this.triangles[num12]     = num11;
                this.triangles[num12 + 1] = num11 + 1;
                this.triangles[num12 + 2] = num11 + 2;
                this.triangles[num12 + 3] = num11 + 2;
                this.triangles[num12 + 4] = num11 + 1;
                this.triangles[num12 + 5] = num11 + 3;
            }
            if (num9 > num11 + 4)
            {
                this.colors[num11]     = this.vertexColor;
                this.colors[num11 + 1] = this.vertexColor;
                this.colors[num11 + 2] = this.vertexColor;
                this.colors[num11 + 3] = this.vertexColor;
            }
            if ((this.fadeType == SplineTrailRenderer.FadeType.Alpha || this.fadeType == SplineTrailRenderer.FadeType.Both) && num9 > num11 + 4)
            {
                Color[] expr_6F0_cp_0 = this.colors;
                int     expr_6F0_cp_1 = num11;
                expr_6F0_cp_0[expr_6F0_cp_1].a = expr_6F0_cp_0[expr_6F0_cp_1].a * num13;
                Color[] expr_70D_cp_0 = this.colors;
                int     expr_70D_cp_1 = num11 + 1;
                expr_70D_cp_0[expr_70D_cp_1].a = expr_70D_cp_0[expr_70D_cp_1].a * num13;
                Color[] expr_72A_cp_0 = this.colors;
                int     expr_72A_cp_1 = num11 + 2;
                expr_72A_cp_0[expr_72A_cp_1].a = expr_72A_cp_0[expr_72A_cp_1].a * num14;
                Color[] expr_747_cp_0 = this.colors;
                int     expr_747_cp_1 = num11 + 3;
                expr_747_cp_0[expr_747_cp_1].a = expr_747_cp_0[expr_747_cp_1].a * num14;
            }
            a      = position;
            vector = tangent;
            a2     = vector2;
            num4   = num10;
        }
        for (int j = (num2 - 1) * 6; j < this.maxInstanciedTriCount; j++)
        {
            if (j < this.triangles.Length)
            {
                this.triangles[j] = 0;
            }
        }
        this.lastStartingQuad = ((this.advancedParameters.lengthToRedraw != 0f) ? Math.Max(0, num2 - ((int)(this.advancedParameters.lengthToRedraw / this.width) + 5)) : Math.Max(0, num2 - ((int)(this.maxLength / this.width) + 5)));
        this.mesh.Clear();
        this.mesh.vertices  = this.vertices;
        this.mesh.uv        = this.uv;
        this.mesh.triangles = this.triangles;
        this.mesh.colors    = this.colors;
        this.mesh.normals   = this.normals;
    }