예제 #1
0
        ////////////////////////////////////////////////////////////////

        static void AppendBridgeMeshVertices(ref List <BridgeMeshVertex> inOutBridgeMeshVertices, ValidBridgePoint bridgePoint, bool isEvenPoint)
        {
            BridgeMeshVertex leftVertex  = new BridgeMeshVertex();
            BridgeMeshVertex rightVertex = new BridgeMeshVertex();

            ////////////////////////////////////////////////////////////////
            // General

            // Depending on if we are an even or uneven point, we need to invert our "right" to make sure it is also the bridges "right"
            float facingFactor = isEvenPoint ? 1.0f : -1.0f;

            ////////////////////////////////////////////////////////////////
            // Inner Position
            leftVertex.InnerPositionWS  = bridgePoint.PositionWS;
            rightVertex.InnerPositionWS = bridgePoint.PositionWS;

            ////////////////////////////////////////////////////////////////
            // Position

            float halfBridgeWidth = BridgeOptions.WIDTH_WS / 2.0f;

            leftVertex.PositionWS  = bridgePoint.PositionWS + facingFactor * bridgePoint.RightTangentWS * halfBridgeWidth;
            rightVertex.PositionWS = bridgePoint.PositionWS - facingFactor * bridgePoint.RightTangentWS * halfBridgeWidth;

            ////////////////////////////////////////////////////////////////
            // Normal

            leftVertex.NormalWS  = Vector3.up;              // TODO: Calculate this from the positions themselves to make it more accurate.
            rightVertex.NormalWS = Vector3.up;

            ////////////////////////////////////////////////////////////////
            // UV
            // As the left and the right vertex might be differently far away from the previous points,
            // we need to calculate the UVs per vertex and cannot use the UVs of the points.

            float leftVertexV  = bridgePoint.UV.x;
            float rightVertexV = bridgePoint.UV.x;

            if (inOutBridgeMeshVertices.Count >= 2)
            {
                BridgeMeshVertex prevVertexLeft  = inOutBridgeMeshVertices[inOutBridgeMeshVertices.Count - 2];
                BridgeMeshVertex prevVertexRight = inOutBridgeMeshVertices[inOutBridgeMeshVertices.Count - 1];

                leftVertexV  = prevVertexLeft.UV.y + Vector3.Distance(prevVertexLeft.PositionWS, leftVertex.PositionWS);
                rightVertexV = prevVertexRight.UV.y + Vector3.Distance(prevVertexRight.PositionWS, rightVertex.PositionWS);
            }

            leftVertex.UV  = new Vector2(0.0f, leftVertexV);
            rightVertex.UV = new Vector2(1.0f, rightVertexV);

            ////////////////////////////////////////////////////////////////

            inOutBridgeMeshVertices.Add(leftVertex);
            inOutBridgeMeshVertices.Add(rightVertex);
        }
        public BridgeMesh(List <BridgeMeshVertex> vertices, List <int> indicies)
        {
            Indicies = indicies;

            Positions      = new List <Vector3>();
            InnerPositions = new List <Vector3>();
            Normals        = new List <Vector3>();
            UVs            = new List <Vector2>();

            for (int i = 0; i < vertices.Count; i++)
            {
                BridgeMeshVertex vertex = vertices[i];

                Positions.Add(vertex.PositionWS);
                InnerPositions.Add(vertex.InnerPositionWS);
                Normals.Add(vertex.NormalWS);
                UVs.Add(vertex.UV);
            }
        }
        ////////////////////////////////////////////////////////////////

        public void Serialize(Serializer io)
        {
            io.Serialize("Indicies", ref Indicies);

            ////////////////////////////////////////////////////////////////

            List <BridgeMeshVertex> vertices = new List <BridgeMeshVertex>();

            if (io.GetState() == Serializer.State.Saving)
            {
                for (int i = 0; i < Positions.Count; i++)
                {
                    vertices.Add(new BridgeMeshVertex(Positions[i], InnerPositions[i], Normals[i], UVs[i]));
                }
            }

            io.Serialize("Vertices", ref vertices, false);

            if (io.GetState() == Serializer.State.Loading)
            {
                Positions      = new List <Vector3>();
                InnerPositions = new List <Vector3>();
                Normals        = new List <Vector3>();
                UVs            = new List <Vector2>();

                for (int i = 0; i < vertices.Count; i++)
                {
                    BridgeMeshVertex vertex = vertices[i];

                    Positions.Add(vertex.PositionWS);
                    InnerPositions.Add(vertex.InnerPositionWS);
                    Normals.Add(vertex.NormalWS);
                    UVs.Add(vertex.UV);
                }
            }
        }