Ejemplo n.º 1
0
        /// <summary>
        /// Performs further custom initialization for this instance.
        /// </summary>
        /// <exception cref="System.ObjectDisposedException">Skybox disposed.</exception>
        protected override void Initialize()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException("Skybox");
            }

            base.Initialize();

            this.material = new SkyboxMaterial(this.cubemapTexture);
            this.material.Initialize(this.Assets);

            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)
            };

            Vector2[] texCoord = 
            {
                new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0),
                new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0),
                new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
                new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0),
            };

            float sizeOverTwo = 0.5f;

            ushort[] indices = new ushort[36];
            VertexPositionTexture[] vertices = new VertexPositionTexture[24];

            ushort currentIndice = 0;
            ushort currentVertex = 0;

            // Create each face in turn.
            for (int i = 0, j = 0; i < normals.Length; i++, j += 4)
            {
                Vector3 normal = normals[i];

                // 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[currentIndice++] = (ushort)(currentVertex + 0);
                indices[currentIndice++] = (ushort)(currentVertex + 1);
                indices[currentIndice++] = (ushort)(currentVertex + 3);

                indices[currentIndice++] = (ushort)(currentVertex + 1);
                indices[currentIndice++] = (ushort)(currentVertex + 2);
                indices[currentIndice++] = (ushort)(currentVertex + 3);

                // 1   2
                // 0   3

                // Four vertices per face.
                vertices[currentVertex].Position = (normal - side1 - side2) * sizeOverTwo;
                vertices[currentVertex++].TexCoord = texCoord[j + 0];
                vertices[currentVertex].Position = (normal - side1 + side2) * sizeOverTwo;
                vertices[currentVertex++].TexCoord = texCoord[j + 1];
                vertices[currentVertex].Position = (normal + side1 + side2) * sizeOverTwo;
                vertices[currentVertex++].TexCoord = texCoord[j + 2];
                vertices[currentVertex].Position = (normal + side1 - side2) * sizeOverTwo;
                vertices[currentVertex++].TexCoord = texCoord[j + 3];
            }

            VertexBuffer vertexBuffer = new VertexBuffer(VertexPositionTexture.VertexFormat);
            vertexBuffer.SetData(vertices, 24);

            IndexBuffer indexBuffer = new IndexBuffer(indices);

            // create the quad
            this.cubeMesh = new Mesh(0, 24, 0, 12, vertexBuffer, indexBuffer, PrimitiveType.TriangleList);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Refresh cubemap material
        /// </summary>
        private void RefreshCubemapMaterial()
        {
            if (this.material != null)
            {
                this.material.Unload(this.Assets);
                this.material = null;
            }

            if (!string.IsNullOrEmpty(this.cubemapPath))
            {
                this.material = new SkyboxMaterial(this.cubemapPath);
                this.material.Initialize(this.Assets);
            }
        }