public class LightEmmiter { // : X, Y, Z public Vector3 position; // public Vector3 color; // ( — ) public float corrector; // public float radius; // internal void UpdateEffect(EffectParameter effectParameter) { effectParameter.StructureMembers["position"].SetValue(position); effectParameter.StructureMembers["color"].SetValue(color * corrector); effectParameter.StructureMembers["invRadius"].SetValue(1f / radius); } }
Texture2D texture; // Color ( ) Texture2D textureNormal; // Normal private Effect deferred; // SpriteFont spriteFont; // , Debug- EffectParameter lightParameter; // private float lightRadius; // private float lightZ; // Z- private float lightC; // LightEmmiter[] lights = new LightEmmiter[2]; //
texture = Content.Load<Texture2D>("test1"); // textureNormal = Content.Load<Texture2D>("test1_map"); // deferred = Content.Load<Effect>("deferred"); // spriteFont = Content.Load<SpriteFont>("default"); // lightRadius = 320f; // lightZ = 50f; // Z- lightC = 1f; // lights[0] = new LightEmmiter(); lights[0].position = new Vector3(20, 30, 0); lights[0].radius = lightRadius; lights[0].corrector = lightC; lights[0].color = new Vector3(1f, 0f, 0f); lights[1] = new LightEmmiter(); lights[1].position = new Vector3(20, 30, 0); lights[1].radius = lightRadius; lights[1].corrector = lightC; lights[1].color = new Vector3(1f, 1f, 1f);
// RenderTarget — GraphicsDevice.Clear(Color.LightSkyBlue); GraphicsDevice.SetRenderTarget(null); // lights[0].position = new Vector3(Mouse.GetState().X, Mouse.GetState().Y, lightZ); lights[0].radius = lightRadius; lights[0].corrector = lightC; lights[1].position = new Vector3(800 - Mouse.GetState().X, 480 - Mouse.GetState().Y, lightZ); lights[1].radius = lightRadius; lights[1].corrector = lightC; // deferred.CurrentTechnique = deferred.Techniques["Deferred"]; // deferred.Parameters["screenWidth"].SetValue(GraphicsDevice.Viewport.Width); deferred.Parameters["screenHeight"].SetValue(GraphicsDevice.Viewport.Height); deferred.Parameters["ambientColor"].SetValue(new Vector3(1, 1, 1) * 0.1f); deferred.Parameters["numberOfLights"].SetValue(2); deferred.Parameters["normaltexture"].SetValue(textureNormal); // lights- lightParameter = deferred.Parameters["lights"]; // lights- for (int i = 0; i < lights.Length; i++) { LightEmmiter l = lights[i]; l.UpdateEffect(lightParameter.Elements[i]); } // ( ) foreach (EffectPass pass in deferred.CurrentTechnique.Passes) { spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise); pass.Apply(); spriteBatch.Draw(texture, new Rectangle(0, 0, 800, 480), Color.White); spriteBatch.End(); } // Debug- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null); //800 480 spriteBatch.DrawString(spriteFont, "lightRadius: " + lightRadius + "\nlightCorrector: " + lightC + "\nligthZ: " + lightZ, new Vector2(10, 10), Color.LightYellow); spriteBatch.End();
// , struct Light { float3 position; float3 color; float invRadius; }; // texture normaltexture; // - 3- int numberOfLights; Light lights[3]; // float3 ambientColor; // , float screenWidth; float screenHeight; // Color- () sampler ColorMap : register(s0); // Normal- sampler NormalMap : samplerState { Texture = normaltexture; MinFilter = Linear; MagFilter = Linear; AddressU = Clamp; AddressV = Clamp; }; // float3 CalculateLight(Light light, float3 normal, float3 pixelPosition) { // float3 direction = light.position - pixelPosition; float atten = length(direction); direction /= atten; // float amount = max(dot(normal, direction), 0); atten *= light.invRadius; // , modifer , float modifer = max((1 - atten), 0); // return light.color * modifer * amount; } float4 DeferredNormalPS(float2 texCoords : TEXCOORD0) : COLOR { float4 base = tex2D(ColorMap, texCoords); // color- texCoords float3 normal = normalize(tex2D(NormalMap, texCoords) * 2.0f - 1.0f); // X,Y,Z - texCoords : -1, 1. // float3 pixelPosition = float3(screenWidth * texCoords.x, screenHeight * texCoords.y,0); // - float3 finalColor = 0; for (int i=0;i<numberOfLights;i++) { // finalColor += CalculateLight(lights[i], normal, pixelPosition); } // , * , multiply return float4((ambientColor + finalColor) * base.rgb, base.a); } technique Deferred { pass Pass0 { PixelShader = compile ps_2_0 DeferredNormalPS(); } }
Source: https://habr.com/ru/post/134819/
All Articles