public void Render(Cloud cloud, Matrix view, Matrix proj, Vector3 sunDir, Vector3 sunlightColor) { device.SetRenderTarget(lightingTarget); device.Clear(Color.Transparent); effectLight.Parameters["sunDir"].SetValue(sunDir); effectLight.Parameters["color"].SetValue(sunlightColor); effectLight.Parameters["scatteredMin"].SetValue(ScatteredMin); effectLight.Parameters["scatteredMax"].SetValue(ScatteredMax); effectLight.Parameters["dirMin"].SetValue(DirectionalMin); effectLight.Parameters["dirMax"].SetValue(DirectionalMax); effectLight.Parameters["dirExp"].SetValue(DirectionalExp); effectLight.Parameters["constant"].SetValue(Constant); //device.DepthStencilState = DepthStencilState.Default; foreach (var sphere in cloud.Spheres) { Matrix world = Matrix.CreateScale(sphere.radius) * Matrix.CreateTranslation(sphere.position); effectLight.Parameters["worldViewProj"].SetValue(world * view * proj); effectLight.Parameters["worldView"].SetValue(world); mesh.Meshes[0].MeshParts[0].Effect = effectLight; mesh.Meshes[0].Draw(); } device.BlendState = BlendState.AlphaBlend; device.SetRenderTarget(blurVTarget); device.Clear(Color.Transparent); effectPostprocess.Parameters["cloudLight"].SetValue(lightingTarget); effectPostprocess.Parameters["offset"].SetValue(1.0f / (float)targetResolution); effectPostprocess.CurrentTechnique.Passes[0].Apply(); device.SetVertexBuffer(fullscreenQuad); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); device.SetRenderTarget(BypassDistortion ? null : blurHTarget); if (!BypassDistortion) device.Clear(Color.Transparent); effectPostprocess.Parameters["cloudLight"].SetValue(blurVTarget); effectPostprocess.Parameters["offset"].SetValue(1.0f / (float)targetResolution); effectPostprocess.CurrentTechnique.Passes[1].Apply(); device.SetVertexBuffer(fullscreenQuad); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); if (!BypassDistortion) { device.SetRenderTarget(null); effectPostprocess.Parameters["cloudLight"].SetValue(blurHTarget); effectPostprocess.Parameters["offset"].SetValue(1.0f / (float)targetResolution); effectPostprocess.Parameters["distortion"].SetValue(distortion); effectPostprocess.Parameters["hardness"].SetValue(Hardness); effectPostprocess.Parameters["frequency"].SetValue(DistortionFreq); effectPostprocess.Parameters["amplitude"].SetValue(DistortionAmp); effectPostprocess.CurrentTechnique.Passes[2].Apply(); device.SetVertexBuffer(fullscreenQuad); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); } device.BlendState = BlendState.Opaque; }
public CloudLayer() { Clouds = new List<Cloud>(); Cloud c = new Cloud(); c.Spheres.Add(new Sphere(new Vector3(0.6f), 0.1f)); c.Spheres.Add(new Sphere(new Vector3(0.8f, 0.7f, 0.6f), 0.1f)); c.Spheres.Add(new Sphere(new Vector3(-2.0f, 1.0f, -2.0f), 0.2f)); c.Spheres.Add(new Sphere(new Vector3(-2.2f, 1.05f, -1.95f), 0.2f)); c.Spheres.Add(new Sphere(new Vector3(-1.95f, 0.99f, -2.01f), 0.2f)); c.Spheres.Add(new Sphere(new Vector3(-1.9f, 1.1f, -2.01f), 0.2f)); Clouds.Add(c); }
public void generateClouds(int n) { while (Clouds.Count < n) { Random rand = new Random(); double theta = 90.0 - 30.0 * rand.NextDouble(); double phi = 360.0 * rand.NextDouble(); float x = (float) (Math.Cos(theta) * Math.Cos(phi)); float z = (float) (Math.Cos(theta) * Math.Sin(phi)); float y = (float) (1.0 + rand.NextDouble()); Cloud c = new Cloud(); c.Spheres.Add(new Sphere(new Vector3(x, y, z), 0.2f)); c.Spheres.Add(new Sphere(new Vector3(x-0.1f, y, z+0.1f), 0.2f)); c.Spheres.Add(new Sphere(new Vector3(x-0.2f, y, z-0.1f), 0.2f)); Clouds.Add(c); } while (Clouds.Count > n) { Clouds.RemoveAt(Clouds.Count - 1); } }