Exemplo n.º 1
0
        /// <summary>
        /// Renders sky with specified technique
        /// </summary>
        /// <param name="rendCtxt"></param>
        /// <param name="techName"></param>
        public void Render(GameTime gameTime, DepthStencilSurface depthBuffer, RenderTargetSurface hdrTarget, Matrix view, Matrix projection, RenderTarget2D cloudTarget, RenderTarget2D smallerCloudTarget)
        {
            var camera = Game.GetService <Camera>();

            var scale    = Matrix.Scaling(Params.SkySphereSize);
            var rotation = Matrix.Identity;

            var sunPos   = GetSunDirection();
            var sunColor = GetSunGlowColor();

            rs.ResetStates();

            //rs.DepthStencilState = depthBuffer==null? DepthStencilState.None : DepthStencilState.Default ;

            rs.SetViewport(0, 0, hdrTarget.Width, hdrTarget.Height);

            rs.SetTargets(depthBuffer, hdrTarget);

            var viewMatrix = view;
            var projMatrix = projection;

            skyConstsData.MatrixWVP    = scale * rotation * MathUtil.Transformation(viewMatrix.Right, viewMatrix.Up, viewMatrix.Backward) * projMatrix;
            skyConstsData.SunPosition  = sunPos;
            skyConstsData.SunColor     = sunColor;
            skyConstsData.Turbidity    = Params.SkyTurbidity;
            skyConstsData.Temperature  = Temperature.Get(Params.SunTemperature);
            skyConstsData.SkyIntensity = Params.SkyIntensity;

            skyConstsCB.SetData(skyConstsData);

            rs.VertexShaderConstants[0] = skyConstsCB;
            rs.PixelShaderConstants[0]  = skyConstsCB;


            //
            //	Sky :
            //
            SkyFlags flags = SkyFlags.PROCEDURAL_SKY;

            ApplyColorSpace(ref flags);

            rs.PipelineState = factory[(int)flags];

            for (int j = 0; j < skySphere.Meshes.Count; j++)
            {
                var mesh = skySphere.Meshes[j];

                rs.SetupVertexInput(vertexBuffers[j], indexBuffers[j]);
                rs.DrawIndexed(mesh.IndexCount, 0, 0);
            }



            //
            //	Clouds :
            //
            scale = Matrix.Scaling(Params.SkySphereSize, Params.SkySphereSize, Params.SkySphereSize);
            //scale		=	Matrix.Scaling( Params.SkySphereSize, Params.SkySphereSize * 0.1f, Params.SkySphereSize );
            skyConstsData.MatrixWVP    = scale * rotation * MathUtil.Transformation(viewMatrix.Right, viewMatrix.Up, viewMatrix.Backward) * projMatrix;
            skyConstsData.SunPosition  = sunPos;
            skyConstsData.SunColor     = GetSunLightColor();
            skyConstsData.Turbidity    = Params.SkyTurbidity;
            skyConstsData.Temperature  = Temperature.Get(Params.SunTemperature);
            skyConstsData.SkyIntensity = Params.SkyIntensity;
            skyConstsData.Ambient      = GetAmbientLevel().ToVector3();
            skyConstsData.Time         = (float)gameTime.Total.TotalSeconds;

            skyConstsCB.SetData(skyConstsData);

            rs.SetTargets(depthBuffer, hdrTarget);
//			rs.SetTargets( null, cloudTarget.Surface );

            flags = SkyFlags.CLOUDS;

            //int i = 0;
            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    flags = SkyFlags.CLOUDS | SkyFlags.A | SkyFlags.RED;
                }
                if (i == 1)
                {
                    flags = SkyFlags.CLOUDS | SkyFlags.B | SkyFlags.GREEN;
                }
                if (i == 2)
                {
                    flags = SkyFlags.CLOUDS | SkyFlags.C | SkyFlags.BLUE;
                }

                ApplyColorSpace(ref flags);

                rs.PipelineState           = factory[(int)flags];
                rs.PixelShaderResources[0] = clouds;
                rs.PixelShaderResources[1] = cirrus;
                rs.PixelShaderResources[2] = noise;
                rs.PixelShaderResources[3] = arrows;
                rs.PixelShaderSamplers[0]  = SamplerState.AnisotropicWrap;



                for (int j = 0; j < cloudSphere.Meshes.Count; j++)
                {
                    var mesh = cloudSphere.Meshes[j];

                    rs.SetupVertexInput(cloudVertexBuffers[j], cloudIndexBuffers[j]);
                    rs.DrawIndexed(mesh.IndexCount, 0, 0);
                }
            }
                        #if false
            //Blur
            var filter = Game.GetService <Filter>();

            //filter.RadialBlur(smallerCloudTarget.Surface, cloudTarget, new Vector2(0.5f, 0.5f), 1.0f, -0.2f);

            //if(Game.InputDevice.IsKeyDown(Keys.LeftButton))
            //{
            Vector4 sunProj = Vector4.Transform(new Vector4(sunPos, 0), viewMatrix);
            sunProj = Vector4.Transform(sunProj, projMatrix);
            sunProj = new Vector4(sunProj.X / sunProj.W, sunProj.Y / sunProj.W, sunProj.Z / sunProj.W, 1);
            Vector2 relSunPosition = new Vector2(0.5f, 0.5f);

            //if ( Math.Abs(sunProj.X ) < 1 && Math.Abs (sunProj.Y) < 1){
            relSunPosition = new Vector2((sunProj.X + 1) / 2, -(sunProj.Y - 1) / 2);
            //}

            //Log.Message(relSunPosition + "");
            filter.RadialBlur(smallerCloudTarget.Surface, cloudTarget, relSunPosition, 1.0f, -0.2f);
            //}



            flags = SkyFlags.BLUR_CLOUD;
            skyConstsData.MatrixWVP = Matrix.Identity;
            skyConstsCB.SetData(skyConstsData);

            rs.SetTargets(null, hdrTarget);

            ApplyColorSpace(ref flags);
            rs.VertexShaderConstants[0] = skyConstsCB;
            rs.PixelShaderConstants[0]  = skyConstsCB;

            rs.PipelineState           = factory[(int)flags];
            rs.PixelShaderResources[4] = cloudTarget;
            rs.PixelShaderSamplers[1]  = SamplerState.LinearWrap;

            var v0 = new VertexColorTextureTBN {
                Position = new Vector3(-1.0f, -1.0f, 0), TexCoord = new Vector2(0, 1),
            };
            var v1 = new VertexColorTextureTBN {
                Position = new Vector3(1.0f, 1.0f, 0), TexCoord = new Vector2(1, 0),
            };
            var v2 = new VertexColorTextureTBN {
                Position = new Vector3(-1.0f, 1.0f, 0), TexCoord = new Vector2(0, 0),
            };
            var v3 = new VertexColorTextureTBN {
                Position = new Vector3(-1.0f, -1.0f, 0), TexCoord = new Vector2(0, 1),
            };
            var v4 = new VertexColorTextureTBN {
                Position = new Vector3(1.0f, -1.0f, 0), TexCoord = new Vector2(1, 1),
            };
            var v5 = new VertexColorTextureTBN {
                Position = new Vector3(1.0f, 1.0f, 0), TexCoord = new Vector2(1, 0),
            };                                                                                                                                    //*/

            var data = new VertexColorTextureTBN[] { v0, v1, v2, v3, v4, v5 };
            //Log.Message(""+v0.TexCoord);
            vertexBufferBlur.SetData(data, 0, 6);
            //for ( int j=0; j<cloudSphere.Meshes.Count; j++) {
            //		var mesh = cloudSphere.Meshes[j];

            //		rs.SetupVertexInput( cloudVertexBuffers[j], cloudIndexBuffers[j] );
            //		rs.DrawIndexed( mesh.IndexCount, 0, 0 );
            //	}
            rs.SetupVertexInput(vertexBufferBlur, null);
            rs.Draw(6, 0);
#endif

            rs.ResetStates();
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        void LoadContent()
        {
            SafeDispose(ref factory);
            SafeDispose(ref vertexBuffers);
            SafeDispose(ref indexBuffers);

            surfaceShader = Game.Content.Load <Ubershader>("surface");
            factory       = new StateFactory(surfaceShader, typeof(SurfaceFlags), Primitive.TriangleList, VertexInputElement.FromStructure <VertexColorTextureTBN>());


            scene = Game.Content.Load <Scene>(@"Scenes\testScene");

            vertexBuffers = scene.Meshes
                            .Select(mesh => VertexBuffer.Create(Game.GraphicsDevice, mesh.Vertices.Select(v => VertexColorTextureTBN.Convert(v)).ToArray()))
                            .ToArray();

            indexBuffers = scene.Meshes
                           .Select(mesh => IndexBuffer.Create(Game.GraphicsDevice, mesh.GetIndices()))
                           .ToArray();


            surfaceProps = scene.Materials
                           .Select(mtrl => new SurfaceProperties()
            {
                Diffuse   = LoadTexture2D(mtrl.TexturePath, "|srgb", defaultDiffuse),
                Specular  = LoadTexture2D(mtrl.TexturePath, "_spec", defaultSpecular),
                NormalMap = LoadTexture2D(mtrl.TexturePath, "_local", defaultNormalMap),
                Emission  = LoadTexture2D(mtrl.TexturePath, "_glow|srgb", defaultEmission),
            })
                           .ToArray();
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        void LoadContent()
        {
            SafeDispose(ref factory);

            skySphere   = Game.Content.Load <Scene>("skySphere");
            cloudSphere = Game.Content.Load <Scene>("cloudSphere");
            clouds      = Game.Content.Load <Texture2D>("clouds|srgb");
            cirrus      = Game.Content.Load <Texture2D>("cirrus|srgb");
            noise       = Game.Content.Load <Texture2D>("cloudNoise");
            arrows      = Game.Content.Load <Texture2D>("arrowsAll");

            vertexBufferBlur = new VertexBuffer(Game.GraphicsDevice, typeof(VertexColorTextureTBN), 6);
            vertexBuffers    = skySphere.Meshes
                               .Select(mesh => VertexBuffer.Create(Game.GraphicsDevice, mesh.Vertices.Select(v => VertexColorTextureTBN.Convert(v)).ToArray()))
                               .ToArray();

            indexBuffers = skySphere.Meshes
                           .Select(mesh => IndexBuffer.Create(Game.GraphicsDevice, mesh.GetIndices()))
                           .ToArray();

            cloudVertexBuffers = cloudSphere.Meshes
                                 .Select(mesh => VertexBuffer.Create(Game.GraphicsDevice, mesh.Vertices.Select(v => VertexColorTextureTBN.Convert(v)).ToArray()))
                                 .ToArray();

            cloudIndexBuffers = cloudSphere.Meshes
                                .Select(mesh => IndexBuffer.Create(Game.GraphicsDevice, mesh.GetIndices()))
                                .ToArray();

            sky     = Game.Content.Load <Ubershader>("sky");
            factory = new StateFactory(sky, typeof(SkyFlags), (ps, i) => EnumFunc(ps, (SkyFlags)i));
        }