Custom vertex attributes in chunk

We have a nice chunk system which allows us to create shaders, override some parts and etc.
But it could be even more powerful with custom attributes.

Currently, we have custom vertex attribute semantics such a pc.SEMANTIC_ATTR0-15, but we can’t set in into a shader’s definition for “standard” for instance.

We also have pc.shaderChunks.collectAttribs which can detect custom attributes in code and add it before compiling process.

What do you think? If it’s okay for workflow, I would like to try to implement and make PR.

It’s pretty easy to achieve:

attributes = pc.extend(attributes, pc.shaderChunks.collectAttribs(vshader));
        
   return {
       attributes: attributes,
       vshader: vshader,
       fshader: fshader,
       tag: pc.SHADERTAG_MATERIAL
   };

Over there https://github.com/playcanvas/engine/blob/master/src/graphics/program-lib/standard.js#L1307

Coming from Unity I was creating a whole new shader every time (or a set of complex shaders composed of multiple included files to reuse my code).

Reading the documentation of PlayCanvas I didn’t find any explanation about this chunk system and how to customize the standard shader (It’s completely missing a “Shaders” chapter).
I found just a tutorial with a plasma shader.
The example is easy to understand but it would be better to read an introduction to this feature and to have reference API of all the chunks. (I know… I can look at the source code… but you know, a reference API is there to avoid digging into the code every time :wink: )

And to answer to your post, yes, would be nice to allow the customization of the vertices definition.

1 Like

Thanks for attention.
Chunk system is really cool tool and I’ll post a nice example of using that.

I would be really appreciate @vaios for reply this topic, please.

2 Likes

Here is a tutorial on how to use the shader chunk system:

3 Likes

That’s nice, I guess @mcmorry could be happy to take a look.

But still, it doesn’t allow to provide custom attributes.

Indeed it would be a nice addition. Could you specify some example use cases?

Sure.

I have an effect which adds one more component to vertex semantic. I set this to UV coords of screen space.

Then I patch getAlbedo method in FS.
I use this coords to sample2D from current render buffer.

 this.material.chunks.startVS = [
        "attribute vec2 vertex_screenCoords; ",
        "varying vec2 vVertex_screenCoords; ",
        "void main(void) { ",
        "   gl_Position = getPosition();",
        "   vVertex_screenCoords = vertex_screenCoords;",
    ].join("\n");
    
    this.material.chunks.diffusePS = [
        "#ifdef MAPCOLOR",
        "uniform vec3 material_diffuse;",
        "#endif",
        "#ifdef MAPTEXTURE",
        "uniform sampler2D texture_diffuseMap;",
        "varying vec2 vVertex_screenCoords;",
        "uniform sampler2D texture_refractMap;",
        "uniform sampler2D texture_screenMap;",
        "uniform float uInvisiblePower;",
        "#endif",
        "void getAlbedo() {",
        "    dAlbedo = vec3(1.0);",
        "    #ifdef MAPCOLOR",
        "        dAlbedo *= material_diffuse.rgb;",
        "    #endif",
        "    #ifdef MAPTEXTURE",
        "   float refractHeight = texture2D(texture_refractMap, vUv0).r;",
        "   if (refractHeight > uInvisiblePower) ",
        "        dAlbedo *= texture2DSRGB(texture_diffuseMap, vUv0).rgb;",
        "   else",
        "        dAlbedo *= texture2DSRGB(texture_screenMap, vVertex_screenCoords).rgb;",
        "    #endif",
        "    #ifdef MAPVERTEX",
        "        dAlbedo *= gammaCorrectInput(saturate(vVertexColor.$VC));",
        "    #endif",
        "}"
    ].join("\n");
    

So I have a nice effect of invisibility.

I had to patch standard program in order to set custom attributes

this.app.graphicsDevice.programLib.unregister("standard")
this.app.graphicsDevice.programLib.register("standard", pc.programlib.standardFixed);

Agreed, that is a good example of how to use them with the chunks system. It is very cool as you say, let’s expand it.

1 Like

@vaios please take a look at this.

I think @Mr_F is the person to ask about this I don’t know much about the chunks system I’m afraid… Or maybe @will ?

1 Like