webgl_simple_gi.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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="https://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. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. THREE.ColorManagement.enabled = false; // TODO: Confirm correct color management.
  28. class GIMesh extends THREE.Mesh {
  29. copy( source ) {
  30. super.copy( source );
  31. this.geometry = source.geometry.clone();
  32. return this;
  33. }
  34. }
  35. //
  36. const SimpleGI = function ( renderer, scene ) {
  37. const SIZE = 32, SIZE2 = SIZE * SIZE;
  38. const camera = new THREE.PerspectiveCamera( 90, 1, 0.01, 100 );
  39. scene.updateMatrixWorld( true );
  40. let clone = scene.clone();
  41. clone.matrixWorldAutoUpdate = false;
  42. const rt = new THREE.WebGLRenderTarget( SIZE, SIZE );
  43. const normalMatrix = new THREE.Matrix3();
  44. const position = new THREE.Vector3();
  45. const normal = new THREE.Vector3();
  46. let bounces = 0;
  47. let currentVertex = 0;
  48. const color = new Float32Array( 3 );
  49. const buffer = new Uint8Array( SIZE2 * 4 );
  50. function compute() {
  51. if ( bounces === 3 ) return;
  52. const object = scene.children[ 0 ]; // torusKnot
  53. const geometry = object.geometry;
  54. const attributes = geometry.attributes;
  55. const positions = attributes.position.array;
  56. const normals = attributes.normal.array;
  57. if ( attributes.color === undefined ) {
  58. const colors = new Float32Array( positions.length );
  59. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setUsage( THREE.DynamicDrawUsage ) );
  60. }
  61. const colors = attributes.color.array;
  62. const startVertex = currentVertex;
  63. const totalVertex = positions.length / 3;
  64. for ( let i = 0; i < 32; i ++ ) {
  65. if ( currentVertex >= totalVertex ) break;
  66. position.fromArray( positions, currentVertex * 3 );
  67. position.applyMatrix4( object.matrixWorld );
  68. normal.fromArray( normals, currentVertex * 3 );
  69. normal.applyMatrix3( normalMatrix.getNormalMatrix( object.matrixWorld ) ).normalize();
  70. camera.position.copy( position );
  71. camera.lookAt( position.add( normal ) );
  72. renderer.setRenderTarget( rt );
  73. renderer.render( clone, camera );
  74. renderer.readRenderTargetPixels( rt, 0, 0, SIZE, SIZE, buffer );
  75. color[ 0 ] = 0;
  76. color[ 1 ] = 0;
  77. color[ 2 ] = 0;
  78. for ( let k = 0, kl = buffer.length; k < kl; k += 4 ) {
  79. color[ 0 ] += buffer[ k + 0 ];
  80. color[ 1 ] += buffer[ k + 1 ];
  81. color[ 2 ] += buffer[ k + 2 ];
  82. }
  83. colors[ currentVertex * 3 + 0 ] = color[ 0 ] / ( SIZE2 * 255 );
  84. colors[ currentVertex * 3 + 1 ] = color[ 1 ] / ( SIZE2 * 255 );
  85. colors[ currentVertex * 3 + 2 ] = color[ 2 ] / ( SIZE2 * 255 );
  86. currentVertex ++;
  87. }
  88. attributes.color.updateRange.offset = startVertex * 3;
  89. attributes.color.updateRange.count = ( currentVertex - startVertex ) * 3;
  90. attributes.color.needsUpdate = true;
  91. if ( currentVertex >= totalVertex ) {
  92. clone = scene.clone();
  93. clone.matrixWorldAutoUpdate = false;
  94. bounces ++;
  95. currentVertex = 0;
  96. }
  97. requestAnimationFrame( compute );
  98. }
  99. requestAnimationFrame( compute );
  100. };
  101. //
  102. let camera, scene, renderer;
  103. init();
  104. animate();
  105. function init() {
  106. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  107. camera.position.z = 4;
  108. scene = new THREE.Scene();
  109. // torus knot
  110. const torusGeometry = new THREE.TorusKnotGeometry( 0.75, 0.3, 128, 32, 1 );
  111. const material = new THREE.MeshBasicMaterial( { vertexColors: true } );
  112. const torusKnot = new GIMesh( torusGeometry, material );
  113. scene.add( torusKnot );
  114. // room
  115. const materials = [];
  116. for ( let i = 0; i < 8; i ++ ) {
  117. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.BackSide } ) );
  118. }
  119. const boxGeometry = new THREE.BoxGeometry( 3, 3, 3 );
  120. const box = new THREE.Mesh( boxGeometry, materials );
  121. scene.add( box );
  122. //
  123. renderer = new THREE.WebGLRenderer();
  124. renderer.setPixelRatio( window.devicePixelRatio );
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  127. document.body.appendChild( renderer.domElement );
  128. new SimpleGI( renderer, scene );
  129. const controls = new OrbitControls( camera, renderer.domElement );
  130. controls.minDistance = 1;
  131. controls.maxDistance = 10;
  132. window.addEventListener( 'resize', onWindowResize );
  133. }
  134. function onWindowResize() {
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. }
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. renderer.setRenderTarget( null );
  142. renderer.render( scene, camera );
  143. }
  144. </script>
  145. </body>
  146. </html>