Normal mapping particles - only shows on half

In my spare time I’ve been messing around trying to get normal maps on particles, but every single time it seems like only one triangle is receiving normals:

What am I doing wrong?

Shader "Particle Test" {
        Properties {
                _MainColor ("Base Color", Color) = (0.5,0.5,0.5,1)
                _MainTex ("Base (RGB)", 2D) = "white" {}
                _BumpMap ("Base (RGB)", 2D) = "bump" {}
              
        }
        SubShader {
                Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
                LOD 200
              
                CGPROGRAM            
                #pragma exclude_renderers gles
                #pragma surface surf pLight alpha
                #pragma vertex vert
              
                half4 LightingpLight (SurfaceOutput s, half3 lightDir, half atten)
                {
                  half dotL = dot (s.Normal, lightDir);
                  half diff = dotL;
                  half4 c;
                  c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);               
               
                  c.a = 1;
                 
                  return c;
                    }
              
                fixed4 _MainColor;
                sampler2D _MainTex;
                sampler2D _BumpMap;
             
                struct Input
                {
                        float2 uv_MainTex;
                        float3 viewDir;
                        float4 color : COLOR;
                };
              
                void vert (inout appdata_full v, out Input o)
                {
                    UNITY_INITIALIZE_OUTPUT(Input,o);
                    o.color = v.color;
                  
                                  
                }
              
                void surf (Input IN, inout SurfaceOutput o)
                {                      
                        half4 c = tex2D (_MainTex, IN.uv_MainTex) * _MainColor * IN.color;
                        o.Albedo = c.rgb;
                          o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));                         
                        o.Emission = c.rgb;
                }
                ENDCG
        }
        FallBack "Diffuse"
}

I noticed this recently on a few effects - it appeared to be related to the “normal direction” parameter in the particle system being < 1 (eg, bent normals on the billboard)
I assume it’s a mesh generation bug with shuriken, most likely the tangents are getting borked on one of the tri’s (at least it looks very similar to when I last screwed up tangents on a particle :smile: )

Did you ever find a solution? im having trouble setting up particle normal mapping myself

Particles don’t have valid tangents so your basic normal mapping doesn’t work on them. If your particles never turn you can use a camera transform to set the tangents to something useful, or you can look into generating the tangents in the pixel shader using derivatives.

Did bgolus’ reply about tangents finally get it working for you? Would you mind sharing how you calculated them?

Here’s what I use. You use it in the fragment shader. The position you pass to it should be in the same space you have your light directions, which Unity passes to shaders in world positions / directions, so you’ll need to calculate the world position in the vertex shader and pass it to the fragment shader. (Note, particle vertices by default are already in world space positions as of Unity 5.2).

// Unity version of http://www.thetenthplanet.de/archives/1180
float3x3 cotangent_frame( float3 normal, float3 position, float2 uv )
{
    // get edge vectors of the pixel triangle
    float3 dp1 = ddx( position );
    float3 dp2 = ddy( position ) * _ProjectionParams.x;
    float2 duv1 = ddx( uv );
    float2 duv2 = ddy( uv ) * _ProjectionParams.x;
    // solve the linear system
    float3 dp2perp = cross( dp2, normal );
    float3 dp1perp = cross( normal, dp1 );
    float3 T = dp2perp * duv1.x + dp1perp * duv2.x;
    float3 B = dp2perp * duv1.y + dp1perp * duv2.y;
    // construct a scale-invariant frame
    float invmax = rsqrt( max( dot(T,T), dot(B,B) ) );
    return transpose(float3x3( T * invmax, B * invmax, normal ));
}

// Variant with no input normal, construct purely from geometry gradients
float3x3 cotangent_frame( float3 position, float2 uv )
{
    // get edge vectors of the pixel triangle
    float3 dp1 = ddx( position );
    float3 dp2 = ddy( position ) * _ProjectionParams.x;

    float3 normal = normalize(cross(dp1, dp2));

    float2 duv1 = ddx( uv );
    float2 duv2 = ddy( uv ) * _ProjectionParams.x;
    // solve the linear system
    float3 dp2perp = cross( dp2, normal );
    float3 dp1perp = cross( normal, dp1 );
    float3 T = dp2perp * duv1.x + dp1perp * duv2.x;
    float3 B = dp2perp * duv1.y + dp1perp * duv2.y;
    // construct a scale-invariant frame
    float invmax = rsqrt( max( dot(T,T), dot(B,B) ) );
    return transpose(float3x3( T * invmax, B * invmax, normal ));
}

edit: Note that the transpose in these functions aren’t usually something you want to do, but exists for simplicity of use because most people expect to do mul(matrix, normal), but if you remove the transpose you can do mul(normal, matrix) and get some perf back.

1 Like

Really appreciate that bgolus, I’ll give this a shot :)!