Hello, in my code, I’m trying to render text (with positions defined in an array). I can see the text correctly, and everything works except that the letters are not straight. The letters shift more and more with each one. How can I fix this so that the text is perfectly aligned ?
const loader = new FontLoader();
loader.load("https://threejs.org/examples/fonts/helvetiker_regular.typeface.json", (font) => {
textMeshesRef.current = [];
hoverPlaneRefs.current = [];
allPosition.forEach(({ title, x, y, z }, index) => {
const textGroup = new THREE.Group();
const avgZ = z + (title.length * 0.05) / 2;
textMeshesRef.current[index] = [];
hoverPlaneRefs.current[index] = [];
let currentOffset = 0; // Gère l'espacement total
[...title].forEach((char, i) => {
const geometry = new TextGeometry(char, {
font,
size: 0.05,
height: 0.005
});
geometry.computeBoundingBox(); // Calculer la boîte englobante
const charWidth = geometry.boundingBox.max.x - geometry.boundingBox.min.x; // Largeur réelle
const charHeight = geometry.boundingBox.max.y - geometry.boundingBox.min.y; // Hauteur réelle
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
const mesh = new THREE.Mesh(geometry, material);
// Ajuster la position y pour aligner les lettres
const yOffset = -charHeight / 2;
mesh.position.set(x, y + yOffset, z + currentOffset);
mesh.userData.originalPosition = mesh.position.clone();
mesh.lookAt(new THREE.Vector3(x, y + 0.6, avgZ));
mesh.rotation.z = -1.5;
textMeshesRef.current[index].push(mesh);
textGroup.add(mesh);
currentOffset += charWidth + 0.025; // Ajout d'un petit espacement constant entre les lettres
});
sceneRef?.current?.add(textGroup);
const planeGeometry = new THREE.PlaneGeometry(0.5, 0.1);
const planeMaterial = new THREE.MeshBasicMaterial({
color: 0xff0000,
transparent: true,
opacity: 0
});
const hoverPlane = new THREE.Mesh(planeGeometry, planeMaterial);
hoverPlane.position.set(x + 0.025, y, z + 0.2);
hoverPlane.lookAt(new THREE.Vector3(x + 0.025, y + 0.6, z + 0.2));
hoverPlane.rotation.z = -1.5;
sceneRef?.current?.add(hoverPlane);
hoverPlaneRefs.current[index].push(hoverPlane);
});
window.addEventListener("mousemove", onMouseMove);
window.addEventListener("click", onMouseClick);
});