webgl_simple_gi.html 5.4 KB

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