Jump to content

Color + texture


Gugis
 Share

Recommended Posts

Hi.

I want to apply diffuse texture and color for mesh. Texture have transparent areas, so i want to use diffuse color in that areas on a mesh, elsewhere - apply texture. What is the best/easiest way to do it?

Thanks

Link to comment
Share on other sites

Link to comment
Share on other sites

Nailed it.

 

Materials/babylon.standardMaterial.js

Line 296:

this._baseColor.copyFromFloats(1, 1, 1);

Change to:

this._baseColor = this.diffuseColor;

Shaders/default.fragment.fx

Line 416:

baseColor = texture2D(diffuseSampler, vDiffuseUV);

Change to:

baseColor = (vDiffuseColor * (1.0 - texture2D(diffuseSampler, vDiffuseUV).a)) + (texture2D(diffuseSampler, vDiffuseUV) * texture2D(diffuseSampler, vDiffuseUV).a);

Also you can add new property to control material's behaviour. Let's call it mixed

 

Materials/babylon.standardMaterial.js

After line 25:

this.emissiveColor = new BABYLON.Color3(0, 0, 0);

Add new line:

this.mixed = false;

After block (lines) 66 - 72:

if (this.diffuseTexture && BABYLON.ExtendedMaterial.DiffuseTextureEnabled) {    if (!this.diffuseTexture.isReady()) {        return false;    } else {        defines.push("#define DIFFUSE");    }}

Add new block:

if(this.mixed){    defines.push("#define MIXED");}

Now line 301:

this._baseColor = this.diffuseColor;

Change to this:

if(this.mixed)    this._baseColor = this.diffuseColor;else    this._baseColor.copyFromFloats(1, 1, 1);

Lastly after line 529:

newExtendedMaterial.specularPower = this.specularPower;

Add new line:

newExtendedMaterial.mixed = this.mixed;

Shaders/default.fragment.fx

In fragment shader file change line 416 to:

#ifdef MIXED	baseColor = (vDiffuseColor * (1.0 - texture2D(diffuseSampler, vDiffuseUV).a)) + (texture2D(diffuseSampler, vDiffuseUV) * texture2D(diffuseSampler, vDiffuseUV).a);#else	baseColor = texture2D(diffuseSampler, vDiffuseUV);#endif 

Now you can use both color and texture:

var material = new BABYLON.StandardMaterial("male", game.scene);material.diffuseTexture = new BABYLON.Texture("/game/textures/male.png", game.scene);material.diffuseColor = new BABYLON.Color3(0.88,0.72,0.6);material.mixed = true; // false if you want to have old behaviour
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...