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

Make faces face the camera

Last post 9/15/2009 4:39 PM by Roonda. 1 replies.
  • 9/14/2009 7:10 PM

    Make faces face the camera

    This shader:

    1 float3 LightDiffuse(float3 vNormal,float3 vLightDir,float3 vLightColor,float3 vDiffuseMaterial) 
    2
    3     return vDiffuseMaterial * vLightColor * max(dot(vNormal, vLightDir), 0.0f); 
    4
    5 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    6 // Structs 
    7  
    8 struct PS_INPUT_FLORB 
    9
    10    float4 vColor                : COLOR0; 
    11    float2 Tex0                  : TEXCOORD0; 
    12    float4 SunLight              : TEXCOORD1; 
    13    float4 ShadowTexCoord            : TEXCOORD2; 
    14    float2 TexelPos              : TEXCOORD3; 
    15 }; 
    16 struct VS_OUTPUT_FLORB 
    17
    18    float4 vPosition             : POSITION; 
    19    float4 vColor                : COLOR; 
    20    float3 vNormal               : TEXCOORD4; 
    21    float2 Tex0                  : TEXCOORD0; 
    22    float4 SunLight              : TEXCOORD1; 
    23    float4 ShadowTexCoord            : TEXCOORD2; 
    24    float2 TexelPos              : TEXCOORD3; 
    25    float  Fog                   : FOG; 
    26 }; 
    27 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    28 // Vertex Shader, not too much has changed here. 
    29  
    30 VS_OUTPUT_FLORB vs_florb(uniform const int PcfMode,float4 vPosition : POSITION,float2 tc : TEXCOORD0,float4 vColor : COLOR, float3 vNormal : NORMAL) 
    31
    32    VS_OUTPUT_FLORB Out = (VS_OUTPUT_FLORB)0; 
    33    Out.vPosition = mul(matWorldViewProj, vPosition); 
    34    float4 vWorldPos = (float4)mul(matWorld,vPosition); 
    35    float3 P = mul(matWorldView,vPosition); 
    36    Out.Tex0 = tc; 
    37    vColor.rgb = LightDiffuse(vNormal.xyz, vSunDir.rgb, vSunColor.rgb, vColor.rgb); // New 
    38    Out.vColor = vColor * (vAmbientColor + vSunColor * 0.06f); 
    39    Out.SunLight = (vSunColor * 0.34f)* vMaterialColor * Out.vColor; 
    40     if (PcfMode != PCF_NONE) 
    41     { 
    42         float4 ShadowPos = mul(matSunViewProj, vWorldPos); 
    43         Out.ShadowTexCoord = ShadowPos; 
    44         Out.ShadowTexCoord.z /= ShadowPos.w; 
    45         Out.ShadowTexCoord.w = 1.0f; 
    46         Out.TexelPos = Out.ShadowTexCoord * fShadowMapSize; 
    47     } 
    48    float d = length(P); 
    49    Out.Fog = get_fog_amount(d); 
    50    return Out; 
    51
    52 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    53 // Pixel Shaders 
    54  
    55 PS_OUTPUT ps_florb(PS_INPUT_FLORB In, uniform const int PcfMode)  
    56 {  
    57     PS_OUTPUT Output; 
    58     float4 tex_col = tex2D(MeshTextureSampler, In.Tex0); 
    59     clip(tex_col.a - 0.05f); 
    60     tex_col.rgb = pow(tex_col.rgb, input_gamma); 
    61     if (PcfMode != PCF_NONE) 
    62     { 
    63         float sun_amount = GetSunAmount(PcfMode, In.ShadowTexCoord, In.TexelPos); 
    64         Output.RGBColor =  tex_col * ((In.vColor + In.SunLight * sun_amount)); 
    65     } 
    66     else 
    67     { 
    68         Output.RGBColor =  tex_col * ((In.vColor + In.SunLight)); 
    69     } 
    70     Output.RGBColor.rgb = pow(Output.RGBColor.rgb, output_gamma_inv); 
    71     return Output; 
    72

    Which uses this technique:
    1 technique florb_SHDWNVIDIA 
    2
    3     pass P0 
    4     { 
    5     AlphaRef = 0x0f; // Minimizes pixel flickering when moving the camera 
    6     VertexShader = compile vs_2_a vs_florb(PCF_NVIDIA); 
    7     CullMode = none; // Use CW, CCW or none here. 
    8     PixelShader = compile ps_2_a ps_florb(PCF_NVIDIA); 
    9     } 
    10

    Works quite well on tree leaves (a diagonal three foil), but I allways need to use CullMode = none to be able to view the backface. Which sucks performance-wise. So I'd like to ask here: How can I make quads (instead of the 'three foil') keep facing the camera so I don't have to double faces or use CullMode = none?
  • 9/15/2009 4:39 PM In reply to

    Re: Make faces face the camera

    Sounds like you're talking about billboards (requires CPU work to generate the world matrix to face the camera). To get the performance acceptable, you'd probably want to combine that with hardware instancing to avoid a single draw call per leaf. Alternatively, point sprites might suffice.
Page 1 of 1 (2 items) Previous Next