Sps with material plugin

repro: https://playground.babylonjs.com/#22HT5Z#108

I want to be able to set the uniform variable of the material of each sphere under certain conditions. In the PG above, it would be this.custom_color but it could literally be anything. I don’t see any way to access this in the solid particle api. Can I do it at shader level? If so, which shader includes files should I be using/including in the vertex/fragment?

I took a quick look at particles.vertex.fx and particles.fragment.fx. But they don’t seem to fit my needs…

A SPS is drawn with a single draw call, so all elements from the SPS are drawn with the same material. What you can do is to create a vertex buffer that gives you the instance id in the shader:

Oh, I see. That would be tedious for each particle…hmm… What if I wish to animate the variable? In the sample PG, it would be animating this.custom_color per particle. Would exposing the material in initParticles and updateParticles be a better long-term solution?

For example

function recycleParticle(p){
    p.position.copyFromFloats(0,0,0);
    p.position.x = BABYLON.Scalar.RandomRange(-1, 1);
    p.position.z = BABYLON.Scalar.RandomRange(-1, 1);
    if (p.idx%2===1){
        // red
        p.material.blackAndWhite.custom_color = new BABYLON.Color4(1, 0, 0);
    } else {
        // blue
       p.material.blackAndWhite.custom_color = new BABYLON.Color4(0, 0, 1);        
    }
}

Then

SPS.updateParticle = (p) => {
        p.position.y+=BABYLON.Scalar.RandomRange(0.0, 0.01);
        if (p.position.y>2){
            recycleParticle(p);
        } else {
            // color animation 
            p.material.blackAndWhite.custom_color.g += 0.1;
        }
    }

Thoughts?

nvm, I just eyeballed the src. Appears that the sps mesh is a custom mesh with all properties encased in vertexBuffers. A material plugin with N params would require N vertexBuffers for said mesh. Even if a property was exposed to SolidParticle to facilitate material configuration at runtime, it would still be a nightmare rework.

I’m marking previous reply as solution. Thanks for all the help, cheers ! :slight_smile:

1 Like

SPS with material plugin plus vertexBuffer hack. If only it could have been officially supported…

2 Likes