gpt4 book ai didi

javascript - OrbitControls 不是构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 16:15:11 34 4
gpt4 key购买 nike

我已经尝试创建一个 WebVR 环境一个多星期了,我的场景是用三个 js 渲染的,但我似乎无法使 VR 控件工作。

我已经尝试过:

在我的node_modules中添加包并导入它们:

import threeOrbitControls from 'three-orbit-controls';
const OrbitControls = threeOrbitControls(THREE);
const controls = new THREE.OrbitControls(camera, element);

但这会引发错误:

Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_1_three__.OrbitControls is not a constructor(…)

我尝试导入的其他一些模块表示模块内未定义 THREE,因为该函数以 THREE 开头。,但我无法进入该模块并仅导入 THREE,因为如果其他人运行npm install 他们仍然会得到相同的错误。

我尝试在我的index.html中添加脚本(和正在下载的源代码),但还有三个未定义...

我无法猜测。我认为这是因为 webpack,并查找了 WebVR webpack 存储库以了解该人是如何处理它的。我发现this repository但是当安装包并运行 webpack 时,它说 WebVRManager 不是构造函数。老实说,我不知道这里出了什么问题。

这是我用来运行我的项目的代码(现在相当困惑)。

import {sets} from './data/';

import * as THREE from 'three';
import threeOrbitControls from 'three-orbit-controls';
import ColladaLoader from 'three-collada-loader';
// import 'three-vrcontrols';
// import 'three-vreffect';
import threeStereoEffect from 'three-stereo-effect';

// import FirstPersonControls from 'three-first-person-controls';

// import DeviceOrientationControls from './modules/util/DeviceOrientationControls';

import webvrPolyfill from 'webvr-polyfill'; // eslint-disable-line no-unused-vars

// console.log(DeviceOrientationControls);

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

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

const OrbitControls = threeOrbitControls(THREE);
const StereoEffect = threeStereoEffect(THREE);

// const VRControls = new THREE.VRControls(camera);
// const VREffect = new THREE.VREffect();

let scene, camera, renderer, element, container, stereoEffect, vreffect, controls;
let audioCtx, bufferLoader;

let controlData;

let camX = 0;
const camY = 0;
let camZ = 2;

const camSpeed = .1;

const notes = [];
let devices = [];

const init = () => {

this.socket = io(`/`);

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

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

};

const handleWSInit = users => {
const {id: socketId} = this.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(this.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, false);
notes.push(bol);
}

// console.log(notes);
};

const initEnvironment = () => {

//Three.js Scene
scene = new THREE.Scene();


//Create renderer, set size + append to the container
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
element = renderer.domElement;
container = document.querySelector(`main`);
container.appendChild(element);


//Create camera, set position + add to scene
camera = new THREE.PerspectiveCamera(
45, window.innerWidth / window.innerHeight,
1, 10000
);
camera.position.set(0, 0, 2);
camera.lookAt(scene.position);


//VR Controls
// temporaryControls = new VRControls(camera);

// vreffect = new VREffect(renderer);
// vreffect.setSize(window.innerWidth, window.innerHeight);
//
// navigator.getVRDisplays().then(displays => {
// if (displays.length > 0) {
// const vrDisplay = displays[0];
// console.log(`jipse`);
// }
// //Kick off the render loop
// });




//Creates stereo effect
stereoEffect = new StereoEffect(renderer);
// stereoEffect.eyeSeparation = 15;
// stereoEffect.setSize(window.innerWidth, window.innerHeight);

console.log(stereoEffect);

//Controls
// new OrbitControls(camera);
controls = new THREE.OrbitControls(camera, element);
// camera.position.x = 100;
// camera.position.y = 1000;
// camera.position.z = 3000;


//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;
light.shadow.camera.near = 10;
light.shadow.camera.far = 100;
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);


//ENVIRONMENT
const loader = new ColladaLoader();

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

scene.add(collada.scene);
render();
});

};


// function setOrientationControls(e) {
// if (!e.alpha) {
// return;
// }
// controls = new THREE.DeviceOrientationControls(camera, true);
// controls.connect();
// controls.update();
// element.addEventListener(`click`, fullscreen, false);
// window.removeEventListener(`deviceorientation`, setOrientationControls, true);
// }
// window.addEventListener(`deviceorientation`, setOrientationControls, true);

const moveCamera = () => {
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);
};

const render = () => {

moveCamera();
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;

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

renderer.setClearColor(0xdddddd, 1);
stereoEffect.render(scene, camera);

requestAnimationFrame(render);
};

// const fullscreen = () => {
// if (container.requestFullscreen) {
// container.requestFullscreen();
// } else if (container.msRequestFullscreen) {
// container.msRequestFullscreen();
// } else if (container.mozRequestFullScreen) {
// container.mozRequestFullScreen();
// } else if (container.webkitRequestFullscreen) {
// container.webkitRequestFullscreen();
// }
// };

init();

最佳答案

我下载了源代码,通过执行 require(./OrbitControls.js) 来获取它,并在源代码中添加了 module.exports = THREE.OrbitControls

关于javascript - OrbitControls 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41132839/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com