コード例 #1
0
        /// <summary>
        /// Creates the VertexPositionTexture array and uv maps the vertices.
        /// </summary>
        /// <param name="vertices">The vertices to map.</param>
        /// <returns>The VertexPositionTexture array.</returns>
        private static VertexPositionDualTexture[] CreateUVMap(List <Vector3> vertices)
        {
            VertexPositionDualTexture[] vertexArray = new VertexPositionDualTexture[vertices.Count];

            float minX = vertices.OrderBy(v => v.X).ToArray()[0].X;
            float minZ = vertices.OrderBy(v => v.X).ToArray()[0].Z;

            float maxX = vertices.OrderByDescending(v => v.X).ToArray()[0].X;
            float maxZ = vertices.OrderByDescending(v => v.X).ToArray()[0].Z;

            float absX = Math.Abs(minX);
            float absZ = Math.Abs(maxZ);

            for (int i = 0; i < vertices.Count; i++)
            {
                VertexPositionDualTexture vertex = new VertexPositionDualTexture();
                vertex.Position = vertices[i];
                vertex.Texture1 = new Vector2(vertices[i].X + absX, vertices[i].Z + absZ);
                vertex.Texture2 = new Vector2(MathUtils.NormalizeData(minX, maxX, vertices[i].X), MathUtils.NormalizeData(minZ, maxZ, vertices[i].Z));
                vertexArray[i]  = vertex;
            }

            return(vertexArray);
        }
コード例 #2
0
        /// <summary>
        /// Refresh the quad mesh
        /// </summary>
        private void RefreshQuadMesh()
        {
            if (this.quadMesh != null)
            {
                this.GraphicsDevice.DestroyIndexBuffer(this.quadMesh.IndexBuffer);
                this.GraphicsDevice.DestroyVertexBuffer(this.quadMesh.VertexBuffer);
                this.quadMesh = null;
            }

            if (this.texcoord1 == null)
            {
                this.texcoord1 = new Vector2[4]
                {
                    new Vector2(0, 0),
                    new Vector2(1, 0),
                    new Vector2(1, 1),
                    new Vector2(0, 1),
                };
            }

            if (this.texcoord2 == null)
            {
                this.texcoord2 = new Vector2[4]
                {
                    new Vector2(0, 0),
                    new Vector2(1, 0),
                    new Vector2(1, 1),
                    new Vector2(0, 1),
                };
            }

            Vector2 origin                 = this.Transform2D.Origin;
            float   halfWidth              = this.Transform2D.Rectangle.Width;
            float   halfHeight             = this.Transform2D.Rectangle.Height;
            float   originCorrectionWidth  = origin.X * halfWidth;
            float   originCorrectionHeight = origin.Y * halfHeight;

            float opacity = this.Transform2D.GlobalOpacity;
            Color color   = new Color(opacity, opacity, opacity, opacity);

            VertexPositionDualTexture[] vertices = new VertexPositionDualTexture[4];
            vertices[0].Position  = new Vector3(-originCorrectionWidth, -originCorrectionHeight, 0);
            vertices[0].TexCoord  = this.texcoord1[0];
            vertices[0].TexCoord2 = this.texcoord2[0];

            vertices[1].Position  = new Vector3(halfWidth - originCorrectionWidth, -originCorrectionHeight, 0);
            vertices[1].TexCoord  = this.texcoord1[1];
            vertices[1].TexCoord2 = this.texcoord2[1];

            vertices[2].Position  = new Vector3(halfWidth - originCorrectionWidth, halfHeight - originCorrectionHeight, 0);
            vertices[2].TexCoord  = this.texcoord1[2];
            vertices[2].TexCoord2 = this.texcoord2[2];

            vertices[3].Position  = new Vector3(-originCorrectionWidth, halfHeight - originCorrectionHeight, 0);
            vertices[3].TexCoord  = this.texcoord1[3];
            vertices[3].TexCoord2 = this.texcoord2[3];

            VertexBuffer vertexBuffer = new VertexBuffer(VertexPositionDualTexture.VertexFormat);

            vertexBuffer.SetData(vertices, 4);

            ushort[] indices = new ushort[6];
            indices[0] = 0;
            indices[1] = 1;
            indices[2] = 2;
            indices[3] = 2;
            indices[4] = 3;
            indices[5] = 0;

            IndexBuffer indexBuffer = new IndexBuffer(indices);

            // create the quad
            this.quadMesh = new Mesh(0, 4, 0, 2, vertexBuffer, indexBuffer, PrimitiveType.TriangleList);
        }