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