2 回答
TA贡献1786条经验 获得超11个赞
首先使用npm安装 three.js
npm i three
然后在组件中导入它,如下所示
import * as THREE from 'three';
现在您可以像下面的代码一样从您的组件中使用它。注意:这只是举例
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
var animate = function () {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
在 package.json 文件中将 typescript 更新到最新版本(3.7.2)
"devDependencies": {
...
"typescript": "3.7.2"
}
由于您在 tsconfig.json 文件中使用 Angular 7,请禁用 TypeScript 版本检查。如果您使用的是最新的 Angular 版本,则无需执行此操作。
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"disableTypeScriptVersionCheck": true,
}
}
TA贡献1801条经验 获得超16个赞
如果不需要包含,则在角度组件中包含threejs。您可以在 index.html 下面的代码演示中包含threejs脚本ThreeJS Angular Demo
<body>
<app-root></app-root>
<script src="https://threejs.org/build/three.js"></script>
<script>
<!-- Your threejs script that creates and animates the scene --->
<!-- Note: skip the import threejs module import statement --->
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75,
window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
var animate = function () {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
</script>
</body>
添加回答
举报