【问题标题】:Attempts to load a texture show no error but the texture does not display尝试加载纹理未显示错误,但未显示纹理
【发布时间】:2019-07-28 21:28:44
【问题描述】:

我有一个模型、一个背景天空和一个地面。对表面进行纹理处理会导致没有表面。

我已经尝试了基本方法并得出结论,可能是在纹理完成加载之前渲染了场景。在搜索并找到了各种可能的解决方案后,我尝试了其中的几个,但并没有真正理解它们应该如何工作。他们都没有工作。一个问题是它是一个老问题,并且大多数建议都涉及过时的three.js库版本。

// Ground
// create a textured Ground based on an answer in Stackoverflow.
   var loader = new THREE.TextureLoader();
   loader.load('Textures/Ground128.jpg', 
      function (texture) {
         var groundGeometry = new THREE.PlaneBufferGeometry(2000, 2000, 100, 100); 
         const groundMaterial = new THREE.MeshLambertMaterial({map: texture});
         var ground = new THREE.Mesh(groundGeometry, groundMaterial);
         ground.receiveShadow = true;  //Illumination addition
         ground.rotation.x = -0.5 * Math.PI; // rotate into the horizontal.
         scene.add(ground);   
      }
   );      

// This variation does not work either

http://lhodges.users37.interdns.co.uk/me/downloads/Aphaia/Temple.htm http://lhodges.users37.interdns.co.uk/me/downloads/Aphaia/Temple7jsV0.15b.htm

上面的第一个是完整的页面,其中地面是一个普通的台球桌绿色。第二个是包含上述代码的页面。

似乎没有错误(我上次尝试过。)

【问题讨论】:

    标签: three.js textures delayed-loading


    【解决方案1】:

    当您的纹理加载并添加地面时,您的场景已经渲染(并且没有其他渲染调用)。
    将地面添加到场景后,您需要调用renderer.render(scene, camera);

    // Ground
    // create a textured Ground based on an answer in Stackoverflow.
       var loader = new THREE.TextureLoader();
       loader.load('Textures/Ground128.jpg', 
          function (texture) {
             var groundGeometry = new THREE.PlaneBufferGeometry(2000, 2000, 100, 100); 
             const groundMaterial = new THREE.MeshLambertMaterial({map: texture});
             var ground = new THREE.Mesh(groundGeometry, groundMaterial);
             ground.receiveShadow = true;  //Illumination addition
             ground.rotation.x = -0.5 * Math.PI; // rotate into the horizontal.
             scene.add(ground); 
             renderer.render(scene, camera); // <--- add this line  
          }
       ); 
    

    【讨论】:

    • 谢谢! (我不应该这么说)。它有效,但我可能需要做一些平铺。现在没有阴影了。我期待太多了吗?我是否必须调用 ShadowMaterial 或类似的覆盖?
    • 在 stack 上搜索threejs 阴影并查看three.js 示例。但是我可以看到您有renderer.shadowMap.enabled; 行,您需要将其设置为true。 renderer.shadowMap.enabled = true;。如果对您有帮助,请采纳答案。
    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    相关资源
    最近更新 更多