1
0

webgl_camera_cinematic.html 5.7 KB

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