webgl_simple_gi.html 5.6 KB

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