webgl_lights_hemisphere.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - hemisphere light</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. color: #444;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container"></div>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl hemisphere light example<br/>
  21. flamingo by <a href="https://mirada.com/" target="_blank" rel="noopener">mirada</a> from <a href="http://www.ro.me" target="_blank" rel="noopener">ro.me</a>
  22. </div>
  23. <script type="x-shader/x-vertex" id="vertexShader">
  24. varying vec3 vWorldPosition;
  25. void main() {
  26. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  27. vWorldPosition = worldPosition.xyz;
  28. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  29. }
  30. </script>
  31. <script type="x-shader/x-fragment" id="fragmentShader">
  32. uniform vec3 topColor;
  33. uniform vec3 bottomColor;
  34. uniform float offset;
  35. uniform float exponent;
  36. varying vec3 vWorldPosition;
  37. void main() {
  38. float h = normalize( vWorldPosition + offset ).y;
  39. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
  40. }
  41. </script>
  42. <!-- Import maps polyfill -->
  43. <!-- Remove this when import maps will be widely supported -->
  44. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  45. <script type="importmap">
  46. {
  47. "imports": {
  48. "three": "../build/three.module.js",
  49. "three/addons/": "./jsm/"
  50. }
  51. }
  52. </script>
  53. <script type="module">
  54. import * as THREE from 'three';
  55. import Stats from 'three/addons/libs/stats.module.js';
  56. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  57. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  58. let camera, scene, renderer;
  59. const mixers = [];
  60. let stats;
  61. const clock = new THREE.Clock();
  62. init();
  63. animate();
  64. function init() {
  65. const container = document.getElementById( 'container' );
  66. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 5000 );
  67. camera.position.set( 0, 0, 250 );
  68. scene = new THREE.Scene();
  69. scene.background = new THREE.Color().setHSL( 0.6, 0, 1 );
  70. scene.fog = new THREE.Fog( scene.background, 1, 5000 );
  71. // LIGHTS
  72. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.6 );
  73. hemiLight.color.setHSL( 0.6, 1, 0.6 );
  74. hemiLight.groundColor.setHSL( 0.095, 1, 0.75 );
  75. hemiLight.position.set( 0, 50, 0 );
  76. scene.add( hemiLight );
  77. const hemiLightHelper = new THREE.HemisphereLightHelper( hemiLight, 10 );
  78. scene.add( hemiLightHelper );
  79. //
  80. const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  81. dirLight.color.setHSL( 0.1, 1, 0.95 );
  82. dirLight.position.set( - 1, 1.75, 1 );
  83. dirLight.position.multiplyScalar( 30 );
  84. scene.add( dirLight );
  85. dirLight.castShadow = true;
  86. dirLight.shadow.mapSize.width = 2048;
  87. dirLight.shadow.mapSize.height = 2048;
  88. const d = 50;
  89. dirLight.shadow.camera.left = - d;
  90. dirLight.shadow.camera.right = d;
  91. dirLight.shadow.camera.top = d;
  92. dirLight.shadow.camera.bottom = - d;
  93. dirLight.shadow.camera.far = 3500;
  94. dirLight.shadow.bias = - 0.0001;
  95. const dirLightHelper = new THREE.DirectionalLightHelper( dirLight, 10 );
  96. scene.add( dirLightHelper );
  97. // GROUND
  98. const groundGeo = new THREE.PlaneGeometry( 10000, 10000 );
  99. const groundMat = new THREE.MeshLambertMaterial( { color: 0xffffff } );
  100. groundMat.color.setHSL( 0.095, 1, 0.75 );
  101. const ground = new THREE.Mesh( groundGeo, groundMat );
  102. ground.position.y = - 33;
  103. ground.rotation.x = - Math.PI / 2;
  104. ground.receiveShadow = true;
  105. scene.add( ground );
  106. // SKYDOME
  107. const vertexShader = document.getElementById( 'vertexShader' ).textContent;
  108. const fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
  109. const uniforms = {
  110. 'topColor': { value: new THREE.Color( 0x0077ff ) },
  111. 'bottomColor': { value: new THREE.Color( 0xffffff ) },
  112. 'offset': { value: 33 },
  113. 'exponent': { value: 0.6 }
  114. };
  115. uniforms[ 'topColor' ].value.copy( hemiLight.color );
  116. scene.fog.color.copy( uniforms[ 'bottomColor' ].value );
  117. const skyGeo = new THREE.SphereGeometry( 4000, 32, 15 );
  118. const skyMat = new THREE.ShaderMaterial( {
  119. uniforms: uniforms,
  120. vertexShader: vertexShader,
  121. fragmentShader: fragmentShader,
  122. side: THREE.BackSide
  123. } );
  124. const sky = new THREE.Mesh( skyGeo, skyMat );
  125. scene.add( sky );
  126. // MODEL
  127. const loader = new GLTFLoader();
  128. loader.load( 'models/gltf/Flamingo.glb', function ( gltf ) {
  129. const mesh = gltf.scene.children[ 0 ];
  130. const s = 0.35;
  131. mesh.scale.set( s, s, s );
  132. mesh.position.y = 15;
  133. mesh.rotation.y = - 1;
  134. mesh.castShadow = true;
  135. mesh.receiveShadow = true;
  136. scene.add( mesh );
  137. const mixer = new THREE.AnimationMixer( mesh );
  138. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  139. mixers.push( mixer );
  140. } );
  141. // RENDERER
  142. renderer = new THREE.WebGLRenderer( { antialias: true } );
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. container.appendChild( renderer.domElement );
  146. renderer.shadowMap.enabled = true;
  147. // STATS
  148. stats = new Stats();
  149. container.appendChild( stats.dom );
  150. //
  151. const params = {
  152. toggleHemisphereLight: function () {
  153. hemiLight.visible = ! hemiLight.visible;
  154. hemiLightHelper.visible = ! hemiLightHelper.visible;
  155. },
  156. toggleDirectionalLight: function () {
  157. dirLight.visible = ! dirLight.visible;
  158. dirLightHelper.visible = ! dirLightHelper.visible;
  159. }
  160. };
  161. const gui = new GUI();
  162. gui.add( params, 'toggleHemisphereLight' ).name( 'toggle hemisphere light' );
  163. gui.add( params, 'toggleDirectionalLight' ).name( 'toggle directional light' );
  164. gui.open();
  165. //
  166. window.addEventListener( 'resize', onWindowResize );
  167. }
  168. function onWindowResize() {
  169. camera.aspect = window.innerWidth / window.innerHeight;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. }
  173. //
  174. function animate() {
  175. requestAnimationFrame( animate );
  176. render();
  177. stats.update();
  178. }
  179. function render() {
  180. const delta = clock.getDelta();
  181. for ( let i = 0; i < mixers.length; i ++ ) {
  182. mixers[ i ].update( delta );
  183. }
  184. renderer.render( scene, camera );
  185. }
  186. </script>
  187. </body>
  188. </html>