关于 three.js 文档的问题 -- 对象 属性 与十六进制字符串参数

Question on three.js documentation -- object property vs. hexadecimal string parameter

来自 MeshBasicMaterial 的 threejs 文档:

Constructor MeshBasicMaterial( parameters : Object ) parameters - (optional) an object with one or more properties defining the material's appearance. Any property of the material (including any property inherited from Material) can be passed in here.

The exception is the property color, which can be passed in as a hexadecimal string and is 0xffffff (white) by default. Color.set( color ) is called internally.

“可以作为十六进制字符串传入”是什么意思?
下面的代码有 MeshBasicMaterial(0xff0000) 但它不起作用。
我查看了 three.js,但没有看到任何参数类型检查。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My first three.js app</title>
    <style>
      body { margin: 0; }
    </style>
  </head>
  <body>
    <script src="http://threejs.org/build/three.js"></script>
    <script>
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize( window.innerWidth, window.innerHeight );
      document.body.appendChild( renderer.domElement );
      const geometry = new THREE.BoxGeometry();
//      const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
      const material = new THREE.MeshBasicMaterial(0xff0000); // THIS DOESN'T WORK
      const cube = new THREE.Mesh( geometry, material );
      scene.add( cube );
      camera.position.z = 5;
      function animate() {
        requestAnimationFrame( animate );
        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;
        renderer.render( scene, camera );
      };
      animate();
    </script>
  </body>
</html>

您正试图将参数作为十六进制字符串传递。您需要将 颜色 属性 作为十六进制字符串传递给参数对象。

new THREE.MeshBasicMaterial({ color: 0xff0000 });

然后 internally material 将调用 Color.set(color)

它描述的“例外”是 color 是唯一一个 属性 不需要传递其类型 Color 的对象。例如:

  • aoMap:需要THREE.Texture
  • 颜色:可以是 THREE.Color 十六进制 0xffffff 值。