Hi I’m trying to create a shader that will map between two textures on the normal angle to the light (it’s a day/night Earth scene). It’s been a while since I wrote a shader and that was for Unity and it had a macro to move everything to tangent space so I could do the bump mapping. I guess I’m a bit at sea as to how to do this without those macros I was used to.
Don’t suppose anyone has some template shaders I could look at or couple post an example of a bump mapping vertex/fragment shader that works in PlayCanvas?
So using standard material, there is a shader chunks system, there you can override just a small part of shader implementation. That allows you to benefit from existing whole system, and make some custom differences.
var Earth = pc.createScript('earth');
// initialize code called once per entity
Earth.prototype.initialize = function() {
var material = this.entity.model.meshInstances[0].material;
material.chunks.emissiveTexPS =
"uniform sampler2D texture_emissiveMap;\n\n" +
"vec3 getEmission() {\n" +
" float f = clamp(dot(dNormalW, light0_direction), 0.0, 1.0);\n" +
" return $texture2DSAMPLE(texture_emissiveMap, $UV).$CH * f;\n" +
"}\n\n";
material.update();
};
I’m dotting the world space normal of the fragment against the world space direction of the light and clamping between zero and one. That scales the ‘night time’ map that I’ve set on the emissive map slot of the material for the Earth.
@max and I just updated the project to include a sun ‘billboard’ and a new shader chunk which tries to simulate sunlight passing through the atmosphere. Check it out. It’s fairly sexy.