我正在嘗試使用three.js 為我的網格加載紋理,但我得到的是純黑色物件,而不是加載的紋理。我按照 three.js 檔案(https://threejs.org/docs/?q=texture#api/en/loaders/TextureLoader - Example)中的方式進行了操作,并且還嘗試了不同的解決方案,但沒有任何效果。這是代碼
//Creating The Sun model
const sunGeometry = new THREE.SphereGeometry(16, 30, 30);
const texture = new THREE.TextureLoader().load('assets/images/sun.jpg');
const sunMaterial = new THREE.MeshBasicMaterial({ map: texture }); // not working
//const sunMaterial = new THREE.MeshBasicMaterial({ color: 'skyblue' }); //working
const sun = new THREE.Mesh(sunGeometry, sunMaterial);
//Adding The Sun to the Scene
scene.add(sun);
renderer.render(scene, camera);
設定網格顏色效果很好。也嘗試使用網址,沒有區別。我正在使用 Angular 并且該代碼在建構式中
編輯:影像路徑是正確的
uj5u.com熱心網友回復:
您renderer.render(scene, camera);在創建 后立即呼叫Mesh,但當時紋理尚未加載。加載紋理可能需要幾分之一秒,因此在加載它們時已經進行了渲染。如果你render()以每秒 60 幀的速度呼叫,你可能不會注意到這種延遲,但如果你只呼叫一次,那么你會得到一個黑色的素材。
如果您只想在紋理加載后render()呼叫一次,您應該只使用檔案頁面代碼示例中概述的方法中的回呼。onLoad
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517885.html
下一篇:Python:抓取作業門戶網站
