webgl_simple_gi.html 5.3 KB

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