webgl_lights_hemisphere.html 6.8 KB

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