Path: blob/main/projects/HexGL/bkcore/threejs/Preloader.js
4627 views
/*!1* @class bkcore.threejs.Preloader2*3* Displays a small 3D preloader scene4*5* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>6*/78/*!9* @package bkcore.threejs10*/11var bkcore = bkcore || {};12bkcore.threejs = bkcore.threejs || {};1314/**15* Creates a new preloader scene.16* You have to update Preloader.ratio with the % loaded info (float 0.0-1.0)17* @param {Object{width, height, scale, line}} opts18*/19bkcore.threejs.Preloader = function(opts)20{21this.document = opts.document || document;2223this.end = false;2425this.time = 0.0;26this.y = 0.3;27this.ratio = 0.0;2829this.height = opts.height;30this.width = opts.width;3132this.scale = opts.scale == undefined ? 10 : opts.scale33this.line = opts.line == undefined ? 3 : opts.line;3435this.container = opts.container;3637this.renderer = new THREE.CanvasRenderer({38clearColor: 0xffffff39});40this.renderer.setSize( opts.width, opts.height );4142this.container.appendChild( this.renderer.domElement );4344this.ctx = this.renderer.domElement.getContext('2d');45this.ctx.textAlign = "center";4647this.scene = new THREE.Scene();4849this.camera = new THREE.PerspectiveCamera( 70, opts.width / opts.height, 1, 1000 );50this.camera.position.z = 100;51this.scene.add( this.camera );5253this.stage = new THREE.Object3D();54this.stage.position.set(0,10,0);55this.scene.add(this.stage);5657this.cube = new THREE.Mesh( new THREE.CubeGeometry( this.scale, this.scale, this.scale, 1, 1, 1 ),58new THREE.MeshBasicMaterial( { color: 0x999999 } ) );5960this.cube.scale.set(0.0,0.0,0.0);61this.stage.add(this.cube);6263this.cubew = new THREE.Mesh( new THREE.CubeGeometry( this.scale, this.scale, this.scale, 1, 1, 1 ),64new THREE.MeshBasicMaterial( {65wireframe: true,66wireframeLinewidth: this.line,67//wireframeLinecap: 'square',68//wireframeLinejoin: 'square',69color: 0xffffff70} ) );71this.cube.add(this.cubew);7273this.outercube = new THREE.Mesh( new THREE.CubeGeometry( this.scale, this.scale, this.scale, 1, 1, 1 ),74new THREE.MeshBasicMaterial( {75wireframe: true,76wireframeLinewidth: this.line,77//wireframeLinecap: 'square',78//wireframeLinejoin: 'square',79color: 0x0093d880} ) );81this.stage.add(this.outercube);8283var self = this;8485function raf()86{87if(!self.end)88{89requestAnimationFrame( raf );90self.render();91}92}93raf();9495function mm(e){96self.mouseMove.call(self, e);97}9899this.mmsave = mm;100101this.document.addEventListener( 'mousemove', mm, false );102}103104/**105* Render method to be called from a RAF loop106*/107bkcore.threejs.Preloader.prototype.render = function()108{109this.time += 0.02;110111this.ctx.clearRect(0 , 0 , this.width , this.height);112113var s = (this.ratio - this.cube.scale.x) * 0.3;114115this.cube.scale.addScalar(s);116this.cube.rotation.y = this.time;117this.outercube.rotation.y = this.time;118119this.stage.rotation.x += (this.y - this.stage.rotation.x)*0.3;120121this.renderer.render( this.scene, this.camera );122123this.ctx.save();124this.ctx.font = "40px Arial";125this.ctx.fillStyle = "rgb(200, 200, 200)";126this.ctx.fillText(Math.round(this.ratio*100), this.width/2, this.height/2+30);127this.ctx.restore();128}129130bkcore.threejs.Preloader.prototype.mouseMove = function(event)131{132var h2 = this.height/2;133this.y = -(event.clientY - h2)/h2+0.3;134}135136/**137* Deletes the Preloader138*/139bkcore.threejs.Preloader.prototype.remove = function()140{141this.document.removeEventListener( 'mousemove', this.mm, false );142this.end = true;143this.renderer = null;144this.scene = null;145this.stage = null;146this.cube = null;147this.cubew = null;148this.innercube = null;149this.container.innerHTML = "";150}151152