Sign in to follow this  
Flake

Chaining or Merging Shaders

Recommended Posts

Shaders are still new to me, so this might be a silly question:

Is it possible to chain, or merge shaders?

I've got a PBRMaterial which uses the ColorCurves, and a custom shader that generates a texture. I'd love to be able to be able to combine or chain them, so I can have both effects.

My initial thought was: 'grab the PBRMaterial and Shader from github, and hack in the properties/functions from my custom shader'. But those files have ~2000 lines of code in them, so if anyone knows an easier approach I'd be grateful. 

Pseudo code, because one can never be too clear :)

What I've got:

...
var mesh = new Mesh();

var materialA = new BABYLON.PBRMaterial("pbrMat", scene);
var curve = new BABYLON.ColorCurves();
curve.GlobalHue = 250; 
materialA.cameraColorCurves = curve;


var materialB = new BABYLON.ShaderMaterial("otherMat", scene, "./assets/shaders/otherMat", 
  {
    attributes: ["position", "uv"],
    uniforms: ["worldViewProjection", "attribute"]
  });

materialB.setFloat("attribute", someVar);


mesh.material = materialA; // or materialB.. but not both
...

What I'd want:

var mesh = new Mesh();

var materialA = new BABYLON.PBRMaterial("pbrMat", scene);
var materialB = new BABYLON.ShaderMaterial("otherMath", scene, "./assets/shaders/specialMat");


mesh.material.Add([materialA, materialB]);

// or 

mesh.material = materialA.chain(materialB);

Thanks for your time!

Share this post


Link to post
Share on other sites

Thanks for the reply guys.

13 hours ago, Luaacro said:

Do you mean that the two materials may be mixed ? That means the final color of each pixel may be a mix between the ouput colors of the PBR material and the Other material

Not a direct mix; rather I want to run shaderA and feed the output into shaderB, and render the result of that.

11 hours ago, NasimiAsl said:

You must make a one shader if you haven't any conflict in variable and uniforms . you can draw it with renderTarget too and mix it with postprocess.

I have to take a closer look at your code, but the helperMaterial" looks promising!

I'll post an update, once I've gone through the code.. Also the sphere in the third example looks amazing!

Edit 1:

I could not get the  BABYLONX.ShaderBuilder to behave how I wanted.

// avrage m1 and m2
.InLine('result = vec4( m2.xyz*0.5 +m1.xyz*0.5  ,0.0);') // when the alpha was zero, the entire mesh was talk transparent and ghostlike

.InLine('result = vec4( m2.xyz*0.5 +m1.xyz*0.5  ,q.0);') // and when the alpha was one, the mesh was fine, but the entire scene was completely back (the alpha component of the clear colour was ignored)

Any help is still appreciated, i'll get back to trying around :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.