Hello
Thank you for your suggestion. From there I started and I studied and I realized that a function regenerates the uv map based on the “fitTo” parameter inserted in the texture object.
The basic idea is that a texture must have a fixed dimension independent of the geometry to which it is applied as it happens in real world.
The “fitTo” variable is expressed in units.
The function works well enough but is not perfect on inclined surfaces like on octahedron.
I’ve taken pieces of code from various sources and I do not have a thorough understanding of geometry and UV map to do better. If someone wanted to help me improve it, I would be happy.
var fixTexture = new THREE.TextureLoader().load( texture_image );
fixTexture.userData = {
fitTo : 1
};
fixTexture.wrapS = THREE.RepeatWrapping;
fixTexture.wrapT = THREE.RepeatWrapping;
var material = new THREE.MeshPhongMaterial( {
color: 0x00ffff,
flatShading: true,
transparent: false,
opacity: 0.7,
map: fixTexture
});
call meshFitUvMap on mesh
var geometry = new THREE.BoxGeometry( 2, 5, 2 );
var box = new THREE.Mesh( geometry, material );
meshFitUvMap(box);
console.log(box);
scene.add( box );
This is the function
function meshFitUvMap(mesh) {
if (mesh.geometry &&
mesh.material &&
mesh.material.map &&
mesh.material.map.userData &&
mesh.material.map.userData.fitTo > 0) {
var geometry = mesh.geometry;
var textureFitTo = mesh.material.map.userData.fitTo;
var faces = mesh.geometry.faces;
for (var i = 0, len = faces.length; i < len; i ++) {
var face = faces[i];
var uv = geometry.faceVertexUvs[0][i];
var components = ['x', 'y', 'z'].sort(function(a, b) {
return Math.abs(face.normal[a]) > Math.abs(face.normal[b]);
});
var v1 = mesh.geometry.vertices[face.a];
var v2 = mesh.geometry.vertices[face.b];
var v3 = mesh.geometry.vertices[face.c];
var newUv0 = new THREE.Vector2(v1[components[0]] / textureFitTo, v1[components[1]] / textureFitTo);
var newUv1 = new THREE.Vector2(v2[components[0]] / textureFitTo, v2[components[1]] / textureFitTo);
var newUv2 = new THREE.Vector2(v3[components[0]] / textureFitTo, v3[components[1]] / textureFitTo);
uv[0].copy(newUv0);
uv[1].copy(newUv1);
uv[2].copy(newUv2);
}
}
}