Jump to content

Chaining or Merging Shaders


Flake
 Share

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!

Link to comment
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 :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...