2
0

webgl_simple_gi.html 5.2 KB

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