简体   繁体   English

Threejs 与 React .. 几何存在但未显示

[英]Threejs with React .. Geometry exists but not showing

Could anyone help me with this code ..?谁能帮我这个代码..?

it's not working properly with React.它不能与 React 一起正常工作。 I want to show the sphere under the light.我想在灯光下展示球体。

here is my code in the sandbox:这是我在沙箱中的代码:

https://codesandbox.io/s/three-planet-fwkpx5?file=/src/ThreeMars.js https://codesandbox.io/s/three-planet-fwkpx5?file=/src/ThreeMars.js

First, you're changing from not having textures (when marsMaterial is declared) to having them (after the TextureLoader runs).首先,您正在从没有纹理(在声明marsMaterial时)变为拥有它们(在TextureLoader运行之后)。 This change requires recompiling the shader program through a call to marsMaterial.needsUpdate = true;此更改需要通过调用marsMaterial.needsUpdate = true; . . See this page under "Materials."请参阅“材料”下的此页面

Second, you need to re-check the URLs for your textures.其次,您需要重新检查纹理的 URL。 Viewing them under the "Network" tab of the Dev Tools shows that they're responding not as the textures, but as your whole sandbox (which is how CodeSandbox handles a 404).在开发工具的“网络”选项卡下查看它们表明它们响应的不是纹理,而是整个沙箱(CodeSandbox 处理 404 的方式)。

To correct the loading of the textures, add imports for them after your existing import statements.要更正纹理的加载,请在现有import语句之后为它们添加导入。 The file locations in a React app's source don't correspond to URLs in the running app, because the running app has been bundled; React 应用程序源中的文件位置与正在运行的应用程序中的 URL 不对应,因为正在运行的应用程序已被捆绑; these imports cause the files to be bundled with the app.这些导入会导致文件与应用程序捆绑在一起。 1 1

+   import marsDiffuse from "./assets/mars/mars-map.jpeg";
+   import marsBump from "./assets/mars/mars-bump.jpeg";

Then, change the loader.load() calls to match.然后,更改loader.load()调用以匹配。

-   marsMaterial.map = loader.load("./assets/mars/mars-map.jpeg");
-   marsMaterial.bumpMap = loader.load("./assets/mars/mars-bump.jpeg");
+   marsMaterial.map = loader.load(marsDiffuse);
+   marsMaterial.bumpMap = loader.load(marsBump);

Finally, after setting the other properties on your material, add a line to set needsUpdate on it.最后,在你的材质上设置其他属性后,添加一行来设置needsUpdate

    ...
    marsMaterial.specular = new THREE.Color("#000000");
+   marsMaterial.needsUpdate = true;

Oh, and turn down your light, just a hair.哦,关掉你的灯,只是一根头发。

-   let light = new THREE.PointLight(0xffffff, 200, 5000);
+   let light = new THREE.PointLight(0xffffff, 1, 0, 2);    

1. This is a clumsy way to describe the matter, I know; 1. 我知道,这是一种笨拙的描述方式; I welcome anyone more familiar with React to comment with a better description.我欢迎任何更熟悉 React 的人用更好的描述发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM