如何将 env 映射添加到 gltf 对象上

How to add env map onto gltf object

我在将环境贴图添加到加载的 GLTF / GLB 文件时遇到了很多麻烦,截至目前,我得到了某种反射而不是上面有光点的黑点,

我正在阅读一些关于三个 js 的文档,我认为我可以使用标准网格材质将其拉下来并以某种方式将其应用到对象 (gltf) 并将网格添加到场景中。我尝试了类似的模型,但该项目消失了。不知道怎么办,求助大佬们

这是我试图应用到它的环境贴图,(或类似的东西) https://hdrihaven.com/files/hdri_images/tonemapped/8192/venice_sunset.jpg

这是我正在使用的代码笔 https://codepen.io/8AD/pen/XWpxmpO

HTML

<script src="https://unpkg.com/three@0.87.1/build/three.js"></script>
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/build/three.min.js"></script>
<!-- OrbitControls.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/controls/OrbitControls.js"></script>
<!-- DRACOLoader.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/loaders/DRACOLoader.js"></script>
<!-- GLTFLoader.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/loaders/GLTFLoader.js"></script>

<div id="3dmain">

  
</div>

JS

var gltf = null;
var mixer = null;
var clock = new THREE.Clock();
var controls;
var camera;
init();
animate();

var renderCalls = [];
function render () {
requestAnimationFrame( render );
renderCalls.forEach((callback)=>{ callback(); });
}
render();

  
function init() {
    width = window.innerWidth;
    height = window.innerHeight;
    
    scene = new THREE.Scene();
    
  var light = new THREE.PointLight( 0xffffcc, 20, 200 );
light.position.set( 4, 30, 80 );
scene.add( light );

var light2 = new THREE.AmbientLight( 0x20202A, 20, 100 );
light2.position.set( 30, -10, 30 );
scene.add( light2 );

  
    camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 10000 );
    camera.position.set(0, 3, 10);


    window.addEventListener( 'resize', function () {
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      renderer.setSize( window.innerWidth, window.innerHeight );
    }, false );
    
    var geometry = new THREE.BoxGeometry(100, 5, 100);
    var material = new THREE.MeshLambertMaterial({
        color: "#707070"
    });
    
 
    var manager = new THREE.LoadingManager();
    manager.onProgress = function ( item, loaded, total ) {
        console.log( item, loaded, total );
    };

    var loader = new THREE.GLTFLoader();
    loader.setCrossOrigin( 'anonymous' ); 
    
    
    var scale = 0.01;
    var url = "https://8ad.studio/wp-content/uploads/3D%20Assets/blimp.glb";
    
    loader.load(url, function (data) {
        gltf = data;
        var object = gltf.scene;
        object.scale.set(scale, scale, scale);
        //object.position.y = -5;
        //object.position.x = 4;
        object.castShadow = true;
        object.receiveShadow = true;

        var animations = gltf.animations;
        if ( animations && animations.length ) {
            mixer = new THREE.AnimationMixer( object );
            for ( var i = 0; i < animations.length; i ++ ) {
                var animation = animations[ i ];
                mixer.clipAction( animation ).play();
            }
        }
        scene.add(object);
    });

    

    renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true } );
    renderer.setClearColor( 0x000000, 0 );
    renderer.shadowMap.enabled = true;
    
    controls = new THREE.OrbitControls( camera,);
      controls.rotateSpeed = 0.3;
controls.zoomSpeed = 0.9;

controls.minDistance = 14;
controls.maxDistance = 14;

controls.minPolarAngle = 0; // radians
controls.maxPolarAngle = Math.PI /2; // radians

controls.enableDamping = true;
controls.dampingFactor = 0.05;

var renderCalls = [];
renderCalls.push(function(){
  controls.update()
});

    renderer.setSize( width, height );
    renderer.gammaOutput = true;
    document.getElementById('3dmain').appendChild( renderer.domElement );
}

function animate() {
    requestAnimationFrame( animate );
    if (mixer) mixer.update(clock.getDelta());
    controls.update();
    render();
}

function render() {
    renderer.render( scene, camera );
}


您必须将 RGBELoader 包含到您的应用程序中以导入 HDR 纹理并使用 PMREMGenerator 以便预处理环境贴图以用于 PBR material .

var gltf = null;
var mixer = null;
var clock = new THREE.Clock();
var controls;
var camera;
var renderer;
init();
animate();

var renderCalls = [];

function render() {
  requestAnimationFrame(render);
  renderCalls.forEach((callback) => {
    callback();
  });
}
render();


function init() {
  width = window.innerWidth;
  height = window.innerHeight;

  scene = new THREE.Scene();

  var light = new THREE.PointLight(0xffffcc, 20, 200);
  light.position.set(4, 30, 80);
  scene.add(light);

  var light2 = new THREE.AmbientLight(0x20202A, 20, 100);
  light2.position.set(30, -10, 30);
  scene.add(light2);

  camera = new THREE.PerspectiveCamera(60, width / height, 0.01, 10000);
  camera.position.set(0, 3, 10);


  renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true
  });
  renderer.outputEncoding = THREE.sRGBEncoding;
  renderer.toneMapping = THREE.ACESFilmicToneMapping;
  renderer.toneMappingExposure = 1;
  renderer.setClearColor(0x000000, 0);
  renderer.shadowMap.enabled = true;

  window.addEventListener('resize', function() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
  }, false);

  var geometry = new THREE.BoxGeometry(100, 5, 100);
  var material = new THREE.MeshLambertMaterial({
    color: "#707070"
  });


  var manager = new THREE.LoadingManager();
  manager.onProgress = function(item, loaded, total) {
    console.log(item, loaded, total);
  };

  var scale = 0.01;
  var url = "https://8ad.studio/wp-content/uploads/3D%20Assets/blimp.glb";

  var loader = new THREE.GLTFLoader();
  loader.setCrossOrigin('anonymous');

  const pmremGenerator = new THREE.PMREMGenerator(renderer);
  pmremGenerator.compileEquirectangularShader();

  const rgbeLoader = new THREE.RGBELoader();
  rgbeLoader.load('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr', function(texture) {

    const envMap = pmremGenerator.fromEquirectangular(texture).texture;

    scene.background = envMap;
    scene.environment = envMap;

    texture.dispose();
    pmremGenerator.dispose();

    loader.load(url, function(data) {
      gltf = data;
      var object = gltf.scene;
      object.scale.set(scale, scale, scale);
      //object.position.y = -5;
      //object.position.x = 4;
      object.castShadow = true;
      object.receiveShadow = true;

      var animations = gltf.animations;
      if (animations && animations.length) {
        mixer = new THREE.AnimationMixer(object);
        for (var i = 0; i < animations.length; i++) {
          var animation = animations[i];
          mixer.clipAction(animation).play();
        }
      }
      scene.add(object);
    });

  });

  controls = new THREE.OrbitControls(camera, renderer.domElement);
  controls.rotateSpeed = 0.3;
  controls.zoomSpeed = 0.9;

  controls.minDistance = 14;
  controls.maxDistance = 14;

  controls.minPolarAngle = 0; // radians
  controls.maxPolarAngle = Math.PI / 2; // radians

  controls.enableDamping = true;
  controls.dampingFactor = 0.05;

  var renderCalls = [];
  renderCalls.push(function() {
    controls.update()
  });

  renderer.setSize(width, height);
  document.getElementById('3dmain').appendChild(renderer.domElement);
}

function animate() {
  requestAnimationFrame(animate);
  if (mixer) mixer.update(clock.getDelta());
  controls.update();
  render();
}

function render() {
  renderer.render(scene, camera);
}
body {
      margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/three@0.127/build/three.js"></script>
<!-- OrbitControls.js -->
<script src="https://cdn.jsdelivr.net/npm/three@0.127/examples/js/controls/OrbitControls.js"></script>
<!-- DRACOLoader.js -->
<script src="https://cdn.jsdelivr.net/npm/three@0.127/examples/js/loaders/DRACOLoader.js"></script>
<!-- GLTFLoader.js -->
<script src="https://cdn.jsdelivr.net/npm/three@0.127/examples/js/loaders/GLTFLoader.js"></script>
<!-- RGBELoader.js -->
<script src="https://cdn.jsdelivr.net/npm/three@0.127/examples/js/loaders/RGBELoader.js"></script>

<div id="3dmain">


</div>

该示例将环境贴图应用于 Scene.environment。但是,您也可以遍历 glTF 对象并将其应用于每个 material 的 envMap 属性.