webgl_camera_logarithmicdepthbuffer.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - cameras - logarithmic depth buffer</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. <style>
  9. .renderer_label {
  10. position: absolute;
  11. bottom: 1em;
  12. width: 100%;
  13. color: white;
  14. z-index: 10;
  15. display: block;
  16. text-align: center;
  17. }
  18. #container {
  19. display: flex;
  20. }
  21. #container_normal {
  22. width: 50%;
  23. display: inline-block;
  24. position: relative;
  25. }
  26. #container_logzbuf {
  27. width: 50%;
  28. display: inline-block;
  29. position: relative;
  30. }
  31. #renderer_border {
  32. position: absolute;
  33. top: 0;
  34. left: 25%;
  35. bottom: 0;
  36. width: 2px;
  37. z-index: 10;
  38. opacity: .8;
  39. background: #ccc;
  40. border: 1px inset #ccc;
  41. cursor: col-resize;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="container">
  47. <div id="container_normal"><h2 class="renderer_label">normal z-buffer</h2></div>
  48. <div id="container_logzbuf"><h2 class="renderer_label">logarithmic z-buffer</h2></div>
  49. <div id="renderer_border"></div>
  50. </div>
  51. <div id="info">
  52. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - cameras - logarithmic depth buffer<br/>
  53. mousewheel to dolly out
  54. </div>
  55. <script type="module">
  56. import * as THREE from '../build/three.module.js';
  57. import Stats from './jsm/libs/stats.module.js';
  58. // 1 micrometer to 100 billion light years in one scene, with 1 unit = 1 meter? preposterous! and yet...
  59. var NEAR = 1e-6, FAR = 1e27;
  60. var SCREEN_WIDTH = window.innerWidth;
  61. var SCREEN_HEIGHT = window.innerHeight;
  62. var screensplit = .25, screensplit_right = 0;
  63. var mouse = [ .5, .5 ];
  64. var zoompos = - 100, minzoomspeed = .015;
  65. var zoomspeed = minzoomspeed;
  66. var container, border, stats;
  67. var objects = {};
  68. // Generate a number of text labels, from 1µm in size up to 100,000,000 light years
  69. // Try to use some descriptive real-world examples of objects at each scale
  70. var labeldata = [
  71. { size: .01, scale: 0.0001, label: "microscopic (1µm)" }, // FIXME - triangulating text fails at this size, so we scale instead
  72. { size: .01, scale: 0.1, label: "minuscule (1mm)" },
  73. { size: .01, scale: 1.0, label: "tiny (1cm)" },
  74. { size: 1, scale: 1.0, label: "child-sized (1m)" },
  75. { size: 10, scale: 1.0, label: "tree-sized (10m)" },
  76. { size: 100, scale: 1.0, label: "building-sized (100m)" },
  77. { size: 1000, scale: 1.0, label: "medium (1km)" },
  78. { size: 10000, scale: 1.0, label: "city-sized (10km)" },
  79. { size: 3400000, scale: 1.0, label: "moon-sized (3,400 Km)" },
  80. { size: 12000000, scale: 1.0, label: "planet-sized (12,000 km)" },
  81. { size: 1400000000, scale: 1.0, label: "sun-sized (1,400,000 km)" },
  82. { size: 7.47e12, scale: 1.0, label: "solar system-sized (50Au)" },
  83. { size: 9.4605284e15, scale: 1.0, label: "gargantuan (1 light year)" },
  84. { size: 3.08567758e16, scale: 1.0, label: "ludicrous (1 parsec)" },
  85. { size: 1e19, scale: 1.0, label: "mind boggling (1000 light years)" },
  86. { size: 1.135e21, scale: 1.0, label: "galaxy-sized (120,000 light years)" },
  87. { size: 9.46e23, scale: 1.0, label: "... (100,000,000 light years)" }
  88. ];
  89. init();
  90. function init() {
  91. container = document.getElementById( 'container' );
  92. var loader = new THREE.FontLoader();
  93. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  94. var scene = initScene( font );
  95. // Initialize two copies of the same scene, one with normal z-buffer and one with logarithmic z-buffer
  96. objects.normal = initView( scene, 'normal', false );
  97. objects.logzbuf = initView( scene, 'logzbuf', true );
  98. animate();
  99. } );
  100. stats = new Stats();
  101. container.appendChild( stats.dom );
  102. // Resize border allows the user to easily compare effects of logarithmic depth buffer over the whole scene
  103. border = document.getElementById( 'renderer_border' );
  104. border.addEventListener( 'mousedown', onBorderMouseDown );
  105. window.addEventListener( 'mousemove', onMouseMove, false );
  106. window.addEventListener( 'resize', onWindowResize, false );
  107. window.addEventListener( 'wheel', onMouseWheel, false );
  108. }
  109. function initView( scene, name, logDepthBuf ) {
  110. var framecontainer = document.getElementById( 'container_' + name );
  111. var camera = new THREE.PerspectiveCamera( 50, screensplit * SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  112. scene.add( camera );
  113. var renderer = new THREE.WebGLRenderer( { antialias: true, logarithmicDepthBuffer: logDepthBuf } );
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( SCREEN_WIDTH / 2, SCREEN_HEIGHT );
  116. renderer.domElement.style.position = "relative";
  117. renderer.domElement.id = 'renderer_' + name;
  118. framecontainer.appendChild( renderer.domElement );
  119. return { container: framecontainer, renderer: renderer, scene: scene, camera: camera };
  120. }
  121. function initScene( font ) {
  122. var scene = new THREE.Scene();
  123. scene.add( new THREE.AmbientLight( 0x222222 ) );
  124. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  125. light.position.set( 100, 100, 100 );
  126. scene.add( light );
  127. var materialargs = {
  128. color: 0xffffff,
  129. specular: 0x050505,
  130. shininess: 50,
  131. emissive: 0x000000
  132. };
  133. var geometry = new THREE.SphereBufferGeometry( 0.5, 24, 12 );
  134. for ( var i = 0; i < labeldata.length; i ++ ) {
  135. var scale = labeldata[ i ].scale || 1;
  136. var labelgeo = new THREE.TextBufferGeometry( labeldata[ i ].label, {
  137. font: font,
  138. size: labeldata[ i ].size,
  139. height: labeldata[ i ].size / 2
  140. } );
  141. labelgeo.computeBoundingSphere();
  142. // center text
  143. labelgeo.translate( - labelgeo.boundingSphere.radius, 0, 0 );
  144. materialargs.color = new THREE.Color().setHSL( Math.random(), 0.5, 0.5 );
  145. var material = new THREE.MeshPhongMaterial( materialargs );
  146. var group = new THREE.Group();
  147. group.position.z = - labeldata[ i ].size * scale;
  148. scene.add( group );
  149. var textmesh = new THREE.Mesh( labelgeo, material );
  150. textmesh.scale.set( scale, scale, scale );
  151. textmesh.position.z = - labeldata[ i ].size * scale;
  152. textmesh.position.y = labeldata[ i ].size / 4 * scale;
  153. group.add( textmesh );
  154. var dotmesh = new THREE.Mesh( geometry, material );
  155. dotmesh.position.y = - labeldata[ i ].size / 4 * scale;
  156. dotmesh.scale.multiplyScalar( labeldata[ i ].size * scale );
  157. group.add( dotmesh );
  158. }
  159. return scene;
  160. }
  161. function updateRendererSizes() {
  162. // Recalculate size for both renderers when screen size or split location changes
  163. SCREEN_WIDTH = window.innerWidth;
  164. SCREEN_HEIGHT = window.innerHeight;
  165. screensplit_right = 1 - screensplit;
  166. objects.normal.renderer.setSize( screensplit * SCREEN_WIDTH, SCREEN_HEIGHT );
  167. objects.normal.camera.aspect = screensplit * SCREEN_WIDTH / SCREEN_HEIGHT;
  168. objects.normal.camera.updateProjectionMatrix();
  169. objects.normal.camera.setViewOffset( SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH * screensplit, SCREEN_HEIGHT );
  170. objects.normal.container.style.width = ( screensplit * 100 ) + '%';
  171. objects.logzbuf.renderer.setSize( screensplit_right * SCREEN_WIDTH, SCREEN_HEIGHT );
  172. objects.logzbuf.camera.aspect = screensplit_right * SCREEN_WIDTH / SCREEN_HEIGHT;
  173. objects.logzbuf.camera.updateProjectionMatrix();
  174. objects.logzbuf.camera.setViewOffset( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH * screensplit, 0, SCREEN_WIDTH * screensplit_right, SCREEN_HEIGHT );
  175. objects.logzbuf.container.style.width = ( screensplit_right * 100 ) + '%';
  176. border.style.left = ( screensplit * 100 ) + "%";
  177. }
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. render();
  181. }
  182. function render() {
  183. // Put some limits on zooming
  184. var minzoom = labeldata[ 0 ].size * labeldata[ 0 ].scale * 1;
  185. var maxzoom = labeldata[ labeldata.length - 1 ].size * labeldata[ labeldata.length - 1 ].scale * 100;
  186. var damping = ( Math.abs( zoomspeed ) > minzoomspeed ? .95 : 1.0 );
  187. // Zoom out faster the further out you go
  188. var zoom = THREE.MathUtils.clamp( Math.pow( Math.E, zoompos ), minzoom, maxzoom );
  189. zoompos = Math.log( zoom );
  190. // Slow down quickly at the zoom limits
  191. if ( ( zoom == minzoom && zoomspeed < 0 ) || ( zoom == maxzoom && zoomspeed > 0 ) ) {
  192. damping = .85;
  193. }
  194. zoompos += zoomspeed;
  195. zoomspeed *= damping;
  196. objects.normal.camera.position.x = Math.sin( .5 * Math.PI * ( mouse[ 0 ] - .5 ) ) * zoom;
  197. objects.normal.camera.position.y = Math.sin( .25 * Math.PI * ( mouse[ 1 ] - .5 ) ) * zoom;
  198. objects.normal.camera.position.z = Math.cos( .5 * Math.PI * ( mouse[ 0 ] - .5 ) ) * zoom;
  199. objects.normal.camera.lookAt( objects.normal.scene.position );
  200. // Clone camera settings across both scenes
  201. objects.logzbuf.camera.position.copy( objects.normal.camera.position );
  202. objects.logzbuf.camera.quaternion.copy( objects.normal.camera.quaternion );
  203. // Update renderer sizes if the split has changed
  204. if ( screensplit_right != 1 - screensplit ) {
  205. updateRendererSizes();
  206. }
  207. objects.normal.renderer.render( objects.normal.scene, objects.normal.camera );
  208. objects.logzbuf.renderer.render( objects.logzbuf.scene, objects.logzbuf.camera );
  209. stats.update();
  210. }
  211. function onWindowResize() {
  212. updateRendererSizes();
  213. }
  214. function onBorderMouseDown( ev ) {
  215. // activate draggable window resizing bar
  216. window.addEventListener( "mousemove", onBorderMouseMove );
  217. window.addEventListener( "mouseup", onBorderMouseUp );
  218. ev.stopPropagation();
  219. ev.preventDefault();
  220. }
  221. function onBorderMouseMove( ev ) {
  222. screensplit = Math.max( 0, Math.min( 1, ev.clientX / window.innerWidth ) );
  223. ev.stopPropagation();
  224. }
  225. function onBorderMouseUp() {
  226. window.removeEventListener( "mousemove", onBorderMouseMove );
  227. window.removeEventListener( "mouseup", onBorderMouseUp );
  228. }
  229. function onMouseMove( ev ) {
  230. mouse[ 0 ] = ev.clientX / window.innerWidth;
  231. mouse[ 1 ] = ev.clientY / window.innerHeight;
  232. }
  233. function onMouseWheel( ev ) {
  234. var amount = ev.deltaY;
  235. if ( amount === 0 ) return;
  236. var dir = amount / Math.abs( amount );
  237. zoomspeed = dir / 10;
  238. // Slow down default zoom speed after user starts zooming, to give them more control
  239. minzoomspeed = 0.001;
  240. }
  241. </script>
  242. </body>
  243. </html>