Guest User

Untitled

a guest
Jan 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. /**
  2. * Good basic starting point for a THREE.JS sketch
  3. */
  4. (function(){
  5. var Sketch = function(){
  6. this.onBeforeStart();
  7. };
  8.  
  9. Sketch.prototype.onBeforeStart = function() {
  10. var that = this;
  11. var onContentLoaded = function(){
  12. window.removeEventListener('DOMContentLoaded', onContentLoaded);
  13. that.onLoaded();
  14. };
  15. window.addEventListener('DOMContentLoaded', onContentLoaded, true);
  16. };
  17.  
  18. Sketch.prototype.onLoaded = function() {
  19. var width = window.innerWidth - 10;
  20. var height = window.innerHeight - 10;
  21.  
  22. this._domElement = document.getElementById("threecontainer");
  23. this._domElement.style.width = width + "px";
  24. this._domElement.style.height = height + "px";
  25. this._scene = new THREE.Scene();
  26.  
  27. this._renderer = new THREE.WebGLRenderer({antialias: true});
  28.  
  29. this._renderer.autoClear = true;
  30. this._renderer.sortObjects = true;
  31. this._renderer.setClearColor(new THREE.Color(0x111111), 1);
  32. this._renderer.setSize( width, height );
  33. this._renderer.domElement.tabIndex = "1";
  34. this._domElement.appendChild(this._renderer.domElement);
  35.  
  36. this._camera = new THREE.Camera( 65, width/height, 1, 5000 );
  37. this._camera.position.y = 0;
  38. this._camera.position.z = 50;
  39.  
  40. this._ambientLight = new THREE.AmbientLight(0x111111);
  41. this._scene.addLight(this._ambientLight);
  42.  
  43. this._pointLight = new THREE.PointLight(0xFF00FF, 0.5, 0);
  44. this._pointLight.position.set(0, 10, 10);
  45. this._scene.addLight(this._pointLight);
  46.  
  47. this.setupStats();
  48.  
  49. var geometry = new THREE.CubeGeometry( 5, 5, 5, 2, 2, 2 );
  50. this._cube = new THREE.Mesh( geometry, [new THREE.MeshLambertMaterial( {
  51. color: 0xFF0000,
  52. shading: THREE.SmoothShading
  53. })] );
  54. this._cube.position.set(0, 0,0);
  55.  
  56. this._scene.addObject( this._cube );
  57.  
  58. var that = this;
  59. (function loop() {
  60. that.update();
  61. that.render();
  62. window.requestAnimationFrame(loop);
  63. })();
  64. };
  65.  
  66. /**
  67. * Creates a Stats.js instance and adds it to the page
  68. */
  69. Sketch.prototype.setupStats = function() {
  70. var container = document.createElement('div');
  71. this.stats = new Stats();
  72. this.stats.domElement.style.position = 'absolute';
  73. this.stats.domElement.style.top = '5px';
  74. this.stats.domElement.style.left= '5px';
  75. container.appendChild(this.stats.domElement);
  76. document.body.appendChild(container);
  77. };
  78.  
  79. Sketch.prototype.update = function() {
  80. this.stats.update();
  81. this._cube.rotation.y += 0.02;
  82. };
  83.  
  84. Sketch.prototype.render = function() {
  85. this._renderer.render(this._scene, this._camera);
  86. };
  87.  
  88. var sketch = new Sketch();
  89. })();
Add Comment
Please, Sign In to add comment