webgl_watch.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - watch</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. <meta property="og:title" content="three.js webgl - watch">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_watch.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_watch.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Rolex + aomap - 1200 ko
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/",
  23. "tween": "./jsm/libs/tween.module.js"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import * as TWEEN from 'tween';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  32. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  33. import { DRACOLoader, DRACO_GLTF_CONFIG } from 'three/addons/loaders/DRACOLoader.js';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
  36. import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js';
  37. let camera, scene, renderer;
  38. let gui, dirLight, pointLight, controls, bloomPass, taaPass;
  39. let ready = false;
  40. const meshes = {};
  41. const materials = {};
  42. const torad = Math.PI / 180;
  43. const setting = {
  44. roughness: 0.1,
  45. metalness: 1.0,
  46. opacity: 0.8,
  47. threshold: 0,
  48. strength: 0.007,
  49. radius: 0.0,
  50. postProcess: false
  51. };
  52. init();
  53. function init() {
  54. const container = document.getElementById( 'container' );
  55. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.1, 20 );
  56. camera.position.set( 0.8, 0.5, - 1.5 );
  57. scene = new THREE.Scene();
  58. renderer = new THREE.WebGLRenderer( { antialias: true, outputBufferType: THREE.HalfFloatType } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.setAnimationLoop( animate );
  62. renderer.toneMapping = THREE.NeutralToneMapping;
  63. renderer.toneMappingExposure = 0.7;
  64. renderer.shadowMap.enabled = true;
  65. container.appendChild( renderer.domElement );
  66. taaPass = new TAARenderPass( scene, camera );
  67. taaPass.sampleLevel = 2;
  68. bloomPass = new UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  69. bloomPass.threshold = setting.threshold;
  70. bloomPass.strength = setting.strength;
  71. bloomPass.radius = setting.radius;
  72. new HDRLoader()
  73. .setPath( 'textures/equirectangular/' )
  74. .load( 'lobe.hdr', function ( texture ) {
  75. texture.mapping = THREE.EquirectangularReflectionMapping;
  76. scene.background = texture;
  77. scene.environment = texture;
  78. scene.backgroundBlurriness = 0.5;
  79. scene.backgroundIntensity = 1.0;
  80. scene.environmentIntensity = 1.5;
  81. // model
  82. const loader = new GLTFLoader().setPath( 'models/gltf/' );
  83. loader.setDRACOLoader( new DRACOLoader().setDecoderPath( DRACO_GLTF_CONFIG ) );
  84. loader.load( 'rolex.glb', function ( gltf ) {
  85. gltf.scene.rotation.x = Math.PI * 0.25;
  86. gltf.scene.traverse( ( child ) => {
  87. if ( child.isMesh || child.isGroup ) {
  88. if ( child.isMesh ) {
  89. child.material.vertexColors = false;
  90. if ( materials[ child.material.name ] ) child.material = materials[ child.material.name ];
  91. else materials[ child.material.name ] = child.material;
  92. if ( child.name !== 'glass' && child.name !== 'floor' ) {
  93. child.receiveShadow = true;
  94. child.castShadow = true;
  95. }
  96. }
  97. meshes[ child.name ] = child;
  98. }
  99. } );
  100. scene.add( gltf.scene );
  101. meshes.glass.material = new THREE.MeshPhysicalMaterial( {
  102. color: 0x020205,
  103. transparent: true, opacity: setting.opacity,
  104. metalness: 0, roughness: 0,
  105. iridescence: 0.3,
  106. clearcoat: 1.0,
  107. blending: THREE.AdditiveBlending
  108. } );
  109. ready = true;
  110. createGUI();
  111. } );
  112. } );
  113. controls = new OrbitControls( camera, renderer.domElement );
  114. controls.minDistance = 0.3;
  115. controls.maxDistance = 10;
  116. controls.target.set( 0, - 0.1, 0 );
  117. controls.enableDamping = true;
  118. controls.dampingFactor = 0.05;
  119. controls.update();
  120. dirLight = new THREE.DirectionalLight( 0xFFFFFF, 6 );
  121. dirLight.position.set( 0.2, 0.6, 0.4 );
  122. dirLight.castShadow = true;
  123. scene.add( dirLight );
  124. const shadow = dirLight.shadow;
  125. shadow.mapSize.width = shadow.mapSize.height = 2048;
  126. shadow.radius = 8;
  127. shadow.bias = - 0.0005;
  128. const shadowCam = shadow.camera, s = 0.5;
  129. shadowCam.near = 0.1;
  130. shadowCam.far = 2;
  131. shadowCam.right = shadowCam.top = s;
  132. shadowCam.left = shadowCam.bottom = - s;
  133. // debug shadow
  134. //scene.add( new THREE.CameraHelper(shadowCam) );
  135. pointLight = new THREE.PointLight( 0x7b8cad, 1, 0, 2 );
  136. pointLight.position.set( - 0.3, - 0.2, - 0.2 );
  137. scene.add( pointLight );
  138. window.addEventListener( 'resize', onWindowResize );
  139. moveCamera();
  140. }
  141. function moveCamera() {
  142. controls.enabled = false;
  143. controls.enableDamping = false;
  144. const sph = new THREE.Spherical();
  145. const target = controls.target;
  146. const tmp = {
  147. distance: controls.getDistance(),
  148. phi: controls.getPolarAngle(),
  149. theta: controls.getAzimuthalAngle()
  150. };
  151. new TWEEN.Tween( tmp )
  152. .to( { distance: 1, theta: - Math.PI * 0.2 }, 6000 )
  153. .easing( TWEEN.Easing.Quadratic.Out )
  154. .onUpdate( function ( n ) {
  155. sph.set( n.distance, n.phi, n.theta );
  156. camera.position.setFromSpherical( sph ).add( target );
  157. camera.lookAt( target );
  158. } )
  159. .onComplete( function () {
  160. controls.enabled = true;
  161. controls.enableDamping = true;
  162. } )
  163. .start();
  164. }
  165. function postProcess( b ) {
  166. if ( b ) {
  167. renderer.setEffects( [ taaPass, bloomPass ] );
  168. } else {
  169. renderer.setEffects( null );
  170. }
  171. }
  172. function createGUI() {
  173. gui = new GUI();
  174. gui.add( setting, 'roughness', 0, 1, 0.01 ).onChange( upMaterial );
  175. gui.add( setting, 'metalness', 0, 1, 0.01 ).onChange( upMaterial );
  176. gui.add( setting, 'opacity', 0, 1, 0.01 ).onChange( upMaterial );
  177. //
  178. gui.add( setting, 'postProcess' ).onChange( postProcess );
  179. gui.add( setting, 'threshold', 0, 1, 0.01 ).onChange( upBloom );
  180. gui.add( setting, 'strength', 0, 0.1, 0.001 ).onChange( upBloom );
  181. gui.add( setting, 'radius', 0, 1, 0.01 ).onChange( upBloom );
  182. }
  183. function upMaterial() {
  184. materials.Gold.metalness = materials.Silver.metalness = setting.metalness;
  185. materials.Gold.roughness = materials.Silver.roughness = setting.roughness;
  186. meshes.glass.material.opacity = setting.opacity;
  187. }
  188. function upBloom() {
  189. if ( ! bloomPass ) return;
  190. bloomPass.threshold = setting.threshold;
  191. bloomPass.strength = setting.strength;
  192. bloomPass.radius = setting.radius;
  193. }
  194. function getTime() {
  195. const currentDate = new Date();
  196. let hour = currentDate.getHours();
  197. const minute = currentDate.getMinutes();
  198. const second = currentDate.getSeconds();
  199. let day = currentDate.getDay();
  200. const month = currentDate.getMonth();
  201. const milli = currentDate.getMilliseconds();
  202. if ( hour >= 12 ) hour -= 12;
  203. if ( day > 30 ) day = 30;
  204. meshes.hour.rotation.y = - hour * 30 * torad;
  205. meshes.minute.rotation.y = - minute * 6 * torad;
  206. meshes.second.rotation.y = - second * 6 * torad;
  207. meshes.mini_03.rotation.y = - day * 12 * torad;
  208. meshes.mini_02.rotation.y = - month * 30 * torad;
  209. meshes.mini_01.rotation.y = - milli * 0.36 * torad;
  210. }
  211. function onWindowResize() {
  212. const width = window.innerWidth;
  213. const height = window.innerHeight;
  214. camera.aspect = width / height;
  215. camera.updateProjectionMatrix();
  216. renderer.setSize( width, height );
  217. }
  218. //
  219. function animate() {
  220. controls.update();
  221. TWEEN.update();
  222. renderer.render( scene, camera );
  223. if ( ready ) getTime();
  224. }
  225. </script>
  226. </body>
  227. </html>
粤ICP备19079148号