Jump to content

Changing model texture in runtime


royibernthal
 Share

Recommended Posts

I'm changing the texture of a model in runtime by creating a material with diffuseTexture and setting it as the material of all meshes related to the model.

I have 2 questions about that:

 

1) mesh.material is null before I set it to the material I created (even when I test it seconds after initialization), and the model's texture is still rendered. It leads me to believe the mode's texture/material is set somewhere else, and mesh.material simply overrides the original place.

What is the parameter that contains the texture/material of the mesh? I wasn't able to find it when I went over a mesh's properties in the dev tools.

 

2) The material with diffuseTexture I create in runtime is "shiny" (reflects light more I guess?).

 

The puppy on the right is the result of the original material before my intervention, as specified in the .babylon file - this is the result I'd like to achieve.

The puppy on the left is the result after I set the material of all meshes to the one I create in runtime - the "shiny" one.

puppy.png

Here's some relevant info from the .babylon file:

"materials":[{"name":"puppy.puppy_mat","id":"puppy.puppy_mat","ambient":[1,1,1],"diffuse":[0.8,0.8,0.8],"specular":[0,0,0],"emissive":[0,0,0],"specularPower":12,"alpha":1,"backFaceCulling":true,"checkReadyOnlyOnce":false, 
"diffuseTexture":{"name":"puppy_01.jpg","level":1,"hasAlpha":1,"coordinatesMode":0,"uOffset":0,"vOffset":0,"uScale":1,"vScale":1,"uAng":0,"vAng":0,"wAng":0,"wrapU":1,"wrapV":1,"coordinatesIndex":0}}]

 

Which parameter is responsible for what I'd like to achieve (making the material not "shiny") and how/where do I set it in runtime?

Link to comment
Share on other sites

Hi Royi!  Umm...

http://www.babylonjs-playground.com/#1SVN3I#30

See lines 32-44?  We had to go thru some hell... to make sure the material/texture was loaded/ready... before we set alpha = 1.  I also added a report to console... about the url used for the texture (line 41).  Reports a good url.

You may need to do the same for your new model.  I don't know if you will need mesh.material.isReady(), because scene.executeWhenReady might already wait for all materials to be ready.  Test and experiment.  If your model has a texture, its .material will not be null (impossible, as far as I know).  You might be checking it too early.

The shine of a material is set to 0... by two different methods.  You can set material.specularColor to black, or set light.specular to black.  If the material has a light.specularTexture, kill that, too, or set its texture.level == 0.  Hope this helps.

Link to comment
Share on other sites

Hey

I have a preloader + timeout, so timing wasn't the problem, I referenced the wrong mesh.

The model has 2 meshes, I have no idea what the first one is, but it doesn't have a material. The second mesh does have a material like you helped me see.

Following your PG I also got the idea to change the diffuseTexture rather than the material, that way I won't have to deal with material.specularColor and other parameters as everything is already set to what was defined in blender.

Regardless, setting material.specularColor to black indeed solved the "shiny" issue.

 

What I'm trying to accomplish is having different skins (textures) for each model and changing between them easily. e.g. being able to do mesh.skin = 2 to render a different (already loaded) texture.

Is there an existing way to do that easily (ideally something like add multiple options for texture in blender which will then be exported to .babylon) or would I have to load the skins textures separately from the .babylon and change material.diffuseTexture manually?

Link to comment
Share on other sites

You'll have to duplicate materials anyway because if you just change the diffuseTexture on one model it will change it for all models because they still share the same material.

You can duplicate the model in blender and apply new materials to the clones as many times as you need; that way all materials will get exported without having to define them in Javascript.

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...