webgl_camera_cinematic.html 5.6 KB

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