I am trying to create a glittering effect for my realtime shader but I don't know how.
What technique can I use to implement this?
Well, when asked to design any shader, we should start by breaking things down to smaller problems. And just as note the glittering effect doesn't actually makes the shader looks good, but the overall lighting and effect, using only one of them won't look as good.
First of all let us state what is not directly part of the shader:
Secondly, let's break the actual shader into separate effects:
Note that such fabrics have infinitely many normals, but the technique described here approximates the most significant normal
In order to approximate the most significant normal, one way to do, is to use texture coordinates and calculate tangents of the mesh, and instead of calculating N.L you calculate 1-(N.T).
Full explanation here. And you probably need to implement that in fragment shader rather than the vertex technique they talk about. Other anisotropic models can also apply.
Now for the glittering effect:
This can be done in world space/local texture space, or screen space as a separate pass.
The algorithm I can think off, uses image processing technique (assuming the mesh have texture coords).
Note the above image is just an example of the max filter, and applying it on the noise will give sth similar to a star field.
Example of Gausian filter applied on max(ed) noise.
Note, this technique might need significant optimzation for a real time shader.
There's an interesting article AMD - Getting Procedural.
It seems that sparkles are harder than I think.
Decent solution: Use 3D position to index 3D noise function, add the view vector, use frac function to further randomize things.
Sparkle: float specBase = saturate(dot(reflect(-normalize(viewVec), normal), lightDir)); // Perturb a grid pattern with some noise and with the view-vector // to let the glittering change with view. float3 fp = frac(0.7 * pos + 9 * Noise3D( pos * 0.04).r + 0.1 * viewVec); fp *= (1 - fp); float glitter = saturate(1 - 7 * (fp.x + fp.y + fp.z)); float sparkle = glitter * pow(specBase, 1.5);