Jump to content

[SOLVED] Changing SubMaterial turns Mesh Invisible


djeustice
 Share

Recommended Posts

Hello.  My Kayak model has 4 sub materials on it.  I have a button called "KayakLime". I'd like this button to change sub material #2 from Peach.png to Lime.png when clicked.  Example #1 below works,  but it changes the 4 kayak sub materials all to Lime.png.

 var myKayak2;
        BABYLON.SceneLoader.ImportMesh("", "models/", "Kayak.gltf", scene, function (newMeshes)  {
            myKayak2 = newMeshes[0];       
        });

var LimeTexture = new BABYLON.StandardMaterial("Lime", scene);
    LimeTexture.diffuseTexture = new BABYLON.Texture("models/Lime.png", scene);
    LimeTexture.diffuseTexture.hasAlpha = false;
    LimeTexture.backFaceCulling = false;
    
 document.getElementById("KayakLime").addEventListener("click",function () {
            myKayak2.material = LimeTexture;  
        });

Example #2: I thought this click code below would change only sub material #2 to Lime.png.  However it turns the kayak invisible.  Do I need to add more properties in the LimeTexture material? Or is it something else I'm missing?

document.getElementById("KayakLime").addEventListener("click",function () {
            myKayak2.material.subMaterials[2] = LimeTexture;  
        }); 

Example#1.jpg

Example#2.jpg

Link to comment
Share on other sites

Thanks for replying. I changed my Lime Standard material to PBR material and it seems to be working now.  I think it was erroring because Kayak.gltf was looking for PBR properties:

    var myKayak2;
        BABYLON.SceneLoader.ImportMesh("", "models/", "Kayak.gltf", scene, function (newMeshes)  {
            myKayak2 = newMeshes[0];              
        }); 
    
    var LimeTexture = new BABYLON.PBRMaterial("Lime", scene);
    LimeTexture.albedoTexture = new BABYLON.Texture("models/Lime.png", scene);
    LimeTexture.baseColor = new BABYLON.Color3(1.000, 1.000, 1.000);
    LimeTexture.metallic = 0.0;
    LimeTexture.roughness = 1.0;
    LimeTexture.albedoTexture.hasAlpha = false;
    LimeTexture.backFaceCulling = false;
    
    document.getElementById("KayakLime").addEventListener("click",function () {
            myKayak2.material.subMaterials[2] = LimeTexture;  
        });  

LimeTexture.jpg

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