Ejemplo n.º 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);

        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, normal);

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

        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);
            float   mheight  = spline.GetHeight(marker);
            Vector3 binormal = CatmullRomSpline.ComputeBinormal(tangent, normal);

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

            if (fadeType == FadeType.Alpha || fadeType == FadeType.None)
            {
                rh  = h > 0 ? mheight : 0;
                rh2 = h2 > 0 ? mheight : 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 / mheight, 1);
                uv[firstVertexIndex + 1] = new Vector2(lastDistance / mheight, 0);
                uv[firstVertexIndex + 2] = new Vector2(distance / mheight, 1);
                uv[firstVertexIndex + 3] = new Vector2(distance / mheight, 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);
            }

            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.triangles = triangles;
        mesh.colors    = colors;
        mesh.normals   = normals;
    }