這是一篇關於怎麼樣基於three.js進行可配置的three.js的對象建立的文章
項目地址react
這是建立的一個Three.js基類其中包含了場景,相機,渲染器,控制器以及一些方法git
// VRcore.js import * as THREE from 'three'; const OrbitControls = require('three-orbit-controls')(THREE) let Scene, Camera, Renderer, Controls, loopID; function createScene({domContainer = document.body, fov = 50,far = 1000}){ if (!(domContainer instanceof HTMLElement)) { throw new Error('domContainer is not a HTMLElement!'); } // 初始化 scene Scene = new THREE.Scene(); // 初始化 camera Camera = new THREE.PerspectiveCamera(fov, domContainer.clientWidth / domContainer.clientHeight, 1, far); Camera.position.set( 200, 200, 200 ); Camera.lookAt(Scene.position); Scene.add(Camera); // 初始化 renderer Renderer = new THREE.WebGLRenderer({ canvas: domContainer, antialias: true, alpha: true } ); Renderer.clear(); Renderer.setClearColor( 0xeeeeee, 1); // 更改渲染器顏色 Renderer.setSize(domContainer.clientWidth, domContainer.clientHeight); Renderer.shadowMap.Enabled = true; Renderer.setPixelRatio(domContainer.devicePixelRatio); initVR(); } function initVR() { // 初始化控制器 Controls = new OrbitControls(Camera, Renderer.domElement);; Controls.addEventListener('change', render); Controls.enableZoom = true; } function render() { Renderer.render(Scene, Camera); } function renderStart(callback) { loopID = 0; // 記錄循環幾回,後面有與清除場景中的物體 if (loopID === -1) return; let animate = function(){ loopID = requestAnimationFrame(animate); callback(); Controls.update(); render(); } animate(); } // 暫停動畫渲染 function renderStop() { if (loopID !== -1) { window.cancelAnimationFrame(loopID); loopID = -1; } } // 回收當前場景 function clearScene() { for(let i = Scene.children.length - 1; i >= 0; i-- ) { Scene.remove(Scene.children[i]); } } // 清理頁面 function cleanPage() { renderStop(); clearScene(); } export { Scene, Camera, Renderer, Controls, createScene, initVR, renderStart, renderStop, clearScene, cleanPage }
這是一個VRpage的基類,全部須要建立Three項目都須要繼承這個類,而後生成一個Three項目github
// VRpage.js import * as THREE from 'three'; import * as VRcore from './VRcore.js'; export default class VRpage { constructor(options) { // 建立場景 VRcore.createScene(options); this.start(); this.loadPage(); } loadPage() { VRcore.renderStart(() => this.update()); this.loaded(); } initPage() { this.loadPage(); this.start(); } start() {} loaded() {} update() {} }
下面的文件是一個繼承了VRpage類的一個類,而後咱們重寫了start方法以及update方法,start方法中咱們向場景中添加了一個正方體,update方法是咱們給這個正方體的一個變形動畫,他會結合VRcore.js裏面的renderStart方法來進行動畫效果canvas
// page.js import * as THREE from 'three'; import VRpage from '../../utils/VRpage.js'; import * as VRcore from '../../utils/VRcore.js'; export default class Page extends VRpage { start() { // 啓動渲染以前,建立場景3d模型 let geometry = new THREE.CubeGeometry(100,100,100); let material = new THREE.MeshLambertMaterial( { color:0x0000ff} ); this.box = new THREE.Mesh(geometry,material); this.box.position.set(3, -2, -3); const PointLight = new THREE.PointLight(0xffffff); PointLight.position.set(500, 500, 500); const AmbientLight = new THREE.AmbientLight( 0x404040 ); // soft white light VRcore.Scene.add(PointLight); VRcore.Scene.add(AmbientLight); VRcore.Scene.background = new THREE.Color( 0xeeeeee ); // 更改場景背景色 VRcore.Scene.add(this.box); } update() { this.box.rotation.y += 0.01; } }
這裏我使用的是react的框架框架
// index.js import React, { Component } from 'react'; import PropTypes from 'prop-types'; import Page from './Page.js'; class Oho extends Component { constructor() { super(); this.init = this.init.bind(this); } componentDidMount() { const dom = document.querySelector('#Oho'); this.init(dom); } init(dom) { const page = new Page({domContainer: dom}); } render() { return ( <div className="three-demo"> <canvas id="Oho" ref="camera"></canvas> </div> ); } } export default Oho;