I have been porting a MaxScript custom model import script to JavaScript using three.js. I got all of the logic ported. The last part is building the mesh from the retrieved data. This was easy for mesh vertices and faces. However, it gets extremely complicated when it comes to the UV data. I get the UV data in an array of three point value arrays as like [-0.600184977054596, 1.9818799495697021, 0]
(long numbers but comparing output from actual script, the values are correct. Also, third point is always 0). Every example I have seen for the faceVertexUvs has been like:
geometry.faceVertexUvs[0].push(
// front
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
// right
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
// back
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
// left
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
// top
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
// bottom
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
);
However, the original script does it like:
for u = 1 to (mesh_uv_array.count) do setTVert cmdlmesh u mesh_uv_array[u]
for u = 1 to (mesh_face_array.count) do setTVFace cmdlmesh u mesh_face_array[u]
for u = 1 to (mesh_face_array.count) do setFaceMatID cmdlmesh u mesh_mat_array[u]
I’m not sure how to port this exactly. Going by the definition of setTVert and setTVFace, 3DS Max appears to automatically calculate UVs based on an array of vertexes and faces? I’m not exactly sure but the logic doesn’t match up with how three.js does it. I’d appreciate some help here.
PS:
Uing just the faces and vertices, I do get the mesh to successfully appear. So I did get that part down.