Hello,
new to Three.js
, love it.
Playing around with a very simple scene
that has all elements within xyz [0, 1].
I’ve added OrbitControls
and set the target, so that the scene rotates around the center of 0.5, 0.5, 0.5 (middle of [0, 1] scene). This works just fine after initial hiccups, see next.
I’m facing two issues:
(1) the scene initially renders with a canvas of [-1, 1], so my objects are not centered as all of my objects are within [0, 1] - as they will be using external data that is normalized [0, 1]
(2) once I click drag the mouse (to rotate the canvas), the scene now jumps into a different position (because of attached OrbitControls
) and then stays there
I ultimately want to start the scene in a certain position and have OrbitControls
attached so that user can pan/zoom/tilt/move the scene (without jumping) from that starting position.
I’ve tried re-positioning the scene
and camera
, but no luck.
The screenshot below shows the starting position I want.
How can I achieve that?
and this right here is the full code to re-create:
import * as THREE from 'three';
import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
// -------- (1) canvas attrs --------
const w = 800;
const h = 800;
// -------- (2) create scene --------
const scene = new THREE.Scene();
// -------- (3) create camera --------
const camera = new THREE.PerspectiveCamera(35, w/h, 0.01, 3000);
camera.position.z = 3.5;
// -------- (4) create renderer --------
const renderer = new THREE.WebGLRenderer();
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);
// -------- (5) create objects --------
const gh_1 = new THREE.GridHelper(1, 10);
gh_1.position.x = 0.5;
gh_1.position.z = 0.5;
scene.add(gh_1);
const gh_2 = new THREE.GridHelper(1, 10);
gh_2.position.x = 0.5;
gh_2.position.z = 0;
gh_2.position.y = 0.5;
gh_2.rotation.x = 1.57; // <-- mystery number
scene.add(gh_2);
const gh_3 = new THREE.GridHelper(1, 10);
gh_3.position.x = 0;
gh_3.position.z = 0.5;
gh_3.position.y = 0.5;
gh_3.rotation.x = 1.57;
gh_3.rotation.z = 1.57;
scene.add(gh_3);
// -------- (6) add orbit controls --------
const controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', draw);
controls.target = new THREE.Vector3(0.5, 0.5, 0.5);
// -------- (7) render --------
function draw()
{
renderer.render(scene, camera);
}
draw();
as a bonus question:
why does the rotation.x|z of Gridhelper
has to be 1.57 in order to position it orthogonal to the bottom Gridhelper
plane? I ended there merely by playing with the numbers.
As a side note, I do not use AnimationLoop
because the scene will only have static data and only the user will pan/zoom/tilt/move the elements.
Thanks!