I create points based on the fibonacci sphere, then using TextGeometry I create a virtual sphere from the resulting positions.How i can rotate this virtual sphere with no effect on text nodes?
function fibonacciSphere(numPoints, point) {
const rnd = 1;
const offset = 2 / numPoints;
const increment = Math.PI * (3 - Math.sqrt(5));
const y = point * offset - 1 + offset / 2;
const r = Math.sqrt(1 - Math.pow(y, 2));
const phi = ((point + rnd) % numPoints) * increment;
const x = Math.cos(phi) * r;
const z = Math.sin(phi) * r;
return new THREE.Vector3(x, y, z);
}
const loader = new FontLoader();
const group = new THREE.Group();
function main() {
const camera = new THREE.PerspectiveCamera(75, 2, 0.1, 100);
camera.position.z = 2;
const scene = new THREE.Scene();
function addTextNode(text, vert) {
loader.load("./fonts/Fira.json", function (font) {
const geometry = new TextGeometry(text, {
font: font,
size: 0.16,
height: 0.01,
});
const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const mesh = new THREE.Mesh(geometry, material);
mesh.position.x = vert.x;
mesh.position.y = vert.y;
mesh.position.z = vert.z;
group.add(mesh);
scene.add(group);
});
}
const renderer = new THREE.WebGLRenderer();
const container = document.querySelector("#root");
container.appendChild(renderer.domElement);
const numPoints = 50;
for (let i = 0; i < numPoints; ++i) {
addTextNode(`p${i}`, fibonacciSphere(numPoints, i));
}
function render(time) {
time *= 0.001;
// group.rotation.x += 0.01;
scene.traverse((child) => {
child.children.map((el) => {
el.quaternion.copy(camera.quaternion);
});
});
renderer.setSize(container.clientWidth, container.clientHeight);
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();
I direct them to face the camera, but when I try to rotate this group of elements, it stands still if I don’t remove the rotation to the camera.
if you put this code in the render function - everything works as it should, but gives an error
THREE.Quaternion: .setFromEuler() encountered an unknown order: undefined
scene.traverse((child) => {
let position = new THREE.Vector3();
let direction = new THREE.Vector3();
child.children.map((el) => {
el.getWorldPosition(position);
el.position.copy(position);
child.getWorldDirection(direction);
child.rotation.copy(direction);
});
});