Guest User

Oleg Orlov

a guest
Mar 7th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var GC3D = { Version: '1.0.2' };
  4.  
  5. GC3D.Enums = {
  6.     renderer: {
  7.         id: 0,
  8.         type: THREE.WebGLRenderer,
  9.         antialias: true,
  10.         alpha: true,
  11.         canvasDomId: 'webgl-application'
  12.     },
  13.     camera: {
  14.         id: 1,
  15.         type: THREE.PerspectiveCamera,
  16.         fov: 45,
  17.         aspect: window.innerWidth / window.innerHeight,
  18.         near: 1,
  19.         far: 20000
  20.     },
  21.     light: {
  22.         id: 2,
  23.         type: THREE.PointLight,
  24.         color: 0xffff00
  25.     },
  26.     mapDefaultSettings: {
  27.         id: 3,
  28.         type: Object,
  29.         coordinates: {
  30.             x: 39604,
  31.             y: 20492,
  32.             zoom: 16
  33.         }
  34.     }
  35. };
  36.  
  37.  
  38. GC3D.Utils = function() {
  39.     this.renderer = undefined;
  40.     this.scene = undefined;
  41.     this.camera = undefined;
  42.    
  43.     this.init();
  44. };
  45. GC3D.Utils.prototype.init = function() {
  46.     this.initRender();
  47.     this.initScene();
  48.     this.initCamera();
  49.     this.initLight();
  50. };
  51. GC3D.Utils.prototype.initRender = function() {
  52.     var properties = GC3D.Enums.renderer;
  53.     this.renderer = new THREE.WebGLRenderer({
  54.         antialias: properties.antialias,
  55.         alpha: properties.alpha,
  56.         canvas: document.getElementById( properties.canvasDomId )
  57.     });
  58.    
  59.     this.renderer.setSize( window.innerWidth, window.innerHeight );
  60. };
  61. GC3D.Utils.prototype.renderScene = function() {
  62.     requestAnimationFrame( GC3D.Utils.prototype.renderScene.bind( this ) );
  63.     this.renderer.render( this.scene, this.camera );
  64.    
  65.     this.scene.children.filter(function( item ) {
  66.         if ( item instanceof THREE.Mesh ) {
  67.             item.rotation.x += 0.02;
  68.         }
  69.     });
  70. };
  71. GC3D.Utils.prototype.initScene = function() {
  72.     this.scene = new THREE.Scene();
  73. };
  74. GC3D.Utils.prototype.initCamera = function() {
  75.     var properties = GC3D.Enums.camera;
  76.     this.camera = new THREE.PerspectiveCamera(
  77.         properties.fov,
  78.         properties.aspect,
  79.         properties.near,
  80.         properties.far
  81.     );
  82.     this.scene.add( this.camera );
  83. };
  84. GC3D.Utils.prototype.setCameraPosition = function( newPosition ) {
  85.     this.camera.position.set( newPosition.x, newPosition.y, newPosition.z );
  86. };
  87. GC3D.Utils.prototype.setCameraLookAtView = function( object ) {
  88.     this.camera.lookAt( object );
  89. };
  90. GC3D.Utils.prototype.initLight = function() {
  91.     var properties = GC3D.Enums.light;
  92.     this.light = new THREE.PointLight( properties.color );
  93.     this.scene.add( this.light );
  94. };
  95. GC3D.Utils.prototype.setLightPosition = function( newPosition ) {
  96.     this.light.position.set( newPosition.x, newPosition.y, newPosition.z );
  97. };
  98.  
  99.  
  100. GC3D.Application = function() {
  101.     this.utils = new GC3D.Utils();
  102.    
  103.     this.run();
  104. };
  105. GC3D.Application.prototype.run = function() {
  106.     this.utils.setCameraPosition( { x: 3, y: 3, z: 3 } );
  107.     this.utils.setCameraLookAtView( this.utils.scene.position );
  108.     this.utils.setCameraPosition( { x: 10, y: 10, z: 10 } );
  109.     var cube = new THREE.Mesh(
  110.         new THREE.CubeGeometry( 3, 3, 3 ),
  111.         new THREE.MeshBasicMaterial({
  112.             map: THREE.ImageUtils.loadTexture( '/images/textures/box.jpg' )
  113.         })
  114.     );
  115.     this.utils.scene.add( cube );
  116.     this.utils.renderScene();
  117.     var worker = new Worker( "webworker.js" );
  118.     worker.postMessage( JSON.stringify( this.utils.scene ) );
  119. };
Advertisement
Add Comment
Please, Sign In to add comment