webgl_simple_gi.html 5.3 KB

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