webgl_watch.html 8.7 KB

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