Specular Animation shader (HLSL/GLSL)

Started by
5 comments, last by Koehler 11 years, 9 months ago
Hello,

I am trying to make a Shader that simulates the specular animation on a surface(e.g shield) and was wondering if someone could help me out.

Please see the attachment for a better visualization. I would like the Specular section to move slightly from left to right and repeats again after a few seconds.

Thank you again in advance.

H
Advertisement
Generally specular reflection would behave in the manner you described by animating the mesh, or the light, or the camera (eye direction). For example the specular highlight would move from the left to the right of the object if you rotate it say in the range -15 to +15 degrees around its up axis. Do you have a specular shader in place? If not some nice tips/examples include:

An intro to shader lighting is here:
http://www.arcsynthesis.org/gltut/Illumination/Tutorial%2011.html
HLSL:
http://www.rastertek.com/dx10tut10.html
http://rbwhitaker.wikidot.com/specular-lighting-shader
http://takinginitiative.net/2010/08/30/directx-10-tutorial-8-lighting-theory-and-hlsl/
GLSL:
http://www.lighthouse3d.com/tutorials/glsl-tutorial/lighting/
http://www.ozone3d.net/tutorials/glsl_lighting_phong.php
Hi Daniel,

Thank you for your respond. Yes, you are right.However, I am not doing 3D and also I don't think any of the standard specular effects (Blinn,Phog, etc...) can give me the results I want.

My scene doesn't have a light and I need this shader to work as a post processing effect.

Here is how I think is possible to achieve this using 3 textures:

1-Texture1(Specular HighLight)
2-Texture2(Diffuse Color)
3-Texture3(Alpha Map)

Then we can pan the Specular highlight from right to left or what not. Please see the attachment for the screenshots.

I am new to HLSL and would appreciate it if someone could post a pseudo code or suggest a way to achieve this without doing any multitexturing.

Thanks,

H
You can just do a simple multiply-add; it works out to ( diffuse + ( color * alpha ) ). Animating things isn't too difficult either, just add some goofed-with time-based offset to the X coordinate of the specular highlight texture.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
What's the problem with "multitexturing" ? Just scroll the UVs for the 'specular' texture and draw it
I would probably just try an animated image. Like a .gif. Or just use a series of images to do the animation. Personally, I think that would be the most simple method of doing that. I'm sure it has many downsides but if you have a deadline and you can't find a better solution, this could be a fallback idea.
This can be accomplished very simply. Just pass in a float uniform (Let's call it "Offset") that you increase by a little bit each frame.

With Multi-Texturing
When you call Tex2D to sample a texture, you pass it a 2D vector of texture coordinates (U,V). All you have to do is add Offset to the U component of your texture coordinates when sampling the specular texture, add that result to the color sampled from your color texture (Without "offset" added to the coordinates), and multiply the sum of those two values by the value sampled from your alpha texture (Again at the original texture coordinates, To keep everything shield-shaped).

Without Multi-Texturing
You could just use one texture, with RGB and alpha of your shield in it (say that one on the left in your Photoshop window). We know that your texture coordinates are probably a value from 0 to 1. This means that multiplying the U value of your texture coordinate by pi, or 3.14, and taking the sine of the resulting value will yield a nice smooth value ranging from 0 to 1 and back to 0 across the middle of the shield.

To make the effect "move" across the shield, we just have to add the offset to the texture coordinate before multiplying by pi. Now the peak of the sine curve "moves". Multiplying this value by a color vector, then by the original texture's alpha, and adding the result to your output color yields the moving glare effect you're looking for.

Since we're only increasing Offset, we should clamp the value of the sine to [0,1] to make sure we don't add corresponding dark spots (since the sine of pi to 2*pi is negative). Finally, we can multiply the resulting value by some scale amount (<1 to make it fall off sharply, > 1 to make it wider).



//Pass in the offset and color you want the "glare" effect to be...
Uniform float Offset;
Uniform float3 GlareRGB;

//pass in a value for how sharply you want the glare to fall off. 2 = twice as fast as a normal sine curve

Uniform float Glare_Sharpness;

//get our color and alpha from the shield texture
float4 color = Tex2D(colorSampler, texCoords.uv);

//Calculate how bright the glare is at this pixel
float glare_amount =(1/Glare_Sharpness)*clamp(sin(3.14*(texCoords.u + Offset)),0.0,1.0);

float4 OUT_COLOR = float4(color.rgb + glare_amount*GlareRGB), color.a);

This topic is closed to new replies.

Advertisement