简体   繁体   English

Angular-CLI 和 ThreeJS

[英]Angular-CLI & ThreeJS

I have been trying to add the proper npm dependencies to add THREE to my Angular-CLI project.我一直在尝试添加适当的 npm 依赖项,以将三个添加到我的 Angular-CLI 项目中。 With CLI changing so rapidly in the past few months, I haven't been able to find working source.由于 CLI 在过去几个月中变化如此之快,我一直无法找到工作源。

Here are a few ideas...这里有一些想法......

  • Piggyback with scripts背负脚本

    This was my first attempt, to simply add <script src="three.js"></script> to the index.html file.这是我的第一次尝试,简单地将<script src="three.js"></script>到 index.html 文件中。 However I'm having trouble working with the javascript in the typescript interface.但是,我在打字稿界面中使用 javascript 时遇到问题。

  • Webpack网络包

    This was my second attempt, but I ran into several documentation problems.这是我的第二次尝试,但我遇到了几个文档问题。 Angular-cli doesn't seem to have a consistent way of using webpacks. Angular-cli 似乎没有一致的使用 webpack 的方式。 There are four different ways of implementing webpacks.有四种不同的方式来实现 webpack。 I wasn't able to get any working with THREE.我无法与三一起工作。

  • Bundles捆绑

    This seems like a hack, and a poor/lengthy one.这似乎是一个 hack,而且是一个糟糕的/冗长的。 It would be to add bundles to the THREE library so that it can be interpreted angular 2.将捆绑包添加到三个库中,以便可以将其解释为角度 2。

I'm still currently working on making a Angluar-CLI + THREE.js project.我目前仍在制作一个 Angluar-CLI + THREE.js 项目。 If I don't see progress in the next week, I may drop angular-cli.如果我在下周看不到进展,我可能会放弃 angular-cli。 Any advice/sources would be greatly appreciated.任何建议/来源将不胜感激。

As of angular-cli 1.0.0 :从 angular-cli 1.0.0

  1. npm install three --save
  2. npm install @types/three --save-dev
  3. Add a div element in AppComponent.html: <div #rendererContainer></div>在 AppComponent.html 中添加一个 div 元素: <div #rendererContainer></div>
  4. Import three.js in AppComponent.ts: import * as THREE from 'three';在 AppComponent.ts 中import * as THREE from 'three'; three.js: import * as THREE from 'three';
  5. Get a handle to your div element with @ViewChild('rendererContainer') rendererContainer: ElementRef;使用@ViewChild('rendererContainer') rendererContainer: ElementRef;获取 div 元素的句柄@ViewChild('rendererContainer') rendererContainer: ElementRef;
  6. Do the necessary setup in your constructor / lifecycle methods.在构造函数/生命周期方法中进行必要的设置。 Note: the ViewChild is not available until ngAfterViewInit .注意: ViewChild直到ngAfterViewInit

Full AppComponent:完整的应用组件:

import {Component, ViewChild, ElementRef} from '@angular/core';
import * as THREE from 'three';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    @ViewChild('rendererContainer') rendererContainer: ElementRef;

    renderer = new THREE.WebGLRenderer();
    scene = null;
    camera = null;
    mesh = null;

    constructor() {
        this.scene = new THREE.Scene();

        this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
        this.camera.position.z = 1000;

        const geometry = new THREE.BoxGeometry(200, 200, 200);
        const material = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
        this.mesh = new THREE.Mesh(geometry, material);

        this.scene.add(this.mesh);
    }

    ngAfterViewInit() {
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.rendererContainer.nativeElement.appendChild(this.renderer.domElement);
        this.animate();
    }

    animate() {
        window.requestAnimationFrame(() => this.animate());
        this.mesh.rotation.x += 0.01;
        this.mesh.rotation.y += 0.02;
        this.renderer.render(this.scene, this.camera);
    }
}

Full AppComponent.html:完整的 AppComponent.html:

<div #rendererContainer></div>

If you want to use some of the additional scripts:如果你想使用一些额外的脚本:

You may end up wanting to use some of the additional scripts, such as loaders and controls.您可能最终想要使用一些额外的脚本,例如加载程序和控件。 Most of these haven't been written as modules, but instead add functionality to the THREE namespace on the window when loaded.其中大部分还没有被编写为模块,而是在加载时向window上的THREE命名空间添加功能。 Because of this I ended up telling the angular-cli to just load up my scripts manually by adding the following to my .angular-cli.json file:因此,我最终告诉 angular-cli 通过将以下内容添加到我的.angular-cli.json文件来手动加载我的脚本:

{
  "apps": [
    {
      "scripts": [
        "../node_modules/tween.js/src/Tween.js",
        "../node_modules/three/build/three.js",
        "../node_modules/stats.js/build/stats.min.js",
        "../vendor/VRMLLoader.js",
        "../vendor/OrbitControls.js"
      ],
      ...

Note that you will also need to deal with the fact that your three.js @types file does not define any types for these additional scripts.请注意,您还需要处理这样一个事实,即您的three.js @types 文件没有为这些附加脚本定义任何类型。 Ideally I would like to extend the existing type definitions, but for the time being I am just foregoing type hinting for three.js to get around the errors by adding declare const THREE: any to the top of my files that use three.js.理想情况下,我想扩展现有的类型定义,但目前我只是通过在我使用three.js 的文件的顶部添加declare const THREE: any来避免对three.js 进行类型提示以解决错误。 If you find a good way to instead extend the existing definitions from @types/three , please report back!如果您找到了一种好方法来扩展来自@types/three的现有定义,请反馈!

To handle resizing the window:要处理调整窗口大小:

While I am at it I will also mention that resizing your window can cause things like raycasting (which I use to decide if an object is clicked) to not work correctly anymore.在此期间,我还会提到调整window大小会导致光线投射(我用它来决定是否单击对象)之类的事情不再正常工作。 To fix this just do:要解决这个问题,只需执行以下操作:

@HostListener('window:resize', ['$event'])
onWindowResize(event) {
    this.renderer.setSize(event.target.innerWidth, event.target.innerHeight)
}

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

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