框的“ 0”宽度呈现为“ 1”宽度

时间:2019-07-03 16:32:32

标签: javascript three.js 3d

创建盒子时

new THREE.BoxGeometry(opening.geometry.xLength, opening.geometry.yLength, opening.geometry.zLength)

制作宽度为0的框时。

new THREE.BoxGeometry(0, 1, 1)

它以1作为宽度在屏幕上呈现。我认为它不应呈现任何内容。这是threejs的错误吗?。

2 个答案:

答案 0 :(得分:1)

传递给BoxGeometryBoxBufferGeometry的构造函数的尺寸必须为正。

three.js无法验证函数或构造函数的参数。

three.js r.106

答案 1 :(得分:1)

Three.js的当前代码编写为0 =使用默认大小1

您可以通过设置大小为1的框并缩放几何形状来解决此问题

const geometry = new THREE.BoxGeometry(); // default is 1
geometry.applyMatrix(new THREE.Matrix4().makeScale(
    opening.geometry.xLength, opening.geometry.yLength, opening.geometry.zLength));

'use strict';

/* global THREE */

function main() {
  const canvas = document.querySelector('#c');
  const renderer = new THREE.WebGLRenderer({canvas});

  const fov = 75;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 5;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.z = 2;

  const scene = new THREE.Scene();
  scene.background = new THREE.Color('white');

  function addLight(...pos) {
    const color = 0xFFFFFF;
    const intensity = 1;
    const light = new THREE.DirectionalLight(color, intensity);
    light.position.set(...pos);
    scene.add(light);
  }
  addLight(-1, 2, 4);
  addLight( 2, 1, 4);

  const geometry = new THREE.BoxGeometry();
  geometry.applyMatrix(new THREE.Matrix4().makeScale(0, 1, 1));
  const material = new THREE.MeshPhongMaterial({color:'red'});
  const box = new THREE.Mesh(geometry, material);
  scene.add(box);

  function resizeRendererToDisplaySize(renderer) {
    const canvas = renderer.domElement;
    const width = canvas.clientWidth;
    const height = canvas.clientHeight;
    const needResize = canvas.width !== width || canvas.height !== height;
    if (needResize) {
      renderer.setSize(width, height, false);
    }
    return needResize;
  }

  function render(time) {
    time *= 0.001;

    if (resizeRendererToDisplaySize(renderer)) {
      const canvas = renderer.domElement;
      camera.aspect = canvas.clientWidth / canvas.clientHeight;
      camera.updateProjectionMatrix();
    }

    box.rotation.x = time;
    box.rotation.y = time;

    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
}

main();
body { margin: 0; }
#c { width: 100vw; height: 100vh; display: block; }
<canvas id="c"></canvas>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r105/three.min.js"></script>