BoxHelper使BoxGeometry巨大

时间:2016-12-19 14:48:59

标签: javascript 3d three.js

修改

我发现只要我想添加HelperBox就会出现巨型盒子,我制作了a JSFiddle,这里没有任何东西会影响第二个盒子的大小。然而,在我的代码中,有一些东西干扰了我的盒子,因为当我将Helper添加到场景时它会弄乱我的第二个盒子,你可以在这里看到:你可以看到巨型盒子的轮廓{{3}如果我再缩小一些你可以看到他们全部被覆盖on this screenshot我无法弄清楚为什么会发生这种情况......特别是因为JSFiddle工作正常。 (或许盒子的乱七八糟的轮廓是一个指示物?)

ORIGINAL

我试图检查三个盒子中两个盒子的碰撞,我得到了一些结果,我并没有期待并实现了THREE.BoxHelper。当我在我的第一个盒子上使用它时,没有什么不对,但是当我在第二个盒子上添加帮助器时,它显得如此巨大......我似乎无法找到我出错的地方。我的基础是in the giant blue box.

我在视觉上得到的截图:

this article

这是触发此操作的代码的主要部分:

const makeTestCube = () => {
  testBox = new THREE.Mesh(
    new THREE.BoxGeometry(3, 3, 3),
    new THREE.MeshBasicMaterial({color: 0xFF0099})//PINK
  );
  testBox.position.set(0, 0, 20);
  testBoxBbox = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
  testBoxBbox.setFromObject(testBox);
  const testBoxHelper = new THREE.BoxHelper(testBoxBbox, 0xCC6600); //ORANGE
  testBox.geometry.computeBoundingBox();
  scene.add(testBox);
  scene.add(testBoxHelper);
};

const makeAnotherTestCube = () => {
  anotherTestBox = new THREE.Mesh(
    new THREE.BoxGeometry(3, 3, 3),
    new THREE.MeshBasicMaterial({color: 0x990000})//RED This is the small box in the giant box
  );
  anotherTestBox.position.set(0, 0, 30);
  anotherTestBoundingBox = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
  anotherTestBoundingBox.setFromObject(anotherTestBox);
  const anotherTestBoxHelper = new THREE.BoxHelper(anotherTestBox, 0xCC6600);// This is the giant box (Orange)
  anotherTestBox.geometry.computeBoundingBox();
  scene.add(anotherTestBox);
  scene.add(anotherTestBoxHelper);
};

这是我的整个剧本:

import sets from './data/sets';

import ColladaLoader from 'three-collada-loader';

import BufferLoader from './modules/sound/BufferLoader';
import SpawnObject from './modules/render/SpawnObject';
import Controls from './modules/util/Controls';

import io from 'socket.io-client';
import {isEmpty} from 'lodash';

const OrbitControls = require(`./modules/util/OrbitControls`)(THREE);
const DeviceOrientationControls = require(`./modules/util/DeviceOrientationControls`);

// const PointerLockControls = require(`./modules/util/PointerLockControls`);

let scene, camera, renderer, VRcontrols, element, cameraBox, testBox, anotherTestBox, cameraBoundingBox, testBoundingBox, anotherTestBoundingBox;
let testBoxBbox;
let audioCtx, bufferLoader;
let socket, controlData;
let player, mobile;
let moveForward = false, moveBackward = false, moveLeft = false, moveRight = false;
let camX = 0;
const camY = 0;
let camZ = 2;
let mousedown = false;
const camSpeed = .1;
const notes = [];
let devices = [];
const collidableMeshList = [];

const mobilecheck = () => {
  mobile = false;
  (function(a) {if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) mobile = true;})(navigator.userAgent || navigator.vendor || window.opera);
  return mobile;
};

const init = () => {

  mobilecheck();

  socket = io(`/`);

  socket.on(`init`, handleWSInit);
  socket.on(`dataTransfer`, handleWSData);

  window.AudioContext = window.AudioContext || window.webkitAudioContext;

  if (!mobile) {
    document.addEventListener(`keydown`, useArrowDown);
    document.addEventListener(`keyup`, useArrowUp);
    document.addEventListener(`mousedown`, () => mousedown = true);
    document.addEventListener(`mouseup`, () => mousedown = false);
    document.addEventListener(`mousemove`, e => {
      if (mousedown) {
        const rotateAngle = Math.PI / 4;
        player.rotateY(rotateAngle * e.movementX * .1);
      }
    });
  }

};

const handleWSInit = users => {
  const {id: socketId} = socket;

  users = users.map(u => {
    if (u.socketId === socketId) u.isMe = true;
    return u;
  });

  devices = users;

  if (window.location.href.indexOf(`controls`) > - 1) {
    const controls = new Controls(socket, devices);
    console.log(controls);
    return;
  }

  document.querySelector(`main`).classList.remove(`controls`);

  loadAudio();

};

const loadAudio = () => {
  audioCtx = new AudioContext();
  bufferLoader = new BufferLoader(audioCtx);

  bufferLoader.load(sets.drums);
    // .then(data => spawnObject(data));


  initEnvironment();
};

const handleWSData = data => {
  if (data !== undefined || data !== null || isEmpty(data)) controlData = data;

  devices = devices.map(u => {
    u.yo = false;
    return u;
  });

};

const spawnObject = data => {

  for (let i = 0;i < 5;i ++) {
    const bol = new SpawnObject(`object.dae`, audioCtx, data[0], scene, true);
    notes.push(bol);
    collidableMeshList.push((notes[0].scene.children[2]));

  }
};

const initEnvironment = () => {
  console.log(notes);
  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(
    50, window.innerWidth / window.innerHeight,
    1, 1000
  );

  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  element = renderer.domElement;
  document.body.appendChild(element);

  //PLAYER
  player = new THREE.Object3D();
  player.add(camera);
  player.velocity = new THREE.Vector3(0, 0, 0);
  makeTestCube();
  // makeCameraBox();
  makeAnotherTestCube();
  scene.add(player);

  camera.position.set(0, 0, 2); // first-player view
  player.position.set(0, 0, 0);
  player.rotation.y = - Math.PI / 2.0;

  VRcontrols = mobile ? new DeviceOrientationControls(camera) : new OrbitControls(camera);

  //LIGHTS
  const light = new THREE.PointLight(0xFFFFFF);
  light.position.set(0, 0, 9);
  light.castShadow = true;
  light.shadow.mapSize.width = 1024;
  light.shadow.mapSize.height = 1024;
  scene.add(light);

  //FLOOR
  const matFloor = new THREE.MeshPhongMaterial();
  const geoFloor = new THREE.BoxGeometry(2000, 1, 2000);
  const mshFloor = new THREE.Mesh(geoFloor, matFloor);

  matFloor.color.set(0x212E39);
  mshFloor.receiveShadow = true;
  mshFloor.position.set(0, - 1, 0);

  scene.add(mshFloor);


  const loader = new ColladaLoader();

  loader.load(`../assets/environment.dae`, collada => {
    collada.scene.traverse(child => {
      child.castShadow = true;
      child.receiveShadow = true;
    });
    scene.add(collada.scene);
    animate();
  });

};

// const makeCameraBox = () => {
//   const cameraBoxGeometry = new THREE.BoxGeometry(3, 3, 3);
//   const cameraBoxMaterial = new THREE.MeshBasicMaterial({color: 0x00ffff});
//   cameraBox = new THREE.Mesh(cameraBoxGeometry, cameraBoxMaterial);
//   cameraBox.geometry.computeBoundingBox();
//   const cameraBoxHelper = new THREE.BoxHelper(cameraBox, 0x0000ff);
//   cameraBoundingBox = new THREE.Box3(
//     cameraBox.geometry.boundingBox.min,
//     cameraBox.geometry.boundingBox.max
//   );
//   cameraBoundingBox.setFromObject(cameraBox);
//   // player.add(cameraBoundingBox);
//   scene.add(cameraBoxHelper);
//   scene.add(cameraBox);
// };

const makeTestCube = () => {
  testBox = new THREE.Mesh(
    new THREE.BoxGeometry(3, 3, 3),
    new THREE.MeshBasicMaterial({color: 0xFF0099})//PINK
  );
  testBox.position.set(0, 0, 20);
  testBoxBbox = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
  testBoxBbox.setFromObject(testBox);
  const testBoxHelper = new THREE.BoxHelper(testBoxBbox, 0xCC6600); //ORANGE
  testBox.geometry.computeBoundingBox();
  scene.add(testBox);
  scene.add(testBoxHelper);
};

const makeAnotherTestCube = () => {
  anotherTestBox = new THREE.Mesh(
    new THREE.BoxGeometry(3, 3, 3),
    new THREE.MeshBasicMaterial({color: 0x990000})//RED This is the small box in the giant box
  );
  anotherTestBox.position.set(0, 0, 30);
  anotherTestBoundingBox = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
  anotherTestBoundingBox.setFromObject(anotherTestBox);
  const anotherTestBoxHelper = new THREE.BoxHelper(anotherTestBox, 0xCC6600);// This is the giant box (Orange)
  anotherTestBox.geometry.computeBoundingBox();
  scene.add(anotherTestBox);
  scene.add(anotherTestBoxHelper);
};

const moveCamera = () => {
  // TODO: find movementX (0= no movement, 1-... = movement)
  //dan -> player.rotateY(rotateAngle * e.movementX * .1);
  const movementX = VRcontrols.object.rotation._y;
  const rotateAngle = Math.PI / 4;
  console.log(VRcontrols.deviceOrientation);

  //player.rotateY(rotateAngle * movementX * .1);
  // notes.forEach(i => {
  //   i.audioCtx.listener.positionX.value = camX + window.innerWidth / 2;
  //   i.audioCtx.listener.positionZ.value = camZ + 300;
  //   i.audioCtx.listener.positionY.value = camY + window.innerHeight / 2;
  // });
  //
  switch (controlData) {
  case `up`:
    camZ -= camSpeed;
    break;
  case `down`:
    camZ += camSpeed;
    break;
  case `left`:
    camX -= camSpeed;
    break;
  case `right`:
    camX += camSpeed;
    break;
  }

  camera.position.set(camX, camY, camZ);
  VRcontrols.update();
};

//CODE BENEATH NEEDS TO BE LESS DIRTY.

const useArrowDown = e => {
  switch (e.key) {
  case `z`:
    moveForward = true;
    break;
  case `s`:
    moveBackward = true;
    break;
  case `q`:
    moveLeft = true;
    break;
  case `d`:
    moveRight = true;
    break;
  }
};

const useArrowUp = e => {
  switch (e.key) {
  case `z`:
    moveForward = false;
    break;
  case `s`:
    moveBackward = false;
    break;
  case `q`:
    moveLeft = false;
    break;
  case `d`:
    moveRight = false;
    break;
  }
};


const animate = () => {

  if (mobile) {
    moveCamera();
  }

  if (moveForward) player.position.z -= 5;
  if (moveBackward) player.position.z += 5;
  if (moveLeft) player.position.x -= 5;
  if (moveRight) player.position.x += 5;

  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;

  renderer.gammaInput = true;
  renderer.gammaOutput = true;

  renderer.setClearColor(0xdddddd, 1);

  renderer.render(scene, camera);
  //
  // if (testBoundingBox.intersectsBox(cameraBoundingBox)) {
  //   console.log(`intersection`);
  // }

  requestAnimationFrame(animate);

};


init();

2 个答案:

答案 0 :(得分:0)

没有冒犯,你的变量名很糟糕。对于这里的内容,令人难以置信的困惑。

试试这个。改变这一行:

const anotherTestBoxHelper = new THREE.BoxHelper(anotherTestBox, 0xCC6600);

要:

const anotherTestBoxHelper = new THREE.BoxHelper(anotherTestBoundingBox, 0xCC6600);

答案 1 :(得分:0)

禁用这段代码为我修复了它,但了解原因。

renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;

renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setClearColor(0xdddddd, 1);