We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

UV Mapping Lost When Updating Diffuse Texture via the API

Home Forums Programming UV Mapping Lost When Updating Diffuse Texture via the API

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #6056
    jem
    Customer

    Hello Soft8soft Team,
    I have a project where I must swap textures on an object. There are many textures and the catalog of textures will change over time. For these reasons, I do not want to include all of my texture files as part of the Blender file. I want to swap the texture with the API. If I can make this work, I could easily add new texture files to the web server and have the Verge3D scene apply them to the objects. This would simplify application maintenance.

    My question is similar to c.j.yen’s question: Is there a way to change basecolor texture . I did try to use Ivan’s suggested code from that thread. Here is my slightly altered version.

        app.ExternalInterface.setDiffuseMap = function(objectName, fileName) {
            var myObj = app.scene.getObjectByName(objectName);
            var mat = myObj.material;
            new v3d.TextureLoader().load(fileName, function(tex) { 
                mat.map = tex;
                mat.needsUpdate = true;
            });    
        }

    This does not work properly for me. When I run this function, the new texture file is applied, but my UV mapping is lost. Please see the attached screenshot. Here is a link to my working example ( example ). Click the thumbnail textures to apply them to the torus. When you click the thumbnail, the API code loads the correct image, but my carefully crafted UV mapping is lost.

    The torus uses basic Blender materials. It does not use the node system. The project archive is included.

    Is it possible to load and apply a new texture file and keep my UV mapping? Your help is much appreciated.
    Thank you,
    Jem

    Jeremy Wernick

    #6063

    Hi jem, that’s because the loaded texture needs to be prepared in some way according to use of the original texture which is replaced. This includes such settings as UV wrapping, color encoding, etc.
    Regarding the UV wrapping: by default it’s “clamp to edge” when the texture is loaded manually, but it seems that your UV map is intended to be “repeat”. So, I think you can just copy these settings from the original texture before assigning the new one.

    You can try the following code:

    
    app.ExternalInterface.setDiffuseMap = function(objectName, fileName) {
        var myObj = app.scene.getObjectByName(objectName);
        var mat = myObj.material;
        new v3d.TextureLoader().load(fileName, function(tex) { 
            if (mat.map) {
                var oldMap = mat.map;
                tex.wrapS = oldMap.wrapS;
                tex.wrapT = oldMap.wrapT;
                tex.encoding = mat.map.encoding;
            }
    
    	mat.map = tex;
            mat.needsUpdate = true;
        });	
    }
    

    Co-founder and lead developer at Soft8Soft.

    #6074
    jem
    Customer

    Hello Ivan,
    Your solution fixed this issue. I do appreciate your team’s rapid responses.

    Your explanation makes sense. You have given me a new area of the Verge3D system to investigate. Thank you :good: ,
    Jem

    Jeremy Wernick

    #6076

    Thanks! Glad it helped.

    Co-founder and lead developer at Soft8Soft.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.