A simple solution I found for creating a caustic effect on objects is by first creating a wave function like this one.
float wave(float x,float y,float timer)
{float z = 0.0f;
float octaves = 5;
float factor = 1.0f;
float d = sqrt(x*x+y*y);
z -= factor* cos(timer*0.001*speed+(1.0f/factor)*x*y*wavesize) *
0.3*sin(timer*0.001*speed+(1.0f/factor)*x*y*wavesize) * 0.8*sin(timer*0.001*speed+ (1.0f/factor)*x*y*wavesize);
factor = factor/2;
octaves--;
} while (octaves > 0);
return 2*amplitude*d*z;
}This function will update be used to change the normal of triangles made up by the vertices in your scene. The triangle normal will be multiplied by the world normal, and then the world normal will be used to adjust the reflection and refraction vectors These values will be used to ultimately change the diffuse color of the object, and the specular intensity. . Here’s an example of how I did it.
float4 worldPosition = mul(float4(position, 1.0), world);
float3 reflectionVector = normalize(reflect(-lightDirection , worldNormal));
float3 directionToCamera = normalize(cameraPosition - worldPosition);float3 refractVector = normalize(refract(-lightDirection, reflectionVector, 0.333));
float diffuseIntensity = saturate( dot(-lightDirection, worldNormal));
float4 diffuseColor = lightColor * diffuseIntensity;float4 specularIntensity = specularColor * pow( saturate(dot(refractVector, directionToCamera)), 1.0);
No comments:
Post a Comment