Пример #1
0
        public MeshGeometry(Scene scene, Mesh mesh)
        {
            this.scene               = scene;
            this.graphicsDevice      = scene.GraphicsDevice;
            this.mySilverlightEffect = scene.ContentManager.Load <SilverlightEffect>("CustomEffect");

            // Cache effect parameters
            worldViewProjectionParameter = mySilverlightEffect.Parameters["WorldViewProjection"];
            worldParameter         = mySilverlightEffect.Parameters["World"];
            lightPositionParameter = mySilverlightEffect.Parameters["LightPosition"];

            // Init static parameters
            this.LightPosition = scene.LightPosition;

            // Temporary lists
            List <VertexPositionColorNormal> vertices = new List <VertexPositionColorNormal>();
            List <ushort> indices = new List <ushort>();

            Vector4 color = new Vector4(1, 1, 0.8f, 1);

            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                var v = mesh.Vertices[i];
                var n = mesh.Normals[i];
                vertices.Add(new VertexPositionColorNormal(new Vector3((float)v.x, (float)v.y, (float)v.z), new Vector3((float)n.x, (float)n.y, (float)n.z), color));
            }
            foreach (var face in mesh.Triangles)
            {
                indices.Add((ushort)face.V0);
                indices.Add((ushort)face.V1);
                indices.Add((ushort)face.V2);
            }

            // Create a vertex buffer, and copy our vertex data into it.
            vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionColorNormal.VertexDeclaration, vertices.Count, BufferUsage.None);
            vertexBuffer.SetData(0, vertices.ToArray(), 0, vertices.Count, VertexPositionColorNormal.Stride);

            // Create an index buffer, and copy our index data into it.
            indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Count, BufferUsage.None);
            indexBuffer.SetData(0, indices.ToArray(), 0, indices.Count);

            // Statistics
            VerticesCount = vertices.Count;
            FaceCount     = indices.Count / 3;
        }
 protected ShaderBase(Engine engine, string assetName)
 {
     _effect = engine.Content.Load <SilverlightEffect>(assetName);
 }
Пример #3
0
        public Cube(Scene scene, float size)
        {
            this.scene               = scene;
            this.graphicsDevice      = scene.GraphicsDevice;
            this.mySilverlightEffect = scene.ContentManager.Load <SilverlightEffect>("CustomEffect");

            // Cache effect parameters
            worldViewProjectionParameter = mySilverlightEffect.Parameters["WorldViewProjection"];
            worldParameter         = mySilverlightEffect.Parameters["World"];
            lightPositionParameter = mySilverlightEffect.Parameters["LightPosition"];

            // Init static parameters
            this.LightPosition = new Vector3(1, 1, -10);

            // Temporary lists
            List <VertexPositionColorNormal> vertices = new List <VertexPositionColorNormal>();
            List <ushort> indices = new List <ushort>();

            // A cube has six faces, each one pointing in a different direction.
            Vector3[] normals =
            {
                new Vector3(0,   0,  1),
                new Vector3(0,   0, -1),
                new Vector3(1,   0,  0),
                new Vector3(-1,  0,  0),
                new Vector3(0,   1,  0),
                new Vector3(0,  -1, 0)
            };

            // Create each face in turn.
            foreach (Vector3 normal in normals)
            {
                // Get two vectors perpendicular to the face normal and to each other.
                Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X);
                Vector3 side2 = Vector3.Cross(normal, side1);

                // Six indices (two triangles) per face.
                indices.Add((ushort)vertices.Count);
                indices.Add((ushort)(vertices.Count + 1));
                indices.Add((ushort)(vertices.Count + 2));

                indices.Add((ushort)vertices.Count);
                indices.Add((ushort)(vertices.Count + 2));
                indices.Add((ushort)(vertices.Count + 3));

                // Four vertices per face.
                Vector4 color = new Vector4(0, 1, 0, 1);
                vertices.Add(new VertexPositionColorNormal((normal - side1 - side2) * size / 2, normal, color));
                vertices.Add(new VertexPositionColorNormal((normal - side1 + side2) * size / 2, normal, color));
                vertices.Add(new VertexPositionColorNormal((normal + side1 + side2) * size / 2, normal, color));
                vertices.Add(new VertexPositionColorNormal((normal + side1 - side2) * size / 2, normal, color));
            }

            // Create a vertex buffer, and copy our vertex data into it.
            vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionColorNormal.VertexDeclaration, vertices.Count, BufferUsage.None);

            vertexBuffer.SetData(0, vertices.ToArray(), 0, vertices.Count, VertexPositionColorNormal.Stride);

            // Create an index buffer, and copy our index data into it.
            indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Count, BufferUsage.None);

            indexBuffer.SetData(0, indices.ToArray(), 0, indices.Count);

            // Statistics
            VerticesCount = vertices.Count;
            FaceCount     = indices.Count / 3;
        }