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