简体   繁体   English

无法解析 PointerLockControls 的所有参数

[英]Cannot resolve all parameters for PointerLockControls

I've started to learn three.js a couple of days ago and want to combine it with the framework Angular.几天前我开始学习three.js,想把它和框架Angular结合起来。 Everything up to this point works fine, but the PointerLockControls service won't do properly.到目前为止一切正常,但 PointerLockControls 服务无法正常运行。

Maybe I am missing something, but I am not sure what exactly.也许我错过了一些东西,但我不确定到底是什么。

I've installed three.js via NPM, thought I might also include this one.我已经通过 NPM 安装了 three.js,我想我可能也包括这个。

I'll append my code with which I am currently experimenting.我将 append 我目前正在试验的代码。
Anything else is untouched, the app component is the only one with "custom" logic.其他任何东西都没有改变,应用程序组件是唯一具有“自定义”逻辑的组件。

import {Component, Inject, OnInit} from '@angular/core';
import * as THREE from 'three';
import {BufferGeometry, Material, Object3D, PerspectiveCamera, Scene, WebGLRenderer} from 'three';
import {WINDOW} from '@typescript/window-injection.token';
import {DOCUMENT} from '@angular/common';
import {PointerLockControls} from 'three/examples/jsm/controls/PointerLockControls';

export const STYLESHEETS_SRC: string = '../stylesheets/css/';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: [STYLESHEETS_SRC + 'app.component.min.css'],
  providers: [Scene, PerspectiveCamera, WebGLRenderer, PointerLockControls]
})
export class AppComponent implements OnInit {
  private sensitivity: number = .02;

  constructor(
    @Inject(DOCUMENT) private document: Document,
    @Inject(WINDOW) private window: Window,
    private scene: Scene = new THREE.Scene(),
    private camera: PerspectiveCamera = new THREE.PerspectiveCamera(),
    private renderer: WebGLRenderer = new THREE.WebGLRenderer(),
    private pointerLock: PointerLockControls
  ) {
    camera.fov = 75;
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.near = .1;
    camera.far = 1000;
    camera.position.z = 5;

    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    renderer.domElement.addEventListener('mousemove', event => {
      if (event.button == 0) {
        camera.position.y -= event.movementY * this.sensitivity;
        camera.position.x += event.movementX * this.sensitivity;
      } else if (event.button == 2) {
        camera.quaternion.y -= event.movementX * this.sensitivity / 10;
        camera.quaternion.x += event.movementY * this.sensitivity / 10;
      }
    });

    this.pointerLock = new PointerLockControls(camera, renderer.domElement);
    pointerLock.lock();

    scene.add(new THREE.AxesHelper(5));
  }

  ngOnInit() {
    let geometry: BufferGeometry = new THREE.BoxGeometry(1, 1, 1);
    let material: Material = new THREE.MeshBasicMaterial({color: 0x00ff00});
    let cube: Object3D = new THREE.Mesh(geometry, material);
    this.scene.add(cube);

    let animate = () => {
      requestAnimationFrame(animate);

      cube.rotation.x += 0.01;
      cube.rotation.y += 0.01;

      this.renderer.render(this.scene, this.camera);
    };

    animate();
  }
}

I'd appreciate any help!我会很感激任何帮助!
If any information is missing, tell me, and I'll try to add it to this post asap.如果缺少任何信息,请告诉我,我会尽快将其添加到此帖子中。

Your component is created via Dependency injection .您的组件是通过Dependency injection创建的。

PointerLockControls can't be in the constructor. PointerLockControls不能在构造函数中。 You will need to create you instance youself.您将需要自己创建实例。

Something like that:像这样的东西:

const controls = new PointerLockControls( camera, document.body );

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

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