1
0

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. <script type="importmap">
  43. {
  44. "imports": {
  45. "three": "../build/three.module.js",
  46. "three/addons/": "./jsm/"
  47. }
  48. }
  49. </script>
  50. <script type="module">
  51. import * as THREE from 'three';
  52. import Stats from 'three/addons/libs/stats.module.js';
  53. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  54. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  55. let camera, scene, renderer;
  56. const mixers = [];
  57. let stats;
  58. const clock = new THREE.Clock();
  59. init();
  60. function init() {
  61. const container = document.getElementById( 'container' );
  62. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 5000 );
  63. camera.position.set( 0, 0, 250 );
  64. scene = new THREE.Scene();
  65. scene.background = new THREE.Color().setHSL( 0.6, 0, 1 );
  66. scene.fog = new THREE.Fog( scene.background, 1, 5000 );
  67. // LIGHTS
  68. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 2 );
  69. hemiLight.color.setHSL( 0.6, 1, 0.6 );
  70. hemiLight.groundColor.setHSL( 0.095, 1, 0.75 );
  71. hemiLight.position.set( 0, 50, 0 );
  72. scene.add( hemiLight );
  73. const hemiLightHelper = new THREE.HemisphereLightHelper( hemiLight, 10 );
  74. scene.add( hemiLightHelper );
  75. //
  76. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  77. dirLight.color.setHSL( 0.1, 1, 0.95 );
  78. dirLight.position.set( - 1, 1.75, 1 );
  79. dirLight.position.multiplyScalar( 30 );
  80. scene.add( dirLight );
  81. dirLight.castShadow = true;
  82. dirLight.shadow.mapSize.width = 2048;
  83. dirLight.shadow.mapSize.height = 2048;
  84. const d = 50;
  85. dirLight.shadow.camera.left = - d;
  86. dirLight.shadow.camera.right = d;
  87. dirLight.shadow.camera.top = d;
  88. dirLight.shadow.camera.bottom = - d;
  89. dirLight.shadow.camera.far = 3500;
  90. dirLight.shadow.bias = - 0.0001;
  91. const dirLightHelper = new THREE.DirectionalLightHelper( dirLight, 10 );
  92. scene.add( dirLightHelper );
  93. // GROUND
  94. const groundGeo = new THREE.PlaneGeometry( 10000, 10000 );
  95. const groundMat = new THREE.MeshLambertMaterial( { color: 0xffffff } );
  96. groundMat.color.setHSL( 0.095, 1, 0.75 );
  97. const ground = new THREE.Mesh( groundGeo, groundMat );
  98. ground.position.y = - 33;
  99. ground.rotation.x = - Math.PI / 2;
  100. ground.receiveShadow = true;
  101. scene.add( ground );
  102. // SKYDOME
  103. const vertexShader = document.getElementById( 'vertexShader' ).textContent;
  104. const fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
  105. const uniforms = {
  106. 'topColor': { value: new THREE.Color( 0x0077ff ) },
  107. 'bottomColor': { value: new THREE.Color( 0xffffff ) },
  108. 'offset': { value: 33 },
  109. 'exponent': { value: 0.6 }
  110. };
  111. uniforms[ 'topColor' ].value.copy( hemiLight.color );
  112. scene.fog.color.copy( uniforms[ 'bottomColor' ].value );
  113. const skyGeo = new THREE.SphereGeometry( 4000, 32, 15 );
  114. const skyMat = new THREE.ShaderMaterial( {
  115. uniforms: uniforms,
  116. vertexShader: vertexShader,
  117. fragmentShader: fragmentShader,
  118. side: THREE.BackSide
  119. } );
  120. const sky = new THREE.Mesh( skyGeo, skyMat );
  121. scene.add( sky );
  122. // MODEL
  123. const loader = new GLTFLoader();
  124. loader.load( 'models/gltf/Flamingo.glb', function ( gltf ) {
  125. const mesh = gltf.scene.children[ 0 ];
  126. const s = 0.35;
  127. mesh.scale.set( s, s, s );
  128. mesh.position.y = 15;
  129. mesh.rotation.y = - 1;
  130. mesh.castShadow = true;
  131. mesh.receiveShadow = true;
  132. scene.add( mesh );
  133. const mixer = new THREE.AnimationMixer( mesh );
  134. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  135. mixers.push( mixer );
  136. } );
  137. // RENDERER
  138. renderer = new THREE.WebGLRenderer( { antialias: true } );
  139. renderer.setPixelRatio( window.devicePixelRatio );
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. renderer.setAnimationLoop( animate );
  142. container.appendChild( renderer.domElement );
  143. renderer.shadowMap.enabled = true;
  144. // STATS
  145. stats = new Stats();
  146. container.appendChild( stats.dom );
  147. //
  148. const params = {
  149. toggleHemisphereLight: function () {
  150. hemiLight.visible = ! hemiLight.visible;
  151. hemiLightHelper.visible = ! hemiLightHelper.visible;
  152. },
  153. toggleDirectionalLight: function () {
  154. dirLight.visible = ! dirLight.visible;
  155. dirLightHelper.visible = ! dirLightHelper.visible;
  156. },
  157. shadowIntensity: 1
  158. };
  159. const gui = new GUI();
  160. gui.add( params, 'toggleHemisphereLight' ).name( 'toggle hemisphere light' );
  161. gui.add( params, 'toggleDirectionalLight' ).name( 'toggle directional light' );
  162. gui.add( params, 'shadowIntensity', 0, 1 ).name( 'shadow intensity' ).onChange( ( value ) => {
  163. dirLight.shadow.intensity = value;
  164. } );
  165. gui.open();
  166. //
  167. window.addEventListener( 'resize', onWindowResize );
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. //
  175. function 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>