So, I have a glb scene of a florest, and I’m loading it into Three JS. The problem is, with a hemispherical lighting, it casts no shadows from the threes, rocks and all. It’s just a plane scene withou shadows. How could I fix this? I thought that I could do the lighting stuff in Blender and then export it and just render in Three JS, but I couldn’t do it. Can someone help me? I’m going to leave the code and some pics here!
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import {OrbitControls} from "three/addons/controls/OrbitControls";
// Classe para o personagem
class Character {
constructor(path) {
this.path = path;
this.model = null;
this.mixer = null;
this.clips = null;
const loader = new GLTFLoader();
loader.load(this.path, (gltf) => {
this.model = gltf.scene;
this.mixer = new THREE.AnimationMixer(this.model);
this.clips = gltf.animations;
})
}
getModel() {
return this.model;
}
update(deltaTime) {
if (this.mixer) {
this.mixer.update(deltaTime);
}
}
}
// Configuração da cena Three.js
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, -20, 5);
const clock = new THREE.Clock()
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0); // Set the target point
controls.enableZoom = true; // Enable zooming with the mouse wheel
controls.enablePan = true; // Enable panning by holding the middle mouse button
const hemisphereLight = new THREE.HemisphereLight(0xaaaaaa, 0x000000, 1);
const shadowLight = new THREE.DirectionalLight(0xffffff, 1);
shadowLight.position.set(-150, 350, 350);
shadowLight.castShadow = true;
shadowLight.shadow.camera.left = -400;
shadowLight.shadow.camera.right = 400;
shadowLight.shadow.camera.top = 400;
shadowLight.shadow.camera.bottom = -400;
shadowLight.shadow.camera.near = 1;
shadowLight.shadow.camera.far = 1000;
shadowLight.shadow.mapSize.width = 2048;
shadowLight.shadow.mapSize.height = 2048;
scene.add(hemisphereLight);
scene.add(shadowLight);
const mainCharacter = new Character('./knight5.glb')
const florest = new Character('florest.glb');
// Função de animação
const animate = () => {
requestAnimationFrame(animate);
const deltaTime = clock.getDelta();
scene.add(mainCharacter.getModel());
scene.add(florest.getModel());
controls.update(); // Update the camera controls
mainCharacter.update(deltaTime);
// Animação do personagem ou quaisquer outras atualizações
renderer.render(scene, camera);
};
animate();