如果是为了写一个demo可以直接在.html文件中编写threejs代码,前端工程化开发,肯定需要在nodejs搭建的开发环境下编程,比如在vue脚手架vue-cli中使用threejs,自然需要通过npm等工具安装threejs模块。
threejs官方文档介绍:Title
npm安装three.js依赖
npm install three --save-dev
yarn安装three.js依赖
yarn add three --dev
安装具体的Three.js版本
调用模块require('three')
,变量名为THREE
,可以保持.html中threejs类的书写习惯,如果使用Th
表示,调用矩形平面几何体API的方式是Th.PlaneGeometry
var THREE = require('three');
// 创建一个几何体
var geo = new THREE.PlaneGeometry(105,209)
nodejs安装好threejs后,通过import方式使用three.js库
// 引入three.js,获得threejs库全部API
import * as THREE from 'three';
// 和在.html文件中一样使用threejs类
...
var geo = new THREE.PlaneGeometry(105,209)
...
var renderer = new THREE.WebGLRenderer({
antialias: true
});
import方式获得threejs具体的类
import { Scene } from 'three';
const scene = new Scene();