webgl_simple_gi.html 5.5 KB

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