在改变纹理后,three.js gltf模型变为灰色且无纹理



嘿,伙计们,我是three.js的新手,我正在尝试使用Meshmatcap材质更改我的纹理。然而,我面临的问题是,我更改了纹理,添加skinning: true后,我的模型纹理和颜色将消失,这是我的模型保持大小所必需的。有什么办法解决这个问题吗?提前谢谢。当前使用的模型来自https://sketchfab.com/3d-models/tyrannosaurus-rex-9d3a3e42c0054c35aa39c3ee07388d16

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>3d model</title>
</head>

<body>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.114/build/three.module.js';

import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.114/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/three@0.114/examples/jsm/loaders/GLTFLoader.js';
import { RGBELoader } from 'https://cdn.jsdelivr.net/npm/three@0.114/examples/jsm/loaders/RGBELoader.js';

var container, controls;
var camera, scene, renderer, mixer, clock;
var obj , material , texture , mesh

init();
animate();

function init() {

container = document.getElementById( 'test' );
document.body.appendChild( container );


camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.01, 1000 );
camera.position.x = 0
camera.position.y = 2
camera.position.z = 10 


scene = new THREE.Scene();
//   scene.background = new THREE.Color(0xffffff);
var light = new THREE.HemisphereLight(0xffffff,0x000000,10);
scene.add(light);

clock = new THREE.Clock(); 

var loader = new GLTFLoader();
// Load a glTF resource

loader.load('scene.gltf', function ( gltf ) {

var textureLoader = new THREE.TextureLoader();
mesh = gltf.scene.children[0]
console.log(mesh)
var texture = textureLoader.load('blue1.jpg');
// texture.minFilter = THREE.LinearFilter;

var matcapMaterial = new THREE.MeshMatcapMaterial({ skinning: true ,normalMap: texture })
obj = scene.add( mesh );
obj.traverse((o) => {
if (o.isMesh) o.material = matcapMaterial

;

})

;

mixer = new THREE.AnimationMixer( mesh );

gltf.animations.forEach( ( clip ) => {

mixer.clipAction( clip ).play();

} );

} );



renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.8;
renderer.outputEncoding = THREE.sRGBEncoding;
container.appendChild( renderer.domElement );


}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );
}

//

function animate() {
requestAnimationFrame( animate );
var delta = clock.getDelta();
if ( mixer ) mixer.update( delta );
renderer.render( scene, camera );

}
</script>
<div id="test">
</div>
</body>
</html>

用手动加载的纹理替换glTF资源的纹理时,必须将Texture.flipY属性设置为false,以适应glTF的uv约定。

THREE.GLTFLoader的文档页面中也提到了这种情况。

最新更新