- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚发现 ThreeJS,并尝试使用 ThreeJS 的 OBJLoader 加载 .obj 文件。
不幸的是,我收到错误
Cannot read property 'scene' of undefined
此错误是在 this.scene.add( object );
这是我的完整组件:
import { Component, OnInit } from '@angular/core';
import * as THREE from 'three';
import * as OBJLoader from 'three-obj-loader';
OBJLoader(THREE);
@Component({
selector: 'app-items-catalog',
templateUrl: './items-catalog.component.html',
styleUrls: ['./items-catalog.component.css']
})
export class ItemsCatalogComponent implements OnInit {
constructor() {
}
private canEleId = 'renderCanvas';
ngOnInit() {
this.createScene(this.canEleId);
this.animate();
}
canvas: HTMLCanvasElement;
private renderer: THREE.WebGLRenderer;
private camera: THREE.PerspectiveCamera;
private scene: THREE.Scene = new THREE.Scene();
private light: THREE.AmbientLight;
private cube: THREE.Mesh;
createScene(elementId: string): void {
//var scene = new THREE.Scene();
// The first step is to get the reference of the canvas element from our HTML document
this.canvas = <HTMLCanvasElement>document.getElementById(elementId);
this.renderer = new THREE.WebGLRenderer({
canvas: this.canvas,
alpha: true, // transparent background
antialias: true // smooth edges
});
this.renderer.setSize(window.innerWidth, window.innerHeight);
// create the scene
this.camera = new THREE.PerspectiveCamera(
75, window.innerWidth / window.innerHeight, 0.1, 1000
);
this.camera.position.z = 5;
this.scene.add(this.camera);
// soft white light
this.light = new THREE.AmbientLight( 0x404040 );
this.light.position.z = 10;
this.scene.add(this.light);
let geometry = new THREE.BoxGeometry(1, 1, 1);
let material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
this.cube = new THREE.Mesh( geometry, material );
//this.scene.add(this.cube);
/* HERE IS THE PART WHERE THE PROBLEM IS */
-----------------------------------------------------------------
const objLoader = new THREE.OBJLoader();
objLoader.setPath('../../assets/icon/threed/')
// objLoader
// var loader = new THREE.OBJLoader();
objLoader.load(
// resource URL
'helicopter.obj',
// called when resource is loaded
function ( object ) {
console.log(object);
object.position.y -= 60;
this.scene.add( object );
},
// called when loading is in progresses
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened' );
}
);
---------------------------------------------------------------
}
animate(): void {
window.addEventListener('DOMContentLoaded', () => {
this.render();
});
window.addEventListener('resize', () => {
this.resize();
});
}
render() {
requestAnimationFrame(() => {
this.render();
});
this.cube.rotation.x += 0.01;
this.cube.rotation.y += 0.01;
this.renderer.render(this.scene, this.camera);
}
resize() {
let width = window.innerWidth;
let height = window.innerHeight;
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.renderer.setSize( width, height );
}
}
提前感谢您的帮助
最佳答案
这是因为objLoader.load
回调函数中的this
引用的是onload函数,而不是类实例。
解决此问题的一种方法是将 this
实例化为 createScene
函数中的变量,然后在加载函数中使用该变量而不是 this:
createScene(elementId: string): void {
let me = this;
...
objLoader.load(
// resource URL
'helicopter.obj',
// called when resource is loaded
function ( object ) {
console.log(object);
object.position.y -= 60;
me.scene.add( object );
},
...
);
}
另一种方法是使用箭头函数,其中 this
将引用 ItemsCatalogComponent
类:
objLoader.load(
// resource URL
'helicopter.obj',
// called when resource is loaded
( object ) => {
console.log(object);
object.position.y -= 60;
this.scene.add( object );
},
// called when loading is in progresses
( xhr ) => {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
( error ) => {
console.log( 'An error happened' );
}
);
希望有所帮助。
关于angular - ThreeJS/Angular - "scene is undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207186/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!