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; }
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; }
// OnUnload() is called when the GameObject is removed from the IGameObjectService. protected override void OnUnload() { _skyboxNode.Parent.Children.Remove(_skyboxNode); _skyboxNode.Dispose(false); _skyboxNode = null; _ambientLightNode.Parent.Children.Remove(_ambientLightNode); _ambientLightNode.Dispose(false); _ambientLightNode = null; _sunlightNode.Parent.Children.Remove(_sunlightNode); _sunlightNode.Dispose(false); _sunlightNode = null; }
// OnLoad() is called when the GameObject is added to the IGameObjectService. protected override void OnLoad() { var content = _services.GetInstance <ContentManager>(); _skyboxNode = new SkyboxNode(content.Load <TextureCube>("Sky2")) { Color = new Vector3F(SkyExposure), }; // The ambient light. var ambientLight = new AmbientLight { Color = new Vector3F(0.9f, 0.9f, 1f), HdrScale = 0.1f, Intensity = 0.5f, HemisphericAttenuation = 0.8f, }; _ambientLightNode = new LightNode(ambientLight) { Name = "Ambient", }; // The main directional light. var sunlight = new DirectionalLight { Color = new Vector3F(1, 0.9607844f, 0.9078432f), HdrScale = 0.4f, DiffuseIntensity = 1, SpecularIntensity = 1, }; _sunlightNode = new LightNode(sunlight) { Name = "Sunlight", Priority = 10, // This is the most important light. PoseWorld = new Pose(QuaternionF.CreateRotationY(-1.4f) * QuaternionF.CreateRotationX(-0.6f)), // This light uses Cascaded Shadow Mapping. Shadow = new CascadedShadow { #if XBOX PreferredSize = 512, #else PreferredSize = 1024, #endif Prefer16Bit = true, } }; // Add a lens flare for the key light. var lensFlare = new LensFlare(true) { QuerySize = 0.2f, Size = 0.2f, Name = "Sun Flare" }; var lensFlareTexture = content.Load <Texture2D>("LensFlare/LensFlares"); var circleTexture = new PackedTexture("Circle", lensFlareTexture, new Vector2F(0, 0), new Vector2F(0.25f, 0.5f)); var glowTexture = new PackedTexture("Glow", lensFlareTexture, new Vector2F(0.25f, 0), new Vector2F(0.25f, 0.5f)); var ringTexture = new PackedTexture("Ring", lensFlareTexture, new Vector2F(0.5f, 0), new Vector2F(0.25f, 0.5f)); var haloTexture = new PackedTexture("Halo", lensFlareTexture, new Vector2F(0.75f, 0), new Vector2F(0.25f, 0.5f)); var sunTexture = new PackedTexture("Sun", lensFlareTexture, new Vector2F(0, 0.5f), new Vector2F(0.25f, 0.5f)); var streaksTexture = new PackedTexture("Streaks", lensFlareTexture, new Vector2F(0.25f, 0.5f), new Vector2F(0.25f, 0.5f)); var flareTexture = new PackedTexture("Flare", lensFlareTexture, new Vector2F(0.5f, 0.5f), new Vector2F(0.25f, 0.5f)); lensFlare.Elements.Add(new LensFlareElement(-0.2f, 0.55f, 0.0f, new Color(175, 175, 255, 20), new Vector2F(0.5f, 0.5f), circleTexture)); lensFlare.Elements.Add(new LensFlareElement(0.0f, 0.9f, 0.0f, new Color(255, 255, 255, 255), new Vector2F(0.5f, 0.5f), sunTexture)); lensFlare.Elements.Add(new LensFlareElement(0.0f, 1.8f, 0.0f, new Color(255, 255, 255, 128), new Vector2F(0.5f, 0.5f), streaksTexture)); lensFlare.Elements.Add(new LensFlareElement(0.0f, 2.6f, 0.0f, new Color(255, 255, 200, 64), new Vector2F(0.5f, 0.5f), glowTexture)); lensFlare.Elements.Add(new LensFlareElement(0.5f, 0.12f, 0.0f, new Color(60, 60, 180, 35), new Vector2F(0.5f, 0.5f), circleTexture)); lensFlare.Elements.Add(new LensFlareElement(0.55f, 0.46f, 0.0f, new Color(100, 100, 200, 60), new Vector2F(0.5f, 0.5f), circleTexture)); lensFlare.Elements.Add(new LensFlareElement(0.6f, 0.17f, 0.0f, new Color(120, 120, 220, 40), new Vector2F(0.5f, 0.5f), circleTexture)); lensFlare.Elements.Add(new LensFlareElement(0.85f, 0.2f, 0.0f, new Color(60, 60, 255, 100), new Vector2F(0.5f, 0.5f), ringTexture)); lensFlare.Elements.Add(new LensFlareElement(1.5f, 0.2f, 0.0f, new Color(255, 60, 60, 130), new Vector2F(0.5f, 0.5f), flareTexture)); lensFlare.Elements.Add(new LensFlareElement(0.15f, 0.15f, 0.0f, new Color(255, 60, 60, 90), new Vector2F(0.5f, 0.5f), flareTexture)); lensFlare.Elements.Add(new LensFlareElement(1.3f, 0.6f, 0.0f, new Color(60, 60, 255, 180), new Vector2F(0.5f, 0.5f), haloTexture)); lensFlare.Elements.Add(new LensFlareElement(1.4f, 0.2f, 0.0f, new Color(220, 80, 80, 98), new Vector2F(0.5f, 0.5f), haloTexture)); lensFlare.Elements.Add(new LensFlareElement(1.5f, 0.1f, 0.0f, new Color(220, 80, 80, 85), new Vector2F(0.5f, 0.5f), circleTexture)); lensFlare.Elements.Add(new LensFlareElement(1.6f, 0.5f, 0.0f, new Color(60, 60, 255, 80), new Vector2F(0.5f, 0.5f), haloTexture)); lensFlare.Elements.Add(new LensFlareElement(1.8f, 0.3f, 0.0f, new Color(90, 60, 255, 110), new Vector2F(0.5f, 0.5f), ringTexture)); lensFlare.Elements.Add(new LensFlareElement(1.95f, 0.5f, 0.0f, new Color(60, 60, 255, 120), new Vector2F(0.5f, 0.5f), haloTexture)); lensFlare.Elements.Add(new LensFlareElement(2.0f, 0.15f, 0.0f, new Color(60, 60, 255, 85), new Vector2F(0.5f, 0.5f), circleTexture)); // Add lens flare as a child of the sunlight. var lensFlareNode = new LensFlareNode(lensFlare); _sunlightNode.Children = new SceneNodeCollection(); _sunlightNode.Children.Add(lensFlareNode); // Add scene nodes to scene graph. var scene = _services.GetInstance <IScene>(); scene.Children.Add(_skyboxNode); scene.Children.Add(_ambientLightNode); scene.Children.Add(_sunlightNode); }
private void RenderHiDef(SkyboxNode node, RenderContext context) { var graphicsDevice = context.GraphicsService.GraphicsDevice; var savedRenderState = new RenderStateSnapshot(graphicsDevice); graphicsDevice.RasterizerState = RasterizerState.CullNone; graphicsDevice.DepthStencilState = DepthStencilState.DepthRead; graphicsDevice.BlendState = node.EnableAlphaBlending ? BlendState.AlphaBlend : BlendState.Opaque; bool sourceIsFloatingPoint = TextureHelper.IsFloatingPointFormat(node.Texture.Format); // Set sampler state. (Floating-point textures cannot use linear filtering. (XNA would throw an exception.)) if (sourceIsFloatingPoint) { graphicsDevice.SamplerStates[0] = SamplerState.PointClamp; } else { graphicsDevice.SamplerStates[0] = SamplerState.LinearClamp; } var cameraNode = context.CameraNode; Matrix view = cameraNode.View; Matrix projection = cameraNode.Camera.Projection; // Cube maps are left handed --> Sample with inverted z. (Otherwise, the // cube map and objects or texts in it are mirrored.) var mirrorZ = Matrix.CreateScale(1, 1, -1); Matrix orientation = node.PoseWorld.Orientation; _parameterWorldViewProjection.SetValue((Matrix)(projection * view * new Matrix(orientation, Vector3.Zero) * mirrorZ)); Vector4 color = node.EnableAlphaBlending ? new Vector4((Vector3)node.Color * node.Alpha, node.Alpha) // Premultiplied : new Vector4((Vector3)node.Color, 1); // Opaque _parameterColor.SetValue(color); _textureParameter.SetValue(node.Texture); if (node.Encoding is RgbEncoding) { _parameterTextureSize.SetValue(node.Texture.Size); if (context.IsHdrEnabled()) { _passRgbToRgb.Apply(); } else { _passRgbToSRgb.Apply(); } } else if (node.Encoding is SRgbEncoding) { if (!sourceIsFloatingPoint) { if (context.IsHdrEnabled()) { _passSRgbToRgb.Apply(); } else { _passSRgbToSRgb.Apply(); } } else { throw new GraphicsException("sRGB encoded skybox cube maps must not use a floating point format."); } } else if (node.Encoding is RgbmEncoding) { float max = GraphicsHelper.ToGamma(((RgbmEncoding)node.Encoding).Max); _parameterRgbmMaxValue.SetValue(max); if (context.IsHdrEnabled()) { _passRgbmToRgb.Apply(); } else { _passRgbmToSRgb.Apply(); } } else { throw new NotSupportedException("The SkyBoxRenderer supports only RgbEncoding, SRgbEncoding and RgbmEncoding."); } _submesh.Draw(); savedRenderState.Restore(); }
private void RenderReach(SkyboxNode node, RenderContext context) { var graphicsDevice = context.GraphicsService.GraphicsDevice; var savedRenderState = new RenderStateSnapshot(graphicsDevice); graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; graphicsDevice.DepthStencilState = DepthStencilState.DepthRead; graphicsDevice.BlendState = node.EnableAlphaBlending ? BlendState.AlphaBlend : BlendState.Opaque; graphicsDevice.SamplerStates[0] = SamplerState.LinearClamp; // Change viewport to render all pixels at max z. var originalViewport = graphicsDevice.Viewport; var viewport = originalViewport; viewport.MinDepth = viewport.MaxDepth; graphicsDevice.Viewport = viewport; var cameraNode = context.CameraNode; var view = cameraNode.View; view.Translation = Vector3.Zero; var projection = cameraNode.Camera.Projection; var basicEffect = (BasicEffect)_effect; basicEffect.View = (Matrix)view; basicEffect.Projection = projection; basicEffect.DiffuseColor = (Vector3)node.Color; basicEffect.Alpha = node.EnableAlphaBlending ? node.Alpha : 1; // Scale skybox such that it lies within view frustum: // distance of a skybox corner = √3 // √3 * scale = far // => scale = far / √3 // (Note: If near > far / √3 then the skybox will be clipped.) float scale = projection.Far * 0.577f; var orientation = node.PoseWorld.Orientation; // Positive X basicEffect.Texture = GetTexture2D(graphicsDevice, node.Texture, CubeMapFace.PositiveX); basicEffect.World = (Matrix) new Matrix(orientation * scale, Vector3.Zero); basicEffect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _faceVertices, 0, 2); // Negative X // transform = scale * rotY(180°) var transform = new Matrix(-scale, 0, 0, 0, scale, 0, 0, 0, -scale); basicEffect.Texture = GetTexture2D(graphicsDevice, node.Texture, CubeMapFace.NegativeX); basicEffect.World = (Matrix) new Matrix(orientation * transform, Vector3.Zero); basicEffect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _faceVertices, 0, 2); // Positive Y // transform = scale * rotX(90°) * rotY(90°) transform = new Matrix(0, 0, scale, scale, 0, 0, 0, scale, 0); basicEffect.Texture = GetTexture2D(graphicsDevice, node.Texture, CubeMapFace.PositiveY); basicEffect.World = (Matrix) new Matrix(orientation * transform, Vector3.Zero); basicEffect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _faceVertices, 0, 2); // Negative Y // transform = scale * rotX(-90°) * rotY(90°) transform = new Matrix(0, 0, scale, -scale, 0, 0, 0, -scale, 0); basicEffect.Texture = GetTexture2D(graphicsDevice, node.Texture, CubeMapFace.NegativeY); basicEffect.World = (Matrix) new Matrix(orientation * transform, Vector3.Zero); basicEffect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _faceVertices, 0, 2); // Cube maps are left-handed, where as the world is right-handed! // Positive Z (= negative Z in world space) // transform = scale * rotY(90°) transform = new Matrix(0, 0, scale, 0, scale, 0, -scale, 0, 0); basicEffect.Texture = GetTexture2D(graphicsDevice, node.Texture, CubeMapFace.PositiveZ); basicEffect.World = (Matrix) new Matrix(orientation * transform, Vector3.Zero); basicEffect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _faceVertices, 0, 2); // Negative Z (= positive Z in world space) // transform = scale * rotY(-90°) transform = new Matrix(0, 0, -scale, 0, scale, 0, scale, 0, 0); basicEffect.Texture = GetTexture2D(graphicsDevice, node.Texture, CubeMapFace.NegativeZ); basicEffect.World = (Matrix) new Matrix(orientation * transform, Vector3.Zero); basicEffect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _faceVertices, 0, 2); graphicsDevice.Viewport = originalViewport; savedRenderState.Restore(); }
private void InitializeSky(ContentManager content) { // This scene node is used as the parent of all sky nodes created here. _skyGroupNode = new SceneNode { Name = "ScatteringSkyGroup", Children = new SceneNodeCollection() }; // If we render the sky into a cube map, then we do not add the sky nodes to the scene. if (!_cacheSky) { _scene.Children.Add(_skyGroupNode); } // Add a skybox with milky way cube map. _milkyWayNode = new SkyboxNode(content.Load <TextureCube>("Sky/MilkyWay")) { Color = new Vector3F(MilkyWayLightScale), }; _skyGroupNode.Children.Add(_milkyWayNode); // Add a starfield with all visible stars from a star catalog. InitializeStarfield(); _skyGroupNode.Children.Add(_starfieldNode); // Add a node which draws a sun disk. _sunNode = new SkyObjectNode { GlowExponent0 = 4000, }; _skyGroupNode.Children.Add(_sunNode); // Add a node which draws a moon texture including moon phase. _moonNode = new SkyObjectNode { Texture = new PackedTexture(content.Load <Texture2D>("Sky/Moon")), LightWrap = 0.1f, LightSmoothness = 1, AngularDiameter = new Vector2F(MathHelper.ToRadians(5)), // Disable glow. GlowColor0 = new Vector3F(0), GlowColor1 = new Vector3F(0), }; _skyGroupNode.Children.Add(_moonNode); // Add a ScatteringSky which renders the sky colors using a physically-based model. _scatteringSkyNode = new ScatteringSkyNode { SunIntensity = 1, // Set a base color to get a dark blue instead of a pitch black night. BaseHorizonColor = new Vector3F(0.043f, 0.090f, 0.149f) * 0.01f, BaseZenithColor = new Vector3F(0.024f, 0.051f, 0.102f) * 0.01f, BaseColorShift = 0.5f }; _skyGroupNode.Children.Add(_scatteringSkyNode); if (_enableClouds) { InitializeClouds(); } // The following scene nodes inherit from SkyNode and are rendered by a SkyRenderer // in the DeferredLightingScreen. We have to set the DrawOrder to render them from // back to front. _milkyWayNode.DrawOrder = 0; _starfieldNode.DrawOrder = 1; _sunNode.DrawOrder = 2; _moonNode.DrawOrder = 3; _scatteringSkyNode.DrawOrder = 4; if (_enableClouds) { _cloudLayerNode1.DrawOrder = 5; _cloudLayerNode0.DrawOrder = 6; } }
//-------------------------------------------------------------- #region Methods //-------------------------------------------------------------- protected override void OnLoad() { // Get services. _inputService = _services.GetInstance <IInputService>(); _graphicsService = _services.GetInstance <IGraphicsService>(); _scene = _services.GetInstance <IScene>(); var content = _services.GetInstance <ContentManager>(); var gameObjectService = _services.GetInstance <IGameObjectService>(); // Get camera game object. _cameraObject = (CameraObject)gameObjectService.Objects["Camera"]; // Create SkyNodes. InitializeSky(content); // Create LightNodes InitializeLights(); // Optionally, the sky is captured into a cube map and the cube map is added // to the scene instead of all the sky nodes. if (_cacheSky) { // We use a SceneCaptureNode to create the cube map. // The cube map uses RGBM encoding to store HDR values. var renderToTexture = new RenderToTexture { Texture = new RenderTargetCube( _graphicsService.GraphicsDevice, #if XBOX 512, #else 1024, #endif true, SurfaceFormat.Color, DepthFormat.None), }; var projection = new PerspectiveProjection(); projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 10); _sceneCaptureNode = new SceneCaptureNode(renderToTexture) { // Note: The scene is captured at the origin (0, 0, 0). CameraNode = new CameraNode(new Camera(projection)), }; SkyboxNode = new SkyboxNode { Encoding = ColorEncoding.Rgbm, Texture = (TextureCube)renderToTexture.Texture, }; _scene.Children.Add(SkyboxNode); } // The Ephemeris class computes the positions of the sun and the moon as seen // from any place on the earth. _ephemeris = new Ephemeris { // Seattle, Washington //Latitude = 47, //Longitude = 122, //Altitude = 100 // Vienna //Latitude = 48, //Longitude = -16, //Altitude = 0 // Equator Latitude = 0, Longitude = 0, Altitude = 0 }; // Update the positions of sky objects and the lights. UpdateSky(); // Create cube map. if (_cacheSky) { UpdateCubeMap(TimeSpan.Zero); } // Add GUI controls to the Options window. var sampleFramework = _services.GetInstance <SampleFramework>(); var optionsPanel = sampleFramework.AddOptions("Game Objects"); var panel = SampleHelper.AddGroupBox(optionsPanel, "DynamicSkyObject"); SampleHelper.AddSlider( panel, "Time", "F2", 0, 24, (float)Time.TimeOfDay.TotalHours, value => { var time = Time; time = time.Subtract(time.TimeOfDay).Add(TimeSpan.FromHours(value)); Time = time; }); SampleHelper.AddCheckBox( panel, "Enable ambient light", EnableAmbientLight, isChecked => EnableAmbientLight = isChecked); SampleHelper.AddSlider( panel, "Fog sample angle", "F2", 0, ConstantsF.PiOver2, FogSampleAngle, value => FogSampleAngle = value); SampleHelper.AddSlider( panel, "Fog saturation", "F2", 0, 1, FogSaturation, value => FogSaturation = value); //SampleHelper.AddSlider( // panel, // "Fog scattering symmetry R", // "F2", // 0, // 1, // FogScatteringSymmetry.X, // value => FogScatteringSymmetry = new Vector3F(value, FogScatteringSymmetry.Y, FogScatteringSymmetry.Z)); //SampleHelper.AddSlider( // panel, // "Fog scattering symmetry G", // "F2", // 0, // 1, // FogScatteringSymmetry.Y, // value => FogScatteringSymmetry = new Vector3F(FogScatteringSymmetry.X, value, FogScatteringSymmetry.Z)); //SampleHelper.AddSlider( // panel, // "Fog scattering symmetry B", // "F2", // 0, // 1, // FogScatteringSymmetry.Z, // value => FogScatteringSymmetry = new Vector3F(FogScatteringSymmetry.X, FogScatteringSymmetry.Y, value)); }
public LdrSkySample(Microsoft.Xna.Framework.Game game) : base(game) { SampleFramework.IsMouseVisible = false; var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService) { RenderCallback = Render, }; GraphicsService.Screens.Insert(0, delegateGraphicsScreen); // Add a custom game object which controls the camera. _cameraObject = new CameraObject(Services); GameObjectService.Objects.Add(_cameraObject); var spriteFont = UIContentManager.Load <SpriteFont>("UI Themes/BlendBlue/Default"); _debugRenderer = new DebugRenderer(GraphicsService, spriteFont); _cieSkyFilter = new CieSkyFilter(GraphicsService); _cieSkyFilter.Exposure = 5; _cieSkyFilter.Strength = 0.9f; _gradientSky = new GradientSkyNode(); //_gradientSky.GroundColor = new Vector4F(0, 0, 1, 1); //_gradientSky.ZenithColor = new Vector4F(0, 0, 1, 1); //_gradientSky.FrontColor = new Vector4F(0, 0, 1, 1); //_gradientSky.BackColor = new Vector4F(0, 0, 1, 1); //_gradientSky.FrontZenithShift = 0.3f; //_gradientSky.FrontGroundShift = 0.1f; //_gradientSky.BackGroundShift = 0.1f; _gradientSky.CieSkyStrength = 0; _gradientTextureSky = new GradientTextureSkyNode(); _gradientTextureSky.TimeOfDay = _time.TimeOfDay; _gradientTextureSky.Color = new Vector4F(1); _gradientTextureSky.FrontTexture = ContentManager.Load <Texture2D>("Sky/GradientSkyFront"); _gradientTextureSky.BackTexture = ContentManager.Load <Texture2D>("Sky/GradientSkyBack"); _gradientTextureSky.CieSkyStrength = 1; _scatteringSky = new ScatteringSkyNode(); _scatteringSky.SunIntensity *= 2; _scatteringSky.BetaMie *= 2; _scatteringSky.GMie = 0.75f; _scatteringSky.ScaleHeight = _scatteringSky.AtmosphereHeight * 0.25f; InitializeStarfield(); _cloudMapRenderer = new CloudMapRenderer(GraphicsService); _skyRenderer = new SkyRenderer(GraphicsService); _milkyWay = ContentManager.Load <TextureCube>("Sky/MilkyWay"); _milkyWaySkybox = new SkyboxNode(_milkyWay) { Color = new Vector3F(0.05f) }; _sun = new SkyObjectNode { GlowColor0 = new Vector3F(1, 1, 1) * 5, GlowExponent0 = 4000, //GlowColor1 = new Vector3F(0.4f) * 0.1f, //GlowExponent1 = 100 }; _moon = new SkyObjectNode { Texture = new PackedTexture(ContentManager.Load <Texture2D>("Sky/Moon")), SunLight = new Vector3F(1, 1, 1) * 1, AmbientLight = new Vector3F(0.001f) * 1, LightWrap = 0.1f, LightSmoothness = 1, AngularDiameter = new Vector2F(MathHelper.ToRadians(5)), GlowColor0 = new Vector3F(0.005f * 0), GlowCutoffThreshold = 0.001f, GlowExponent0 = 100 }; var cloudMap = new LayeredCloudMap { Density = 10, Coverage = 0.5f, Size = 1024, }; var scale = CreateScale(0.2f); cloudMap.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 0.011f * 0); cloudMap.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 0.017f * 0); cloudMap.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 0.033f * 0); cloudMap.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 0.043f * 0); cloudMap.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 0.051f * 0); cloudMap.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 0.059f * 0); cloudMap.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 0.067f * 0); cloudMap.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 128f, 0.081f * 0); _cloudLayerNode = new CloudLayerNode(cloudMap) { ForwardScatterScale = 2.5f, ForwardScatterOffset = 0.3f, TextureMatrix = CreateScale(0.5f), SkyCurvature = 0.9f, NumberOfSamples = 16, }; _ephemeris = new Ephemeris(); // Approx. location of Ternberg: Latitude = 48, Longitude = 15, Altitude = 300 _ephemeris.Latitude = 0; _ephemeris.Longitude = 15; _ephemeris.Altitude = 300; #if XBOX //_time = new DateTime(2013, 5, 1, 17, 17, 0, 0); _time = DateTime.Now; #else _time = new DateTimeOffset(2013, 5, 1, 12, 0, 0, 0, TimeSpan.Zero); //_time = DateTimeOffset.UtcNow; #endif UpdateEphemeris(); _milkyWaySkybox.DrawOrder = 0; _starfield.DrawOrder = 1; _sun.DrawOrder = 2; _moon.DrawOrder = 3; _scatteringSky.DrawOrder = 4; _gradientSky.DrawOrder = 4; _gradientTextureSky.DrawOrder = 4; _cloudLayerNode.DrawOrder = 5; _skyNodes = new SceneNode[] { _milkyWaySkybox, _starfield, _sun, _moon, _scatteringSky, //_gradientSky, //_gradientTextureSky, _cloudLayerNode, }; var graphicsDevice = GraphicsService.GraphicsDevice; _skybox = new SkyboxNode( new RenderTargetCube(graphicsDevice, 512, false, SurfaceFormat.Color, DepthFormat.None)) { Encoding = ColorEncoding.Rgbm, }; _hdrFilter = new HdrFilter(GraphicsService) { MinExposure = 0.5f, MaxExposure = 2, BloomIntensity = 1, BloomThreshold = 0.6f, AdaptionSpeed = 100, }; _colorEncoder = new ColorEncoder(GraphicsService) { SourceEncoding = ColorEncoding.Rgb, TargetEncoding = _skybox.Encoding, }; }
private void button1_Click(object sender, EventArgs e) { switch (listBox1.SelectedItem) { case "SkyboxNode": string sn_skybox = "Content/skybox.png"; InputBoxes.ShowInputDialog(ref sn_skybox, "Skybox"); SkyboxNode node = SkyboxNode.Create(new Bitmap(Image.FromFile(sn_skybox))); node.Name = "NewSkybox"; node.Parent = nodebase; break; case "TextBillboardNode": string sn_billboard = "Hello World!"; TextBillboardNode textBillboardNode = TextBillboardNode.Create(200, 40, 65535); textBillboardNode.Name = "NewBillboard"; textBillboardNode.Parent = nodebase; textBillboardNode.Text = sn_billboard; textBillboardNode.EnableRendering = ThreeFlags.None; break; case "CubeNode": CubeNode cube = CubeNode.Create(); cube.Name = "NewCube"; cube.Parent = nodebase; break; case "ShadowVolumeNode": CubeModel model = new CubeModel(); string sv_pos = CubeModel.strPosition; string sv_normal = "0 0 0"; InputBoxes.ShowInputDialog(ref sv_pos, "Volume Position"); InputBoxes.ShowInputDialog(ref sv_pos, "Volume Normal"); ShadowVolumeNode shadow = ShadowVolumeNode.Create(model, sv_pos, sv_normal, new vec3(1, 1, 1)); shadow.Name = "NewLight"; shadow.Parent = nodebase; break; case "TerrainNode": TerrainNode terrain = TerrainNode.Create(); terrain.Parent = nodebase; terrain.Name = "NewTerrain"; break; case "SpotLightNode": CubeModel sp_model = new CubeModel(); string sp_pos = "0 0 0"; string sp_target = "0 0 0"; InputBoxes.ShowInputDialog(ref sp_pos, "Volume Position"); InputBoxes.ShowInputDialog(ref sp_target, "Volume Target"); string[] sppositions_STR = sp_pos.Split(' '); vec3 sp_posv = new vec3(float.Parse(sppositions_STR[0]), float.Parse(sppositions_STR[1]), float.Parse(sppositions_STR[2])); string[] sptarget_STR = sp_target.Split(' '); vec3 sp_tarv = new vec3(float.Parse(sptarget_STR[0]), float.Parse(sptarget_STR[1]), float.Parse(sptarget_STR[2])); SpotLight spotlight = new SpotLight(sp_posv, sp_tarv, 45); SpotLightNode spotlightnode = SpotLightNode.Create(spotlight, sp_model, sp_pos, sp_pos, new vec3(1, 1, 1)); spotlightnode.Parent = nodebase; spotlightnode.Name = "NewSpotlight"; lastspotlight = spotlight; break; case "CubeLightTestNode": CubeLightTestNode cubeLightTest = CubeLightTestNode.Create(); cubeLightTest.Parent = nodebase; cubeLightTest.Name = "NewCubeLightTest"; cubeLightTest.SetLight(lastspotlight); break; } src_form.Match(src_form.treeView, nodebase); AudioSystem.PlayAudio("GameSounds/clickfast.wav"); Close(); }
protected override void OnLoad() { _inputService = _services.GetInstance <IInputService>(); _graphicsService = _services.GetInstance <IGraphicsService>(); _scene = _services.GetInstance <IScene>(); var content = _services.GetInstance <ContentManager>(); var gameObjectService = _services.GetInstance <IGameObjectService>(); _cameraObject = (CameraObject)gameObjectService.Objects["Camera"]; // This scene node is used as the parent of all other scene nodes created here. _groupNode = new SceneNode { Name = "ScatteringSkyGroup", Children = new SceneNodeCollection() }; _scene.Children.Add(_groupNode); // Add a skybox with milky way cube map. _milkyWayNode = new SkyboxNode(content.Load <TextureCube>("Sky/MilkyWay")) { Color = new Vector3F(MilkyWayLightScale), }; _groupNode.Children.Add(_milkyWayNode); // Add a starfield with all visible stars from a star catalog. InitializeStarfield(); _groupNode.Children.Add(_starfieldNode); // Add a node which draws a sun disk. _sunNode = new SkyObjectNode { GlowExponent0 = 4000, }; _groupNode.Children.Add(_sunNode); // Add a node which draws a moon texture including moon phase. _moonNode = new SkyObjectNode { Texture = new PackedTexture(content.Load <Texture2D>("Sky/Moon")), LightWrap = 0.1f, LightSmoothness = 1, AngularDiameter = new Vector2F(MathHelper.ToRadians(5)), // Disable glow. GlowColor0 = new Vector3F(0), GlowColor1 = new Vector3F(0), }; _groupNode.Children.Add(_moonNode); // Add a ScatteringSky which renders the sky colors using a physically-based model. _scatteringSkyNode = new ScatteringSkyNode { SunIntensity = 1, // Set a base color to get a dark blue instead of a pitch black night. BaseHorizonColor = new Vector3F(0.043f, 0.090f, 0.149f) * 0.01f, BaseZenithColor = new Vector3F(0.024f, 0.051f, 0.102f) * 0.01f, BaseColorShift = 0.5f }; _groupNode.Children.Add(_scatteringSkyNode); if (_enableCloudLayer) { // Add a CloudLayerNode which renders dynamic clouds. // This layer represents dense, lit clouds at low altitude. _cloudMap0 = new LayeredCloudMap { Density = 15, Coverage = 0.5f, Size = 2048, }; _cloudLayerNode0 = new CloudLayerNode(_cloudMap0) { SkyCurvature = 0.9f, TextureMatrix = CreateScale(0.5f), ForwardScatterExponent = 10, ForwardScatterScale = 10f, ForwardScatterOffset = 0.3f, NumberOfSamples = 16, HorizonFade = 0.03f, }; // Define the layers which are combined to a single CloudMap.Texture in the CloudMapRenderer. var scale = CreateScale(0.2f); float animationScale = 0.01f; animationScale = 0; // Uncomment to enable formation and dissolution of clouds. _cloudMap0.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 1 * animationScale); _cloudMap0.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 5 * animationScale); _cloudMap0.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 13 * animationScale); _cloudMap0.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 27 * animationScale); _cloudMap0.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 43 * animationScale); _cloudMap0.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 77 * animationScale); _cloudMap0.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 127 * animationScale); _cloudMap0.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 64f, 200 * animationScale); _groupNode.Children.Add(_cloudLayerNode0); // Add a second CloudLayerNode which renders dynamic clouds. // This layer represents light clouds at high altitude. _cloudMap1 = new LayeredCloudMap { Density = 2, Coverage = 0.4f, Size = 1024, Seed = 77777, }; _cloudLayerNode1 = new CloudLayerNode(_cloudMap1) { SkyCurvature = 0.9f, TextureMatrix = CreateScale(0.5f), ForwardScatterExponent = 10, ForwardScatterScale = 10f, ForwardScatterOffset = 0f, NumberOfSamples = 0, // No samples to disable lighting calculations. HorizonFade = 0.03f, Alpha = 0.5f, // Make these clouds more transparent. }; scale = CreateScale(0.25f); _cloudMap1.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 0); _cloudMap1.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 0); _cloudMap1.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 0); _cloudMap1.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 0); _cloudMap1.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 0); _cloudMap1.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 0); _cloudMap1.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 0); _cloudMap1.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 128f, 0); _groupNode.Children.Add(_cloudLayerNode1); } // The following scene nodes inherit from SkyNode and are rendered by a SkyRenderer // in the DeferredLightingScreen. We have to set the DrawOrder to render them from // back to front. _milkyWayNode.DrawOrder = 0; _starfieldNode.DrawOrder = 1; _sunNode.DrawOrder = 2; _moonNode.DrawOrder = 3; _scatteringSkyNode.DrawOrder = 4; if (_enableCloudLayer) { _cloudLayerNode1.DrawOrder = 5; _cloudLayerNode0.DrawOrder = 6; } // Add an ambient light. var ambientLight = new AmbientLight { HemisphericAttenuation = 1, }; _ambientLightNode = new LightNode(ambientLight) { Name = "Ambient", }; _groupNode.Children.Add(_ambientLightNode); // Add a directional light for the sun. _sunlightNode = new LightNode(new DirectionalLight()) { Name = "Sunlight", Priority = 10, // This is the most important light. // This light uses Cascaded Shadow Mapping. Shadow = new CascadedShadow { PreferredSize = 1024, DepthBiasScale = new Vector4F(0.99f), DepthBiasOffset = new Vector4F(0), FadeOutDistance = 20, MaxDistance = 30, MinLightDistance = 100, ShadowFog = 0.5f, JitterResolution = 3000, SplitDistribution = 0.7f } }; _groupNode.Children.Add(_sunlightNode); // Add a directional light for the moonlight. _moonlightNode = new LightNode(new DirectionalLight()) { Name = "Moonlight", Priority = 5, }; _groupNode.Children.Add(_moonlightNode); // The Ephemeris class computes the positions of the sun and the moon as seen // from any place on the earth. _ephemeris = new Ephemeris(); // Seattle, Washington _ephemeris.Latitude = 47; _ephemeris.Longitude = 122; _ephemeris.Altitude = 100; #if XBOX _time = DateTime.Now; #else _time = DateTimeOffset.Now; #endif // Update the positions of sky objects and the lights. UpdateSky(); }
//-------------------------------------------------------------- #region Methods //-------------------------------------------------------------- protected override void OnLoad() { // Get services. _inputService = _services.GetInstance <IInputService>(); _graphicsService = _services.GetInstance <IGraphicsService>(); _scene = _services.GetInstance <IScene>(); var content = _services.GetInstance <ContentManager>(); var gameObjectService = _services.GetInstance <IGameObjectService>(); // Get camera game object. _cameraObject = (CameraObject)gameObjectService.Objects["Camera"]; // Create SkyNodes. InitializeSky(content); // Create LightNodes InitializeLights(); // Optionally, the sky is captured into a cube map and the cube map is added // to the scene instead of all the sky nodes. if (_cacheSky) { // We use a SceneCaptureNode to create the cube map. // The cube map uses RGBM encoding to store HDR values. var renderToTexture = new RenderToTexture { Texture = new RenderTargetCube( _graphicsService.GraphicsDevice, 1024, false, SurfaceFormat.Color, DepthFormat.None), }; var projection = new PerspectiveProjection(); projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 10); _sceneCaptureNode = new SceneCaptureNode(renderToTexture) { // Note: The scene is captured at the origin (0, 0, 0). CameraNode = new CameraNode(new Camera(projection)), }; SkyboxNode = new SkyboxNode { Encoding = ColorEncoding.Rgbm, Texture = (TextureCube)renderToTexture.Texture, }; _scene.Children.Add(SkyboxNode); } // The Ephemeris class computes the positions of the sun and the moon as seen // from any place on the earth. _ephemeris = new Ephemeris { // Seattle, Washington //Latitude = 47, //Longitude = 122, //Altitude = 100 // Vienna //Latitude = 48, //Longitude = -16, //Altitude = 0 // Equator Latitude = 0, Longitude = 90, Altitude = 0 }; #if XBOX _time = DateTime.Now; #else _time = DateTimeOffset.Now; #endif // Update the positions of sky objects and the lights. UpdateSky(); // Create cube map. if (_cacheSky) { UpdateCubeMap(TimeSpan.Zero); } }