I have a problem with textures; it means they don’t appear by themselves. Everything seems to be in the right place on the server (in the ‘textures’ folder), and the console doesn’t list any errors.
Other way, when I tried load them using ‘THREE.TextureLoader()’, the textures aren’t properly aligned.
My .js
<div data-theme-toc="true">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.157.0/three.module.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.129.0/examples/jsm/controls/OrbitControls.js';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdddddd);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1, 1);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("model-container").appendChild(renderer.domElement);
window.addEventListener('resize', () => {
const newWidth = window.innerWidth;
const newHeight = window.innerHeight;
camera.aspect = newWidth / newHeight;
camera.updateProjectionMatrix();
renderer.setSize(newWidth, newHeight);
});
let model;
let pivot;
const pointLight = new THREE.PointLight(0xffffff, 100);
pointLight.position.set(0, 9, 25);
scene.add(pointLight);
const loader = new GLTFLoader();
loader.load('../scene.gltf', (gltf) => {
const model = gltf.scene;
// Assuming the model's material contains embedded textures
// Access the material and assign it to the model
const material = model.material;
model.traverse((object) => {
if (object.isMesh) {
const material = new THREE.MeshStandardMaterial;
object.material = material;
}
});
// Create a pivot point
pivot = new THREE.Object3D();
pivot.add(model);
scene.add(pivot);
// Set up OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
// Start the rotation after the model is loaded
animate();
});
function animate() {
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
// Start the animation loop
animate();
</div>
Multiple instances of Three.js being imported is in 99.9% cases not a good sign (it’s likely not causing the error here, but it can cause others.)
Did you check the Network panel in devtools? Click on each texture request and check whether the returned response is the proper, binary image, not an XML / JSON error.
this doesn’t look right, with the line const material = model.material;model is associated with the gltf.scene which is a scene object, you’re also overwriting the materials of meshes in the scene with new THREE.MeshStandardMaterial without reasigning the material.map to the new material… this is pretty messy and unnecessary if the gltf model already has materials, they are likely already imported as MeshStandardMaterial and you could omit the whole traverse method but if you need to do this try…
const loader = new GLTFLoader();
loader.load('../scene.gltf', (gltf) => {
const model = gltf.scene;
model.traverse((object) => {
if (object.isMesh) {
const map = object.material && object.material.map ? object.material.map : null
const material = new THREE.MeshStandardMaterial;
material.map = map
object.material = material;
}
});
also as @mjurczyk mentioned you’ll want to sync your three versions…
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.157.0/three.module.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.129.0/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/three@0.157.0/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.157.0/examples/jsm/controls/OrbitControls.js';
Yes they will do if they are embedded, you are overwriting the material that has the texture assigned with new MeshStandardMaterial so you’re effectively loading the textures twice, once with the model and then again with the texture loader…