Home › Forums › Programming › UV Mapping Lost When Updating Diffuse Texture via the API
- This topic has 3 replies, 2 voices, and was last updated 6 years, 4 months ago by Ivan Lyubovnikov.
-
AuthorPosts
-
2018-07-29 at 1:56 am #6056jemCustomer
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,
JemJeremy Wernick
2018-07-30 at 8:25 am #6063Ivan LyubovnikovStaffHi 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.
2018-07-30 at 1:37 pm #6074jemCustomerHello 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 ,
JemJeremy Wernick
2018-07-30 at 1:40 pm #6076Ivan LyubovnikovStaffThanks! Glad it helped.
Co-founder and lead developer at Soft8Soft.
-
AuthorPosts
- You must be logged in to reply to this topic.