While working on a project in three.js to create a ring, I encountered an issue with lighting. The camera setup is fine, but I'm facing difficulties with lighting. Below is my code snippet:
<script src="three.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejsfundamentals.org/threejs/../3rdparty/dat.gui.module.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
const color = 0xFFFFFF;
const intensity = 1;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(10, 10, 10);
light.target.position.set(-5, 0, 0);
scene.add(light);
scene.add(light.target);
document.body.appendChild( renderer.domElement );
let controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.rotateSpeed = 0.5;
controls.update();
camera.position.z = 5;
const geometry = new THREE.RingGeometry( 1, 3, 32 );
const material = new THREE.MeshBasicMaterial( { color: 0xffff00, side: THREE.DoubleSide } );
const mesh = new THREE.Mesh( geometry, material );
mesh.receiveShadow = true;
mesh.castShadow = true
scene.add( mesh );
function animate() {
requestAnimationFrame( animate );
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
renderer.render( scene, camera );
}
animate();
</script>
I've experimented with changing the light source to AmbientLight
and SpotLight
, but unfortunately, they didn't work as expected. My priority is to display the 3D shape, so any type of light source that can help achieve this goal is acceptable.