webgl_camera_cinematic.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - camera cinematic</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. body {
  10. background-color: #fff;
  11. color: #000;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive cubes
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import Stats from 'three/addons/libs/stats.module.js';
  33. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  34. import { CinematicCamera } from 'three/addons/cameras/CinematicCamera.js';
  35. let camera, scene, raycaster, renderer, stats;
  36. const mouse = new THREE.Vector2();
  37. let INTERSECTED;
  38. const radius = 100;
  39. let theta = 0;
  40. init();
  41. function init() {
  42. camera = new CinematicCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.setLens( 5 );
  44. camera.position.set( 2, 1, 500 );
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0xf0f0f0 );
  47. scene.add( new THREE.AmbientLight( 0xffffff ) );
  48. const light = new THREE.DirectionalLight( 0xffffff );
  49. light.position.set( 1, 1, 1 ).normalize();
  50. scene.add( light );
  51. const geometry = new THREE.BoxGeometry( 20, 20, 20 );
  52. for ( let i = 0; i < 1500; i ++ ) {
  53. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  54. object.position.x = Math.random() * 800 - 400;
  55. object.position.y = Math.random() * 800 - 400;
  56. object.position.z = Math.random() * 800 - 400;
  57. scene.add( object );
  58. }
  59. raycaster = new THREE.Raycaster();
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. stats = new Stats();
  66. document.body.appendChild( stats.dom );
  67. document.addEventListener( 'mousemove', onDocumentMouseMove );
  68. window.addEventListener( 'resize', onWindowResize );
  69. const effectController = {
  70. focalLength: 15,
  71. // jsDepthCalculation: true,
  72. // shaderFocus: false,
  73. //
  74. fstop: 2.8,
  75. // maxblur: 1.0,
  76. //
  77. showFocus: false,
  78. focalDepth: 3,
  79. // manualdof: false,
  80. // vignetting: false,
  81. // depthblur: false,
  82. //
  83. // threshold: 0.5,
  84. // gain: 2.0,
  85. // bias: 0.5,
  86. // fringe: 0.7,
  87. //
  88. // focalLength: 35,
  89. // noise: true,
  90. // pentagon: false,
  91. //
  92. // dithering: 0.0001
  93. };
  94. const matChanger = function ( ) {
  95. for ( const e in effectController ) {
  96. if ( e in camera.postprocessing.bokeh_uniforms ) {
  97. camera.postprocessing.bokeh_uniforms[ e ].value = effectController[ e ];
  98. }
  99. }
  100. camera.postprocessing.bokeh_uniforms[ 'znear' ].value = camera.near;
  101. camera.postprocessing.bokeh_uniforms[ 'zfar' ].value = camera.far;
  102. camera.setLens( effectController.focalLength, camera.frameHeight, effectController.fstop, camera.coc );
  103. effectController[ 'focalDepth' ] = camera.postprocessing.bokeh_uniforms[ 'focalDepth' ].value;
  104. };
  105. //
  106. const gui = new GUI();
  107. gui.add( effectController, 'focalLength', 1, 135, 0.01 ).onChange( matChanger );
  108. gui.add( effectController, 'fstop', 1.8, 22, 0.01 ).onChange( matChanger );
  109. gui.add( effectController, 'focalDepth', 0.1, 100, 0.001 ).onChange( matChanger );
  110. gui.add( effectController, 'showFocus', true ).onChange( matChanger );
  111. matChanger();
  112. }
  113. function onWindowResize() {
  114. camera.aspect = window.innerWidth / window.innerHeight;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. }
  118. function onDocumentMouseMove( event ) {
  119. event.preventDefault();
  120. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  121. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  122. }
  123. function animate() {
  124. render();
  125. stats.update();
  126. }
  127. function render() {
  128. theta += 0.1;
  129. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  130. camera.position.y = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  131. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  132. camera.lookAt( scene.position );
  133. camera.updateMatrixWorld();
  134. // find intersections
  135. raycaster.setFromCamera( mouse, camera );
  136. const intersects = raycaster.intersectObjects( scene.children, false );
  137. if ( intersects.length > 0 ) {
  138. const targetDistance = intersects[ 0 ].distance;
  139. camera.focusAt( targetDistance ); // using Cinematic camera focusAt method
  140. if ( INTERSECTED != intersects[ 0 ].object ) {
  141. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  142. INTERSECTED = intersects[ 0 ].object;
  143. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  144. INTERSECTED.material.emissive.setHex( 0xff0000 );
  145. }
  146. } else {
  147. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  148. INTERSECTED = null;
  149. }
  150. //
  151. if ( camera.postprocessing.enabled ) {
  152. camera.renderCinematic( scene, renderer );
  153. } else {
  154. scene.overrideMaterial = null;
  155. renderer.clear();
  156. renderer.render( scene, camera );
  157. }
  158. }
  159. </script>
  160. </body>
  161. </html>