webgl_simple_gi.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - simple global illumination</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #000000;
  11. overflow: hidden;
  12. }
  13. a {
  14. color: #0078ff;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 5px;
  19. width: 100%;
  20. color:#fff;
  21. font-family:Monospace;
  22. font-size:13px;
  23. font-weight: bold;
  24. text-align: center;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank">three.js</a> - simple global illumination (<a href="http://www.iquilezles.org/www/articles/simplegi/simplegi.htm">article</a>)
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/controls/OrbitControls.js"></script>
  34. <script>
  35. // HACK:
  36. THREE.Mesh.prototype.clone = function () {
  37. return new this.constructor( this.geometry.clone(), this.material.clone() ).copy( this );
  38. };
  39. //
  40. var SimpleGI = function ( renderer, scene ) {
  41. var SIZE = 32, SIZE2 = SIZE * SIZE;
  42. var camera = new THREE.PerspectiveCamera( 90, 1, 0.01, 100 );
  43. scene.updateMatrixWorld( true );
  44. var clone = scene.clone();
  45. clone.autoUpdate = false;
  46. var rt = new THREE.WebGLRenderTarget( SIZE, SIZE, {
  47. wrapS: THREE.ClampToEdgeWrapping,
  48. wrapT: THREE.ClampToEdgeWrapping,
  49. stencilBuffer: false,
  50. depthBuffer: true
  51. } );
  52. var normalMatrix = new THREE.Matrix3();
  53. var position = new THREE.Vector3();
  54. var normal = new THREE.Vector3();
  55. var bounces = 0;
  56. var currentVertex = 0;
  57. var color = new Float32Array( 3 );
  58. var buffer = new Uint8Array( SIZE2 * 4 );
  59. function compute() {
  60. if ( bounces === 3 ) return;
  61. var object = scene.children[ 0 ];
  62. var geometry = object.geometry;
  63. var attributes = geometry.attributes;
  64. var positions = attributes.position.array;
  65. var normals = attributes.normal.array;
  66. if ( attributes.color === undefined ) {
  67. var colors = new Float32Array( positions.length )
  68. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  69. }
  70. var colors = attributes.color.array;
  71. var totalVertex = positions.length / 3;
  72. for ( var i = 0; i < 32; i ++ ) {
  73. if ( currentVertex >= totalVertex ) {
  74. clone = scene.clone();
  75. clone.autoUpdate = false;
  76. bounces ++
  77. currentVertex = 0;
  78. break;
  79. }
  80. position.fromArray( positions, currentVertex * 3 );
  81. position.applyMatrix4( object.matrixWorld );
  82. normal.fromArray( normals, currentVertex * 3 );
  83. normal.applyMatrix3( normalMatrix.getNormalMatrix( object.matrixWorld ) ).normalize();
  84. camera.position.copy( position );
  85. camera.lookAt( position.add( normal ) );
  86. renderer.render( clone, camera, rt );
  87. renderer.readRenderTargetPixels( rt, 0, 0, SIZE, SIZE, buffer );
  88. color[ 0 ] = 0;
  89. color[ 1 ] = 0;
  90. color[ 2 ] = 0;
  91. for ( var k = 0, kl = buffer.length; k < kl; k += 4 ) {
  92. color[ 0 ] += buffer[ k + 0 ];
  93. color[ 1 ] += buffer[ k + 1 ];
  94. color[ 2 ] += buffer[ k + 2 ];
  95. }
  96. colors[ currentVertex * 3 + 0 ] = color[ 0 ] / ( SIZE2 * 255 );
  97. colors[ currentVertex * 3 + 1 ] = color[ 1 ] / ( SIZE2 * 255 );
  98. colors[ currentVertex * 3 + 2 ] = color[ 2 ] / ( SIZE2 * 255 );
  99. attributes.color.needsUpdate = true;
  100. currentVertex ++;
  101. }
  102. requestAnimationFrame( compute );
  103. }
  104. requestAnimationFrame( compute );
  105. }
  106. //
  107. var camera, scene, renderer;
  108. init();
  109. animate();
  110. function init() {
  111. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  112. camera.position.z = 4;
  113. scene = new THREE.Scene();
  114. // sphere
  115. var geometry = new THREE.TorusKnotBufferGeometry( 0.75, 0.3, 128, 32, 1 );
  116. // var geometry = new THREE.BoxBufferGeometry( 1, 1, 1, 10, 10, 10 )
  117. var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors });
  118. var mesh = new THREE.Mesh( geometry, material );
  119. // mesh.position.y = 1;
  120. scene.add( mesh );
  121. /*
  122. var geometry = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
  123. for ( var i = 0; i < 10; i ++ ) {
  124. var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.DoubleSide } );
  125. var mesh = new THREE.Mesh( geometry, material );
  126. mesh.position.x = Math.random() * 3 - 1.5;
  127. mesh.position.y = Math.random() * 3 - 1.5;
  128. mesh.position.z = Math.random() * 3 - 1.5;
  129. mesh.updateMatrix();
  130. scene.add( mesh );
  131. }
  132. */
  133. // room
  134. var materials = [];
  135. for ( var i = 0; i < 8; i ++ ) {
  136. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.BackSide } ) );
  137. }
  138. var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
  139. var material = new THREE.MultiMaterial( materials );
  140. var mesh = new THREE.Mesh( geometry, material );
  141. scene.add( mesh );
  142. //
  143. renderer = new THREE.WebGLRenderer();
  144. renderer.setPixelRatio( window.devicePixelRatio );
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. document.body.appendChild( renderer.domElement );
  147. new SimpleGI( renderer, scene );
  148. controls = new THREE.OrbitControls( camera );
  149. window.addEventListener( 'resize', onWindowResize, false );
  150. }
  151. function onWindowResize() {
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. }
  156. function animate() {
  157. requestAnimationFrame( animate );
  158. controls.update();
  159. renderer.render( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>