XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

Render a 2D lightning shader in XNA

Last post 05-20-2008 11:00 PM by MrLeebo. 8 replies.
  • 05-10-2008 2:00 PM

    Render a 2D lightning shader in XNA

    Hi!
    I want to render this 2D lightning shader in my game, but I don't know how to do this.
    What code do I put between the effect.Begin() and effect.End()?
    Here's the shader code:

    #include "C:\XNA\Projects\2D\Game Engines\Softwork Engine\EngineGame\Content\Quad.fxh"

    float Script : STANDARDSGLOBAL <
      string UIWidget = "none";
      string ScriptClass = "scene";
      string ScriptOrder = "postprocess";
      string ScriptOutput = "color";
      string Script = "Technique=Technique?spot:bumpy;";
    > = 0.8; // version #

    float4 ClearColor : DIFFUSE <string UIName="Background";> = {0.5,0.5,0.5,1.0};
    float ClearDepth < string UIWidget = "none"; > = 1.0;

    //////////////////////////////////

    float3 SpotColor <
      string UIName = "Spotlight";
      string UIWidget = "Color";
    > = {1.0f, 0.95f, 0.85f};

    float3 Ambient <
      string UIName = "Ambient";
      string UIWidget = "Color";
    > = {0.05f, 0.05f, 0.1f};

    //////////

    float Angle <
      string UIWidget = "slider";
      string UIName = "Spin";
      float UIMin = 0.0;
      float UIMax = 360.0;
      float UIStep = 0.01;
    > = 220.0f;

    float Azimuth <
      string UIWidget = "slider";
      string UIName = "Azimuth";
      float UIMin = 0.0;
      float UIMax = 90.0;
      float UIStep = 0.01;
    > = 20.0f;

    float CenterX <
      string UIWidget = "slider";
      string UIName = "Center X";
      float UIMin = 0.0;
      float UIMax = 1.0;
      float UIStep = 0.01;
    > = 0.5f;

    float CenterY <
      string UIWidget = "slider";
      string UIName = "Center Y";
      float UIMin = 0.0;
      float UIMax = 1.0;
      float UIStep = 0.01;
    > = 0.5f;

    float Distance <
      string UIWidget = "slider";
      string UIName = "Distance";
      float UIMin = 0.1;
      float UIMax = 5.0;
      float UIStep = 0.01;
    > = 0.8f;

    float ConeAngle <
      string UIWidget = "slider";
      string UIName = "Cone Angle";
      float UIMin = 0.1;
      float UIMax = 90.0;
      float UIStep = 0.01;
    > = 28.0f;

    float Intensity <
      string UIWidget = "slider";
      string UIName = "Spot Intensity";
      float UIMin = 0.1;
      float UIMax = 10.0;
      float UIStep = 0.01;
    > = 0.6f;

    float Bump <
      string UIWidget = "slider";
      string UIName = "Bumpiness";
      float UIMin = 0.1;
      float UIMax = 10.0;
      float UIStep = 0.01;
    > = 3.0f;

    /// VM Globals /////////////////

    static float CosCone = cos(radians(ConeAngle));

    float3 spot_vector() {
      float zr = radians(Angle);
      float yr = radians(Azimuth);
      float3 dir = -sin(yr);
      float a = cos(yr);
      dir.x = a * cos(zr);
      dir.y = a * sin(zr);
      return normalize(dir);
    }

    static float3 SpotDir = -spot_vector();

    float3 spot_location() {
      float3 ctr = float3(CenterX,CenterY,0);
      return ctr - Distance*SpotDir;
    }

    static float3 SpotPos = spot_location();

    ///////////////////////////// Normal Map //////////////////

    texture normalTexture : NORMAL <
    string ResourceName = "default_bump_normal.dds";
    string ResourceType = "2D";
    >;

    sampler2D normalSampler = sampler_state {
      Texture = <normalTexture>;
      MinFilter = Linear;
      MagFilter = Linear;
      MipFilter = Linear;
      AddressU = Wrap;
      AddressV = Wrap;
    };
    ///////////////////////////// Render-to-Texture Data //////
    /*** Shaders ****************************/

    float4 spotPS(QuadVertexOutput IN) : COLOR
    {
    float3 delta = SpotPos - float3(IN.UV.xy,0);
    float fall = Intensity / dot(delta,delta);
      float3 dn = normalize(delta);
      float cone = -dot(dn,SpotDir);
    cone = max((float)0,((cone-CosCone)/(((float)1.0)-CosCone)));
      float4 lit = float4((fall * cone * SpotColor) + Ambient,1);
      return (lit);
    }

    float4 bumpspotPS(QuadVertexOutput IN) : COLOR
    {
    float3 delta = SpotPos - float3(IN.UV.xy,0);
    float fall = Intensity / dot(delta,delta);
      float3 dn = normalize(delta);
      float cone = -dot(dn,SpotDir);
    cone = max((float)0,((cone-CosCone)/(((float)1.0)-CosCone)));
    float2 b = tex2D(normalSampler,IN.UV).xy-float2(0.5,0.5);
    float3 N = float3((Bump*b),1);
    N = normalize(N);
    float diff = dot(N,SpotDir);
      float4 lit = float4((diff*fall * cone * SpotColor) + Ambient,1);
      return (lit);
    }
    /*** Technique **************************/

    technique spot <
      string Script =
          "RenderColorTarget0=;"
       "RenderDepthStencilTarget=;"
       "ClearSetColor=ClearColor;"
       "ClearSetDepth=ClearDepth;"
          "Clear=Color;"
          "Clear=Depth;"
          "ScriptExternal=color;"
      "Pass=p0;";
    > {
    pass p0 <
      string Script = "RenderColorTarget0=;"
                    "Draw=Buffer;";
    > {
        VertexShader = compile vs_1_1 ScreenQuadVS();
        ZEnable = false;
        ZWriteEnable = false;
        CullMode = None;
        AlphaBlendEnable = true;
        SrcBlend = DestColor;
        DestBlend = zero;
        PixelShader = compile ps_2_a spotPS();
      }
    }

    technique bumpy <
      string Script =
          "RenderColorTarget0=;"
       "RenderDepthStencilTarget=;"
       "ClearSetColor=ClearColor;"
       "ClearSetDepth=ClearDepth;"
          "Clear=Color;"
          "Clear=Depth;"
          "ScriptExternal=color;"
      "Pass=p0;";
    > {
    pass p0 <
      string Script = "RenderColorTarget0=;"
                    "Draw=Buffer;";
    > {
        VertexShader = compile vs_1_1 ScreenQuadVS();
        ZEnable = false;
        ZWriteEnable = false;
        CullMode = None;
        AlphaBlendEnable = true;
        SrcBlend = DestColor;
        DestBlend = zero;
        PixelShader = compile ps_2_a bumpspotPS();
      }
    }
  • 05-11-2008 11:32 AM In reply to

    Re: Render a 2D lightning shader in XNA

    I take it you didn't write the shader? I suggest asking the author how (s)he used it.
    Regards,
    Louis Ingenthron
    Fortis Venaliter
    Developer of Unsigned
  • 05-12-2008 11:08 AM In reply to

    Re: Render a 2D lightning shader in XNA

    No, it's from the FX Composer HLSL shader library.
    I just sent an email to one of the nvidia shader guys so I hope for an answer soon.

    Thanks anyway.

  • 05-15-2008 1:00 PM In reply to

    Re: Render a 2D lightning shader in XNA

    Well, I would be interested to know this myself, so if you get it, make sure you post how here! Im new to shaders and have never used one before myself, but I think I may have a use of them for some 2d stuff Im doing!
    Development site: CDX Games
  • 05-16-2008 8:23 AM In reply to

    Re: Render a 2D lightning shader in XNA

    Sure, I'll do that.
    I haven't got a response from him yet so I'm going to send another mail to him.
    Maybe you can do that too so he take this seriously and not just ignore it.

  • 05-16-2008 11:06 AM In reply to

    Re: Render a 2D lightning shader in XNA

    Try to remember that it is very likely that he is a busy person who might not get to respond (or do so quickly). Also remember that large companies like NVidia generally have a review process for releasing code of any nature. So even if he wanted to give you a new shader like that, it's very possible he contractually cannot do so.

    I would say the worst thing you could do is flood him with emails. I know that I generally ignore people who do that to me. Just give it time and if you don't hear anything back, move on to either finding another shader or learning HLSL and making this work yourself.
  • 05-16-2008 3:39 PM In reply to

    Re: Render a 2D lightning shader in XNA

    Okay, thanks for the tip.
    Is there any good HLSL tutorials for this sort of shaders I can read from?
    I really are in need for a shader like this so I want to learn the stuff I need to learn as quickly as possible.

  • 05-16-2008 3:43 PM In reply to

    Re: Render a 2D lightning shader in XNA

    simple ones are a big help.. showing how to render my screen to a rendertarget, or how that works, then apply 1 (one) shader effect and draw it.

    Most of the samples cover 3-4 different things at one time which is good, but harder for the true newb to learn. Although I do really wanna learn, as I think I have a use for a shader right now.. heh

    Development site: CDX Games
  • 05-20-2008 11:00 PM In reply to