private void InitializeStarfield() { // Load star positions and luminance from file with 9110 predefined stars. const int numberOfStars = 9110; var stars = new Star[numberOfStars]; var storage = _services.GetInstance <IStorage>(); using (var reader = new BinaryReader(storage.OpenFile("Sky/Stars.bin"))) { for (int i = 0; i < numberOfStars; i++) { stars[i].Position.X = reader.ReadSingle(); stars[i].Position.Y = reader.ReadSingle(); stars[i].Position.Z = reader.ReadSingle(); // To avoid flickering, the star size should be >= 2.8 pix. stars[i].Size = 2.8f; stars[i].Color.X = reader.ReadSingle() * StarfieldLightScale; stars[i].Color.Y = reader.ReadSingle() * StarfieldLightScale; stars[i].Color.Z = reader.ReadSingle() * StarfieldLightScale; } Debug.Assert(reader.PeekChar() == -1, "End of file should be reached."); } // Note: To improve performance, we could sort stars by brightness and // remove less important stars. _starfieldNode = new StarfieldNode { Color = new Vector3F(1), Stars = stars }; }
protected override void OnUnload() { _cameraObject = null; // Detach and dispose the grouping scene node with all children. _groupNode.Parent.Children.Remove(_ambientLightNode); _groupNode.Dispose(false); // Dispose cloud textures. if (_enableCloudLayer) { _cloudMap0.Dispose(); _cloudMap1.Dispose(); } // Set all references to null. _groupNode = null; _milkyWayNode = null; _starfieldNode = null; _sunNode = null; _moonNode = null; _scatteringSkyNode = null; _ambientLightNode = null; _sunlightNode = null; _moonlightNode = null; _cloudLayerNode0 = null; _cloudLayerNode1 = null; _cloudMap0 = null; _cloudMap1 = null; }
private void InitializeStarfield() { // Load star positions and luminance from file with 9110 predefined stars. const int numberOfStars = 9110; var stars = new Star[numberOfStars]; var storage = Services.GetInstance <IStorage>(); using (var reader = new BinaryReader(storage.OpenFile("Sky/Stars.bin"))) { for (int i = 0; i < numberOfStars; i++) { stars[i].Position.X = reader.ReadSingle(); stars[i].Position.Y = reader.ReadSingle(); stars[i].Position.Z = reader.ReadSingle(); // To avoid flickering, the star size should be >= 2.8 px. stars[i].Size = 2.8f; stars[i].Color.X = reader.ReadSingle(); stars[i].Color.Y = reader.ReadSingle(); stars[i].Color.Z = reader.ReadSingle(); } Debug.Assert(reader.PeekChar() == -1, "End of file should be reached."); } _starfield = new StarfieldNode(); _starfield.Color = new Vector3F(1); _starfield.Stars = stars; }
protected override void OnUnload() { // Detach the scene nodes from the scene. if (_skyGroupNode.Parent != null) { _skyGroupNode.Parent.Children.Remove(_skyGroupNode); } _lightGroupNode.Parent.Children.Remove(_lightGroupNode); // Dispose allocated resources. _skyGroupNode.Dispose(false); _lightGroupNode.Dispose(false); if (_enableClouds) { _cloudLayerNode0.CloudMap.Dispose(); _cloudLayerNode1.CloudMap.Dispose(); } if (_cacheSky) { _sceneCaptureNode.RenderToTexture.Texture.Dispose(); _sceneCaptureNode.Dispose(false); _cloudMapRenderer.Dispose(); _skyRenderer.Dispose(); _colorEncoder.Dispose(); _sceneCaptureRenderer.Dispose(); } // Set references to null. _cameraObject = null; _skyGroupNode = null; _lightGroupNode = null; _milkyWayNode = null; _starfieldNode = null; _sunNode = null; _moonNode = null; _scatteringSkyNode = null; _ambientLightNode = null; _sunlightNode = null; _moonlightNode = null; _cloudLayerNode0 = null; _cloudLayerNode1 = null; _sceneCaptureNode = null; SkyboxNode = null; _cloudMapRenderer = null; _skyRenderer = null; _colorEncoder = null; _sceneCaptureRenderer = null; }
private static Submesh GetStarfieldMesh(StarfieldNode node, RenderContext context) { // The mesh is cached in the scene node. var mesh = node.RenderData as Submesh; if (mesh == null) { mesh = new Submesh { PrimitiveType = PrimitiveType.TriangleList }; node.RenderData = mesh; } if (mesh.VertexBuffer == null || mesh.VertexBuffer.IsDisposed) { int numberOfStars = node.Stars.Count; // The total number of stars is limited by the graphics device max primitives per call limit. var graphicsDevice = context.GraphicsService.GraphicsDevice; int maxNumberOfStars = graphicsDevice.GetMaxPrimitivesPerCall() / 2; if (numberOfStars > maxNumberOfStars) { throw new GraphicsException("The number of stars must be less than " + maxNumberOfStars); } // Create a vertex buffer with a quad for each star. // 1--2 // | /| // |/ | // 0--3 mesh.VertexCount = numberOfStars * 4; var vertices = new StarVertex[numberOfStars * 4]; for (int i = 0; i < numberOfStars; i++) { var star = node.Stars[i]; var positionAndSize = new HalfVector4(star.Position.X, star.Position.Y, star.Position.Z, star.Size); var color = new HalfVector4(star.Color.X, star.Color.Y, star.Color.Z, 1); vertices[i * 4 + 0].PositionAndSize = positionAndSize; vertices[i * 4 + 0].Color = color; vertices[i * 4 + 0].Texture = new Vector2(0, 1); vertices[i * 4 + 1].PositionAndSize = positionAndSize; vertices[i * 4 + 1].Color = color; vertices[i * 4 + 1].Texture = new Vector2(0, 0); vertices[i * 4 + 2].PositionAndSize = positionAndSize; vertices[i * 4 + 2].Color = color; vertices[i * 4 + 2].Texture = new Vector2(1, 0); vertices[i * 4 + 3].PositionAndSize = positionAndSize; vertices[i * 4 + 3].Color = color; vertices[i * 4 + 3].Texture = new Vector2(1, 1); } mesh.VertexBuffer = new VertexBuffer(graphicsDevice, typeof(StarVertex), vertices.Length, BufferUsage.None); mesh.VertexBuffer.SetData(vertices); // Create index buffer for quad (= two triangles, clockwise). var indices = new ushort[numberOfStars * 6]; for (int i = 0; i < numberOfStars; i++) { indices[i * 6 + 0] = (ushort)(i * 4 + 0); indices[i * 6 + 1] = (ushort)(i * 4 + 1); indices[i * 6 + 2] = (ushort)(i * 4 + 2); indices[i * 6 + 3] = (ushort)(i * 4 + 0); indices[i * 6 + 4] = (ushort)(i * 4 + 2); indices[i * 6 + 5] = (ushort)(i * 4 + 3); } mesh.IndexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.None); mesh.IndexBuffer.SetData(indices); mesh.PrimitiveCount = numberOfStars * 2; } return(mesh); }