webgl_watch.html 8.1 KB

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