import "./style.css";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";
import * as dat from "lil-gui";
const abramovic = new THREE.TextureLoader().load("textures/abramovic.jpg");
const wild = new THREE.TextureLoader().load("textures/wild.jpg");
const neon = new THREE.TextureLoader().load("textures/neon.jpg");
const test1 = new THREE.TextureLoader().load("textures/test1.jpg");
const test2 = new THREE.TextureLoader().load("textures/test2.jpg");
const test3 = new THREE.TextureLoader().load("textures/test3.jpg");
const test4 = new THREE.TextureLoader().load("textures/test4.jpg");
const mat1 = new THREE.TextureLoader().load(
"textures/terrazzo-15_Base Color.png"
);
const mat2 = new THREE.TextureLoader().load(
"textures/terrazzo-11_Base Color.png"
);
const mat3 = new THREE.TextureLoader().load(
"textures/modern-pattern-07_basecolor.png"
);
/**
* Base
*/
// Debug
// const gui = new dat.GUI();
// Canvas
const canvas = document.querySelector("canvas.webgl");
// Scene
const scene = new THREE.Scene();
// model
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("/draco/");
const gltfLoader = new GLTFLoader();
gltfLoader.setDRACOLoader(dracoLoader);
let mixer = null;
/*
a_greek_general.glb
countess_armand.glb
greek_helmet.glb
greek_jar.glb
bust.glb
soldier_horse.glb
pbr_corinthian_helmet.glb
*/
let model;
gltfLoader.load("/models/a_greek_general.glb", (gltf) => {
gltf.scene.traverse((node) => {
if (!node.isMesh) return;
// material
/* abramovic, wild, neon, test1, test2, test3, test4, mat1, mat2, mat3 */
node.material = new THREE.MeshLambertMaterial({ map: neon });
model = gltf.scene;
});
scene.add(model);
});
/**
* Lights
*/
const ambientLight = new THREE.AmbientLight(0xdadada, 0.3);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xdadada, 1);
directionalLight.castShadow = true;
directionalLight.shadow.camera.far = 150;
directionalLight.shadow.camera.left = -7;
directionalLight.shadow.camera.top = 7;
directionalLight.shadow.camera.right = 7;
directionalLight.shadow.camera.bottom = -7;
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
/**
* Sizes
*/
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
};
window.addEventListener("resize", () => {
// Update sizes
sizes.width = window.innerWidth;
sizes.height = window.innerHeight;
// Update camera
camera.aspect = sizes.width / sizes.height;
camera.updateProjectionMatrix();
// Update renderer
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
/**
* Camera
*/
// Base camera
const camera = new THREE.PerspectiveCamera(
100,
sizes.width / sizes.height,
0.1,
500
);
camera.position.set(25, 2, 60);
scene.add(camera);
// Controls
const controls = new OrbitControls(camera, canvas);
controls.target.set(0, 0.75, 0);
controls.enableDamping = true;
/**
* Renderer
*/
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
});
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
/**
* Animate
*/
const clock = new THREE.Clock();
let previousTime = 0;
const tick = () => {
const elapsedTime = clock.getElapsedTime();
const deltaTime = elapsedTime - previousTime;
previousTime = elapsedTime;
// Update controls
// ROTATION NOT WORKING
model.rotation.y = 0.4 * elapsedTime;
controls.update();
// Render
renderer.render(scene, camera);
// Call tick again on the next frame
window.requestAnimationFrame(tick);
};
tick();
Compare your code with the BeginnerExample step 3 from the collection.