Declaration variables in shaders

Hello. Where to read about declaring uniform variables. I can’t figure out how to determine their value in the game scripts. For example, I need the screen resolution, I write- " uniform int resolution;", but how to pass the value inside the shader?

Hi @sergey_ch ,

You use the following method to pass a uniform value to a shader:

myMaterial.setParameter('myUniformValue', 10.0);

Check the following tutorials which include several examples of that:

https://developer.playcanvas.com/en/tutorials/?tags=shaders

2 Likes

I wrote a test shader, it worked for the entire scene. But when I apply it to the material, an error appears - “Cannot read property ‘split’ of undefined”

here is the code, what am I doing wrong?

void main(void)
{
 vec3 color = vec3(0.11, 0.34, 0.21);
 gl_FragColor = vec4(color,1.0);
}
Ball.prototype.initialize = function() {
    this.material = new pc.Material();
    var fragmentShader = "precision " + this.app.graphicsDevice.precision + " float;\n";
    fragmentShader = fragmentShader +  this.shader.resource.resource;
    var shaderDefinition = {
        attributes: {
            aUv0: pc.SEMANTIC_TEXCOORD0
        },
       fshader: fragmentShader

    };
    this.sh = new pc.Shader(this.app.graphicsDevice, shaderDefinition)
    this.material.shader = this.sh
    this.entity.model.model.meshInstances[0].material = this.material
};

I don’t see a method split in your code, in what JS script exactly and what line did the error occure?

this error in playcanvas

playcanvas-stable.dbg.js:15776 Uncaught TypeError: Cannot read property 'split' of undefined
    at GraphicsDevice._addLineNumbers (playcanvas-stable.dbg.js:15776)
    at GraphicsDevice.postLink (playcanvas-stable.dbg.js:15798)
    at GraphicsDevice.setShader (playcanvas-stable.dbg.js:15856)
    at ShadowRenderer.submitCasters (playcanvas-stable.dbg.js:22655)
    at ShadowRenderer.render (playcanvas-stable.dbg.js:22696)
    at ForwardRenderer.renderShadows (playcanvas-stable.dbg.js:23694)
    at ForwardRenderer.renderComposition (playcanvas-stable.dbg.js:24668)
    at Application.render (playcanvas-stable.dbg.js:69800)
    at playcanvas-stable.dbg.js:70496

I am not sure if this is the cause of the error, but if you are writing a custom shader (instead of overriding shader chunks), then you need to provide both a vertex and fragment parts in your shader.

Otherwise it can’t compile to something useful. Check the following example:

https://developer.playcanvas.com/en/tutorials/custom-shaders/

There are more examples here as well, for example
http://playcanvas.github.io/#/graphics/shader-burn
http://playcanvas.github.io/#/graphics/point-cloud

2 Likes

I fixed the crash when a shader is not specified here (to be released in a week or so)

3 Likes