webgl_watch.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  32. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  33. import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
  34. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  35. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  36. import { FXAAShader } from 'three/addons/shaders/FXAAShader.js';
  37. let composer, camera, scene, renderer;
  38. let gui, dirLight, pointLight, controls, bloomPass, fxaaPass;
  39. let ready = false;
  40. const meshs = {};
  41. const materials = {};
  42. const torad = Math.PI / 180;
  43. const setting = {
  44. roughness: 0.09,
  45. metalness: 1.0,
  46. opacity: 0.4,
  47. threshold: 0,
  48. strength: 0.08,
  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 } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.setAnimationLoop( animate );
  62. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  63. renderer.toneMappingExposure = 0.7;
  64. renderer.shadowMap.enabled = true;
  65. renderer.shadowMap.type = THREE.VSMShadowMap;
  66. container.appendChild( renderer.domElement );
  67. new HDRLoader()
  68. .setPath( 'textures/equirectangular/' )
  69. .load( 'lobe.hdr', function ( texture ) {
  70. texture.mapping = THREE.EquirectangularReflectionMapping;
  71. scene.background = texture;
  72. scene.environment = texture;
  73. scene.backgroundBlurriness = 0.5;
  74. scene.backgroundIntensity = 1.0;
  75. scene.environmentIntensity = 1.5;
  76. // model
  77. const loader = new GLTFLoader().setPath( 'models/gltf/' );
  78. loader.setDRACOLoader( new DRACOLoader().setDecoderPath( 'jsm/libs/draco/gltf/' ) );
  79. loader.load( 'rolex.glb', function ( gltf ) {
  80. gltf.scene.rotation.x = Math.PI * 0.25;
  81. gltf.scene.traverse( ( child ) => {
  82. if ( child.isMesh || child.isGroup ) {
  83. if ( child.isMesh ) {
  84. child.material.vertexColors = false;
  85. materials[ child.material.name ] = child.material;
  86. if ( child.name !== 'glass' ) {
  87. child.receiveShadow = true;
  88. child.castShadow = true;
  89. }
  90. }
  91. meshs[ child.name ] = child;
  92. }
  93. } );
  94. scene.add( gltf.scene );
  95. meshs.glass.material = new THREE.MeshPhysicalMaterial( {
  96. color: 0x020205,
  97. transparent: true, opacity: setting.opacity,
  98. metalness: 0, roughness: 0,
  99. iridescence: 0.3,
  100. clearcoat: 1.0,
  101. blending: THREE.AdditiveBlending
  102. } );
  103. ready = true;
  104. createGUI();
  105. } );
  106. } );
  107. controls = new OrbitControls( camera, renderer.domElement );
  108. controls.minDistance = 0.3;
  109. controls.maxDistance = 10;
  110. controls.target.set( 0, - 0.1, 0 );
  111. controls.enableDamping = true;
  112. controls.dampingFactor = 0.05;
  113. controls.update();
  114. dirLight = new THREE.DirectionalLight( 0xFFFFFF, 6 );
  115. dirLight.position.set( - 0.1, 0.6, 0.4 );
  116. dirLight.castShadow = true;
  117. scene.add( dirLight );
  118. const shadow = dirLight.shadow;
  119. shadow.mapSize.width = shadow.mapSize.height = 1024;
  120. shadow.radius = 8;
  121. shadow.bias = - 0.0005;
  122. const shadowCam = shadow.camera, s = 0.5;
  123. shadowCam.near = 0.1;
  124. shadowCam.far = 2;
  125. shadowCam.right = shadowCam.top = s;
  126. shadowCam.left = shadowCam.bottom = - s;
  127. // debug shadow
  128. //scene.add( new THREE.CameraHelper(shadowCam) );
  129. pointLight = new THREE.PointLight( 0x7b8cad, 1, 0, 2 );
  130. pointLight.position.set( 0.3, - 0.2, - 0.2 );
  131. scene.add( pointLight );
  132. window.addEventListener( 'resize', onWindowResize );
  133. moveCamera();
  134. }
  135. function moveCamera() {
  136. controls.enabled = false;
  137. controls.enableDamping = false;
  138. const sph = new THREE.Spherical();
  139. const target = controls.target;
  140. const tmp = {
  141. distance: controls.getDistance(),
  142. phi: controls.getPolarAngle(),
  143. theta: controls.getAzimuthalAngle()
  144. };
  145. new TWEEN.Tween( tmp )
  146. .to( { distance: 1, theta: - Math.PI * 0.2 }, 6000 )
  147. .easing( TWEEN.Easing.Quadratic.Out )
  148. .onUpdate( function ( n ) {
  149. sph.set( n.distance, n.phi, n.theta );
  150. camera.position.setFromSpherical( sph ).add( target );
  151. camera.lookAt( target );
  152. } )
  153. .onComplete( function () {
  154. controls.enabled = true;
  155. controls.enableDamping = true;
  156. } )
  157. .start();
  158. }
  159. function postProcess( b ) {
  160. if ( b ) {
  161. if ( composer ) return;
  162. const renderPass = new RenderPass( scene, camera );
  163. const outputPass = new OutputPass();
  164. bloomPass = new UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  165. bloomPass.threshold = setting.threshold;
  166. bloomPass.strength = setting.strength;
  167. bloomPass.radius = setting.radius;
  168. fxaaPass = new ShaderPass( FXAAShader );
  169. const pixelRatio = renderer.getPixelRatio();
  170. fxaaPass.material.uniforms[ 'resolution' ].value.set( 1 / ( window.innerWidth * pixelRatio ), 1 / ( window.innerHeight * pixelRatio ) );
  171. composer = new EffectComposer( renderer );
  172. composer.setPixelRatio( pixelRatio );
  173. composer.addPass( renderPass );
  174. composer.addPass( bloomPass );
  175. composer.addPass( fxaaPass );
  176. composer.addPass( outputPass );
  177. composer.addPass( fxaaPass );
  178. composer.addPass( fxaaPass );
  179. } else {
  180. if ( ! composer ) return;
  181. composer.dispose();
  182. composer = null;
  183. bloomPass = null;
  184. fxaaPass = null;
  185. }
  186. }
  187. function createGUI() {
  188. gui = new GUI();
  189. gui.add( setting, 'roughness', 0, 1, 0.01 ).onChange( upMaterial );
  190. gui.add( setting, 'metalness', 0, 1, 0.01 ).onChange( upMaterial );
  191. gui.add( setting, 'opacity', 0, 1, 0.01 ).onChange( upMaterial );
  192. //
  193. gui.add( setting, 'postProcess' ).onChange( postProcess );
  194. gui.add( setting, 'threshold', 0, 1, 0.01 ).onChange( upBloom );
  195. gui.add( setting, 'strength', 0, 3, 0.01 ).onChange( upBloom );
  196. gui.add( setting, 'radius', 0, 1, 0.01 ).onChange( upBloom );
  197. }
  198. function upMaterial() {
  199. materials.Gold.metalness = materials.Silver.metalness = setting.metalness;
  200. materials.Gold.roughness = materials.Silver.roughness = setting.roughness;
  201. meshs.glass.material.opacity = setting.opacity;
  202. }
  203. function upBloom() {
  204. if ( ! bloomPass ) return;
  205. bloomPass.threshold = setting.threshold;
  206. bloomPass.strength = setting.strength;
  207. bloomPass.radius = setting.radius;
  208. }
  209. function getTime() {
  210. const currentDate = new Date();
  211. let hour = currentDate.getHours();
  212. const minute = currentDate.getMinutes();
  213. const second = currentDate.getSeconds();
  214. let day = currentDate.getDay();
  215. const month = currentDate.getMonth();
  216. const milli = currentDate.getMilliseconds();
  217. if ( hour >= 12 ) hour -= 12;
  218. if ( day > 30 ) day = 30;
  219. meshs.hour.rotation.y = - hour * 30 * torad;
  220. meshs.minute.rotation.y = - minute * 6 * torad;
  221. meshs.second.rotation.y = - second * 6 * torad;
  222. meshs.mini_03.rotation.y = - day * 12 * torad;
  223. meshs.mini_02.rotation.y = - month * 30 * torad;
  224. meshs.mini_01.rotation.y = - milli * 0.36 * torad;
  225. }
  226. function onWindowResize() {
  227. const width = window.innerWidth;
  228. const height = window.innerHeight;
  229. camera.aspect = width / height;
  230. camera.updateProjectionMatrix();
  231. renderer.setSize( width, height );
  232. if ( composer ) {
  233. composer.setSize( width, height );
  234. if ( fxaaPass ) {
  235. const pr = renderer.getPixelRatio();
  236. fxaaPass.material.uniforms[ 'resolution' ].value.set( 1 / ( width * pr ), 1 / ( height * pr ) );
  237. }
  238. }
  239. }
  240. //
  241. function animate() {
  242. controls.update();
  243. TWEEN.update();
  244. if ( composer ) composer.render();
  245. else renderer.render( scene, camera );
  246. if ( ready ) getTime();
  247. }
  248. </script>
  249. </body>
  250. </html>
粤ICP备19079148号