Glittering Effect in Unity

1.4k Views Asked by At

i'm trying to add a glittering effect to a texture on a pine tree. I tried to use the code from here http://developer.amd.com/wordpress/media/2012/10/Shopf-Procedural.pdf (P.22) as suggested in other threads. But i'm not able to apply it to my shader.

float glitter (float3 pos, float3 viewVec){
        float3 fp = frac(0.7 * pos + 9 * snoise3D( pos * 0.04).r + 0.1 * viewVec);

        fp *= (1 - fp);
        float glitter = saturate(1 - 7 * (fp.x + fp.y + fp.z));

        return glitter;
    }



    void surf (Input IN, inout SurfaceOutputStandard o) {

        float glitterValue = (glitter(IN.worldPos, IN.viewDir));
        glitterValue = clamp(0.0f, 1.0f, glitterValue);


        float2 uv_polar = cartesianToPolarCord(IN.uv_MainTex);

        float factor = pow(uv_polar.x * 1.1f, 5.0f);
        factor = clamp(0.0f, 1.0f, factor);
        factor = lerp(factor, factor * (snoise2D(IN.uv_MainTex * 5.0f) * 0.5f + 0.5f), 0.3);

        fixed4 c = lerp(_TreeColor, _SnowColor, factor);

        //c = glitterValue;

        o.Albedo = c.rgb;

        o.Metallic = _Metallic;
        o.Smoothness = _Glossiness;
        o.Alpha = c.a;
    }
    ENDCG

The tree originally looks like this:

pine tree without glittering effect

Now i expect the result from my glitter function to create many little white sparks in the texture. So if i only use the values from the glitter function (uncomment the c = glitterValue column) as color, the tree should become black with many little white sparks.

But it looks more like this instead:

pine tree with glittering effect

Am i getting false values from my glitter function or am i just applying them wrong?

1

There are 1 best solutions below

0
On

Ok I figured it out. The problem is the scale of my pine tree forest. It's a very small scale and the glitter values usually apply to a bigger scale. This means that the big white spots I get are in fact the sparkles i want but in a bigger scale.

I fixed it by adding a factor to the worldPos and viewDir

float glitterValue = (glitter(500*IN.worldPos, 500*IN.viewDir));

You can already see an effect with factors like 2,5,10 or 100. So if you got a similar problem, try different values.