☑ Creating a Bump Shader

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.

That perhaps could work for you?

Oh yeah that sounds better. I’m right now staring at a gl article about to start banging my head on the wall…

Writing whole thing from the scratch - will be a hard task here.

Check out on forum, there were many mentions of shaderChunk system.

How’s this:

https://playcanvas.com/project/419706/overview/planet-earth

The important bit is this script:

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.

Nice thanks! I’ve been fiddling with shader chunks and came up with something different, but not as good lol.

@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. :slight_smile:

2 Likes

Nice Will! Love the shader!