0

I am currently using a matrix to mirror my Collada models.

I have posted this question before: Flip Normals Three.JS after flipping geometry

and referenced this one: ThreeJS geometry flipping

The problem is that I can't flip the geometry before adding it to a mesh to avoid the inverted material, since it is a Collada model, which already contains the mesh.

What would be the best way to go about mirroring a Collada model while maintaining its material?

I thought I could do the following, but I didn't know how to target the materials properly to change them (my new geometry does not have a new material applied to it). Also my models are fairly complex and contain a lot of hierarchies. The result is the flipped Collada model, with an inside out material.

        var mS = (new THREE.Matrix4()).identity();
        var newGroup = new THREE.Object3D();
        var newMesh;

        mS.elements[5] = -1;

        mesh.traverse(function(child){
                    if (child.geometry!==undefined) {
                          if (child instanceof THREE.Mesh) {

                                child.geometry.applyMatrix(mS);
                                child.geometry.verticesNeedUpdate = true;
                                child.geometry.normalsNeedUpdate = true;
                                child.geometry.computeVertexNormals();
                                child.geometry.computeBoundingSphere();
                                child.geometry.computeFaceNormals();
                                child.geometry.computeVertexNormals();
                          
                                if (child.material.materials!=undefined) {
                                      for (var i; i<child.material.materials.length; i++) {
                                            child.material.materials[i]=newMaterial;
                                            child.material.materials[i].wrapAround = true;
                                            child.material.materials[i].side = THREE.DoubleSide;
                                      }
                               
                                } 
                                
                                var geom = child.geometry.clone();
                                
                                newMesh  = new THREE.Mesh(geom, child.material);
                                newMaterial.needsUpdate = true;
                                newGroup.add( newMesh );
                          };
                    };
              });
        return newGroup;

Thanks!

UPDATE

I had a typo in my code. Everything works. var i; should be var i=0;

Community
  • 1
  • 1
Laura
  • 469
  • 1
  • 6
  • 12
  • three.js r.68 does not support transforms involving reflections. The best solution is to mirror your model using modeling software outside of three.js. Your imported mirrored model will then be correct, and you will save yourself a lot of time and trouble. – WestLangley Oct 17 '14 at 01:17
  • @WestLangley The application requires the model to be reflected in some situations and not in others. I guess I will have to have two separate models then. Its unfortunate that three.js does not support reflections. Do you know if there are any plans to support it in upcoming versions? – Laura Oct 17 '14 at 03:13
  • There are no plans to support transforms involving reflections. A work-around would be to develop a utility to reflect a geometry. – WestLangley Oct 17 '14 at 05:06
  • @WestLangley what do you mean by developing a utility to reflect geometry? Do you mean in webgl or three.js or what? How would I start to go about that? – Laura Oct 17 '14 at 18:35
  • I was suggesting the development of a three.js Javascript utility. – WestLangley Oct 18 '14 at 03:09
  • @WestLangley in a high-level overview, what would this three.js javascript utility need to do in order to properly flip the geometry? (I'm unsure how to get started...) – Laura Oct 20 '14 at 13:45
  • 1
    Whoops. My code works if I just correct the typo : for (var i; i – Laura Oct 28 '14 at 16:52

0 Answers0