webgl_simple_gi.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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" rel="noopener">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. var newMaterial = ( this.material.isMaterial ) ? this.material.clone() : this.material.slice();
  38. return new this.constructor( this.geometry.clone(), newMaterial ).copy( this );
  39. };
  40. //
  41. var SimpleGI = function ( renderer, scene ) {
  42. var SIZE = 32, SIZE2 = SIZE * SIZE;
  43. var camera = new THREE.PerspectiveCamera( 90, 1, 0.01, 100 );
  44. scene.updateMatrixWorld( true );
  45. var clone = scene.clone();
  46. clone.autoUpdate = false;
  47. var rt = new THREE.WebGLRenderTarget( SIZE, SIZE, {
  48. wrapS: THREE.ClampToEdgeWrapping,
  49. wrapT: THREE.ClampToEdgeWrapping,
  50. stencilBuffer: false,
  51. depthBuffer: true
  52. } );
  53. var normalMatrix = new THREE.Matrix3();
  54. var position = new THREE.Vector3();
  55. var normal = new THREE.Vector3();
  56. var bounces = 0;
  57. var currentVertex = 0;
  58. var color = new Float32Array( 3 );
  59. var buffer = new Uint8Array( SIZE2 * 4 );
  60. function compute() {
  61. if ( bounces === 3 ) return;
  62. var object = scene.children[ 0 ];
  63. var geometry = object.geometry;
  64. var attributes = geometry.attributes;
  65. var positions = attributes.position.array;
  66. var normals = attributes.normal.array;
  67. if ( attributes.color === undefined ) {
  68. var colors = new Float32Array( positions.length );
  69. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setDynamic( true ) );
  70. }
  71. var colors = attributes.color.array;
  72. var startVertex = currentVertex;
  73. var totalVertex = positions.length / 3;
  74. for ( var i = 0; i < 32; i ++ ) {
  75. if ( currentVertex >= totalVertex ) break;
  76. position.fromArray( positions, currentVertex * 3 );
  77. position.applyMatrix4( object.matrixWorld );
  78. normal.fromArray( normals, currentVertex * 3 );
  79. normal.applyMatrix3( normalMatrix.getNormalMatrix( object.matrixWorld ) ).normalize();
  80. camera.position.copy( position );
  81. camera.lookAt( position.add( normal ) );
  82. renderer.render( clone, camera, rt );
  83. renderer.readRenderTargetPixels( rt, 0, 0, SIZE, SIZE, buffer );
  84. color[ 0 ] = 0;
  85. color[ 1 ] = 0;
  86. color[ 2 ] = 0;
  87. for ( var k = 0, kl = buffer.length; k < kl; k += 4 ) {
  88. color[ 0 ] += buffer[ k + 0 ];
  89. color[ 1 ] += buffer[ k + 1 ];
  90. color[ 2 ] += buffer[ k + 2 ];
  91. }
  92. colors[ currentVertex * 3 + 0 ] = color[ 0 ] / ( SIZE2 * 255 );
  93. colors[ currentVertex * 3 + 1 ] = color[ 1 ] / ( SIZE2 * 255 );
  94. colors[ currentVertex * 3 + 2 ] = color[ 2 ] / ( SIZE2 * 255 );
  95. currentVertex ++;
  96. }
  97. attributes.color.updateRange.offset = startVertex * 3;
  98. attributes.color.updateRange.count = ( currentVertex - startVertex ) * 3;
  99. attributes.color.needsUpdate = true;
  100. if ( currentVertex >= totalVertex ) {
  101. clone = scene.clone();
  102. clone.autoUpdate = false;
  103. bounces ++
  104. currentVertex = 0;
  105. }
  106. requestAnimationFrame( compute );
  107. }
  108. requestAnimationFrame( compute );
  109. }
  110. //
  111. var camera, scene, renderer;
  112. init();
  113. animate();
  114. function init() {
  115. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  116. camera.position.z = 4;
  117. scene = new THREE.Scene();
  118. // sphere
  119. var geometry = new THREE.TorusKnotBufferGeometry( 0.75, 0.3, 128, 32, 1 );
  120. // var geometry = new THREE.BoxBufferGeometry( 1, 1, 1, 10, 10, 10 )
  121. var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors });
  122. var mesh = new THREE.Mesh( geometry, material );
  123. // mesh.position.y = 1;
  124. scene.add( mesh );
  125. /*
  126. var geometry = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
  127. for ( var i = 0; i < 10; i ++ ) {
  128. var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.DoubleSide } );
  129. var mesh = new THREE.Mesh( geometry, material );
  130. mesh.position.x = Math.random() * 3 - 1.5;
  131. mesh.position.y = Math.random() * 3 - 1.5;
  132. mesh.position.z = Math.random() * 3 - 1.5;
  133. mesh.updateMatrix();
  134. scene.add( mesh );
  135. }
  136. */
  137. // room
  138. var materials = [];
  139. for ( var i = 0; i < 8; i ++ ) {
  140. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.BackSide } ) );
  141. }
  142. var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
  143. var mesh = new THREE.Mesh( geometry, materials );
  144. scene.add( mesh );
  145. //
  146. renderer = new THREE.WebGLRenderer();
  147. renderer.setPixelRatio( window.devicePixelRatio );
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. document.body.appendChild( renderer.domElement );
  150. new SimpleGI( renderer, scene );
  151. controls = new THREE.OrbitControls( camera );
  152. window.addEventListener( 'resize', onWindowResize, false );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. renderer.render( scene, camera );
  162. }
  163. </script>
  164. </body>
  165. </html>