Three.js實(shí)現(xiàn)3D機(jī)房效果
3D機(jī)房系統(tǒng)是最近用戶的需求,通過相關(guān)了解最后使用Three.js,也發(fā)現(xiàn)最近有東西可以寫出來分享:
- webGL可以讓我們?cè)赾anvas上實(shí)現(xiàn)3D效果。而three.js是一款webGL框架,由于其易用性被廣泛應(yīng)用
- Three.js是通過對(duì)WebGL接口的封裝與簡(jiǎn)化而形成的一個(gè)易用的圖形庫
分步實(shí)現(xiàn)3D效果
- 初始化3D模型參數(shù)
- 開始搭建場(chǎng)景
- 初始化渲染器
- 初始化攝像機(jī)
- 創(chuàng)建場(chǎng)景
- 燈光布置
- 創(chuàng)建網(wǎng)格線
- 循環(huán)渲染界面
- 創(chuàng)建鼠標(biāo)控制器
- 添加對(duì)象到場(chǎng)景中
一 . 初始化3D模型參數(shù)
//參數(shù)處理 this.option = new Object(); this.option.antialias = option.antialias || true; this.option.clearCoolr = option.clearCoolr || 0x1b7ace; this.option.showHelpGrid = option.showHelpGrid || false; //對(duì)象 this.id = id; this.width = width(); this.height = height(); this.renderer = null;//渲染器 this.scene = null;//場(chǎng)景 this.camera = null;//攝像機(jī) this.selected=null; this.objects = []; this.mouseClick = new THREE.Vector2(); this.raycaster = new THREE.Raycaster(); this.controls = null;//鼠標(biāo)控制器 this.trsnaformControls = null;//鼠標(biāo)控制器 this.dragcontrols = null; this.objList = json.objects;//對(duì)象列表 this.eventList = json.events;//事件對(duì)象列表 this.dragList = []; this.objectStatusList = {}; this.clickList = []; var that = this;
對(duì)于一些需要使用的參數(shù),開始加載進(jìn)行初始化操作。
二 . 開始搭建場(chǎng)景
搭建場(chǎng)景包含一些具體的初始化操作 一些初始化方法(之后才對(duì)具體方法加以說明):
var that = this; room3dObj = that; that.initThree(that.id); //初始化渲染器 that.initCamera(); //初始化攝像機(jī) that.initScene();//創(chuàng)建場(chǎng)景 that.initHelpGrid();//創(chuàng)建網(wǎng)格 that.initLight();//燈光布置 //添加3D對(duì)象 $.each(that.objList, function (index,obj) { that.InitAddObject(obj);//添加對(duì)象到場(chǎng)景中 }); that.initMouseCtrl();//創(chuàng)建鼠標(biāo)控制器 that.animation();//循環(huán)渲染界面
三 . 初始化渲染器
渲染器 WebGLRenderer 定義語法:
var that = this; that.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: that.option.antialias }); that.renderer.setSize(that.width, that.height); $(“#” + that.id).append(that.renderer.domElement); that.renderer.setClearColor(that.option.clearCoolr, 1.0); that.renderer.shadowMap.enabled = true; that.renderer.shadowMapSoft = true; //事件 that.renderer.domElement.addEventListener(‘mousedown',that.onDocumentMouseDown, false); that.renderer.domElement.addEventListener(‘mousemove',that.onDocumentMouseMove, false);
四 . 初始化攝像機(jī)
采用PerspectiveCamera 相機(jī):
var that = this; that.camera = new THREE.PerspectiveCamera(45, that.width / that.height, 1, 100000); that.camera.name = 'mainCamera'; that.camera.position.x =0; that.camera.position.y =2000; that.camera.position.z =1800; that.camera.up.x = 0; that.camera.up.y =1; that.camera.up.z =0; that.camera.lookAt({ x: 100, y: 0, z: 100 }); that.objects.push(that.camera); that.dragList.push(that.camera); that.clickList.push(that.camera);
五 . 創(chuàng)建場(chǎng)景
var that = this; that.scene = new THREE.Scene();
六 . 燈光布置
/* AmbientLight: 環(huán)境光,基礎(chǔ)光源,它的顏色會(huì)被加載到整個(gè)場(chǎng)景和所有對(duì)象的當(dāng)前顏色上。 PointLight:點(diǎn)光源,朝著所有方向都發(fā)射光線 SpotLight :聚光燈光源:類型臺(tái)燈,天花板上的吊燈,手電筒等 DirectionalLight:方向光,又稱無限光,從這個(gè)發(fā)出的光源可以看做是平行光. */ var that = this; var light = new THREE.AmbientLight(0xcccccc); light.position.set(0, 0,0); that.scene.add(light); var light2 = new THREE.PointLight(0x555555); light2.shadow.camera.near =1; light2.shadow.camera.far = 5000; light2.position.set(0, 350, 0); light2.castShadow = true;//表示這個(gè)光是可以產(chǎn)生陰影的 that.scene.add(light2);
七 . 創(chuàng)建網(wǎng)格
var that = this; if (that.option.showHelpGrid) { var helpGrid = new THREE.GridHelper(1000, 50); that.scene.add(helpGrid); }
八 . 循環(huán)渲染界面
var that = room3dObj; if (TWEEN != null && typeof (TWEEN) != ‘undefined') { TWEEN.update(); } requestAnimationFrame(that.animation); that.renderer.render(that.scene, that.camera);
九 . 創(chuàng)建鼠標(biāo)控制器
var that = this; that.controls = new THREE.OrbitControls(that.camera,that.renderer.domElement); that.controls.addEventListener(‘change', that.updateControls);
十 . 添加對(duì)象到場(chǎng)景中
var that = room3dObj; that.scene.add(obj); that.objects.push(obj);
最后效果
瀏覽器兼容
- 目前,本編輯器對(duì)Chrome瀏覽器支持最為完整。建議大家使用較新版本的Chrome.
- IE11以下不支持
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Three.js實(shí)現(xiàn)簡(jiǎn)單3D房間布局
- Three.js的使用及繪制基礎(chǔ)3D圖形詳解
- three.js實(shí)現(xiàn)3D模型展示的示例代碼
- Three.js開發(fā)實(shí)現(xiàn)3D地圖的實(shí)踐過程總結(jié)
- 利用three.js畫一個(gè)3D立體的正方體示例代碼
- three.js實(shí)現(xiàn)炫酷的全景3D重力感應(yīng)
- three.js中3D視野的縮放實(shí)現(xiàn)代碼
- three.js實(shí)現(xiàn)3D視野縮放效果
- vue頁面引入three.js實(shí)現(xiàn)3d動(dòng)畫場(chǎng)景操作
- three.js如何實(shí)現(xiàn)3D動(dòng)態(tài)文字效果
相關(guān)文章
JS實(shí)現(xiàn)的仿東京商城菜單、仿Win右鍵菜單及仿淘寶TAB特效合集
這篇文章主要介紹了JS實(shí)現(xiàn)的仿東京商城菜單、仿Win右鍵菜單及仿淘寶TAB特效合集,以實(shí)例形式較為詳細(xì)的分析了JavaScript實(shí)現(xiàn)動(dòng)態(tài)添加下拉菜單及響應(yīng)鼠標(biāo)事件生成菜單等實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-09-09獲取div編輯框,textarea,input text的光標(biāo)位置 兼容IE,F(xiàn)F和Chrome的方法介紹
獲取div編輯框,textarea,input text的光標(biāo)位置 兼容IE,F(xiàn)F和Chrome的方法介紹,有需求的朋友可以參考2012-11-11js動(dòng)態(tài)創(chuàng)建標(biāo)簽示例代碼
這篇文章主要以示例的方式為大家介紹下js如何動(dòng)態(tài)創(chuàng)建標(biāo)簽,需要的朋友可以參考下2014-06-06小程序從手動(dòng)埋點(diǎn)到自動(dòng)埋點(diǎn)的實(shí)現(xiàn)方法
這篇文章主要介紹了小程序從手動(dòng)埋點(diǎn)到自動(dòng)埋點(diǎn)的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01