webgl_simple_gi.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. <meta property="og:title" content="three.js webgl - simple global illumination">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_simple_gi.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_simple_gi.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <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>)
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  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.addUpdateRange( startVertex * 3, ( 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. 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. renderer.setAnimationLoop( animate );
  125. document.body.appendChild( renderer.domElement );
  126. new SimpleGI( renderer, scene );
  127. const controls = new OrbitControls( camera, renderer.domElement );
  128. controls.minDistance = 1;
  129. controls.maxDistance = 10;
  130. window.addEventListener( 'resize', onWindowResize );
  131. }
  132. function onWindowResize() {
  133. camera.aspect = window.innerWidth / window.innerHeight;
  134. camera.updateProjectionMatrix();
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. }
  137. function animate() {
  138. renderer.setRenderTarget( null );
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>
粤ICP备19079148号