I am new to three.js and I was trying this code but I encountered unexpected output so wanted to check what I am doing wrong here or looking to find reason behind output i am seeing.
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.set(75,20,0)
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
const controls = new OrbitControls(camera, renderer.domElement)
controls.target.set(0, 20, 0)
controls.update()
const sphere1 = new THREE.Mesh(new THREE.SphereGeometry(2, 20, 20), new THREE.MeshBasicMaterial({color: 0xFFFF00}))
sphere1.position.set(-20, 20, 0);
sphere1.castShadow = true
scene.add(sphere1)
const sphere2 = new THREE.Mesh(new THREE.SphereGeometry(2, 20, 20), new THREE.MeshBasicMaterial({color: 0xFFFF00}))
sphere2.position.set(20, 20, 0);
sphere2.castShadow = true
scene.add(sphere2)
function animate() {
requestAnimationFrame(animate)
renderer.render(scene, camera)
}
animate()
I am seeing three issue in this code which I am not able to explain:
- I was expecting two sphere appear right and left to each other but here they are appearing front and back
- When I change sphere position from (0, 20, 100) sphere moves to left which I dont understand why and another thing is that sphere appears oval and distored in shape, which I dont understand why.
3
You set the camera’s position to (75,20,0), then target it to (0,20,0). This results in your camera being very far in the X axis, while looking back to the origin (0,0,0) plus 20 units up. You place 2 spheres at (-20,20,0) and (20,20,0), which places them next to eachother on the X axis. The reason they appear like they are front and back is because you are looking at the spheres from the side.
If you want to view the spheres next to eachother, place your camera at (0,20,75).
Masyn Lithgo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2