webgl_materials_lightmap.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - lightmap</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. <style>
  8. body {
  9. background:#fff;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. font-family:georgia;
  14. text-align:center;
  15. }
  16. h1 { }
  17. a { color:skyblue }
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../build/three.min.js"></script>
  22. <script src="js/Detector.js"></script>
  23. <script src="js/Stats.js"></script>
  24. <script type="x-shader/x-vertex" id="vertexShader">
  25. varying vec3 worldPosition;
  26. void main() {
  27. vec4 mPosition = modelMatrix * vec4( position, 1.0 );
  28. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  29. worldPosition = mPosition.xyz;
  30. }
  31. </script>
  32. <script type="x-shader/x-fragment" id="fragmentShader">
  33. uniform vec3 topColor;
  34. uniform vec3 bottomColor;
  35. uniform float offset;
  36. uniform float exponent;
  37. varying vec3 worldPosition;
  38. void main() {
  39. float h = normalize( worldPosition + offset ).y;
  40. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( h, exponent ), 0.0 ) ), 1.0 );
  41. }
  42. </script>
  43. <script>
  44. var SCREEN_WIDTH = window.innerWidth;
  45. var SCREEN_HEIGHT = window.innerHeight;
  46. var container,stats;
  47. var camera, scene, renderer;
  48. var clock = new THREE.Clock();
  49. init();
  50. animate();
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. // CAMERA
  55. camera = new THREE.PerspectiveCamera( 40, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  56. camera.position.x = 700;
  57. camera.position.z = -500;
  58. camera.position.y = 180;
  59. // SCENE
  60. scene = new THREE.Scene();
  61. scene.fog = new THREE.Fog( 0xfafafa, 1000, 10000 );
  62. scene.fog.color.setHSV( 0.6, 0.125, 1 );
  63. // CONTROLS
  64. controls = new THREE.TrackballControls( camera );
  65. controls.target.z = 150;
  66. // LIGHTS
  67. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.475 );
  68. directionalLight.position.set( 100, 100, -100 );
  69. scene.add( directionalLight );
  70. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 1.25 );
  71. hemiLight.color.setHSV( 0.6, 0.45, 1 );
  72. hemiLight.groundColor.setHSV( 0.1, 0.45, 0.95 );
  73. hemiLight.position.y = 500;
  74. scene.add( hemiLight );
  75. // SKYDOME
  76. var vertexShader = document.getElementById( 'vertexShader' ).textContent;
  77. var fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
  78. var uniforms = {
  79. topColor: { type: "c", value: new THREE.Color( 0x0077ff ) },
  80. bottomColor: { type: "c", value: new THREE.Color( 0xffffff ) },
  81. offset: { type: "f", value: 400 },
  82. exponent: { type: "f", value: 0.6 }
  83. }
  84. uniforms.topColor.value.copy( hemiLight.color );
  85. scene.fog.color.copy( uniforms.bottomColor.value );
  86. var skyGeo = new THREE.SphereGeometry( 4000, 32, 15 );
  87. var skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } );
  88. var sky = new THREE.Mesh( skyGeo, skyMat );
  89. scene.add( sky );
  90. // RENDERER
  91. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false, clearColor: 0xfafafa, clearAlpha: 1 } );
  92. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  93. renderer.domElement.style.position = "relative";
  94. container.appendChild( renderer.domElement );
  95. renderer.setClearColor( scene.fog.color, 1 );
  96. renderer.gammaInput = true;
  97. renderer.gammaOutput = true;
  98. renderer.physicallyBasedShading = true;
  99. // STATS
  100. stats = new Stats();
  101. stats.domElement.style.position = 'absolute';
  102. stats.domElement.style.top = '0px';
  103. stats.domElement.style.zIndex = 100;
  104. container.appendChild( stats.domElement );
  105. stats.domElement.children[ 0 ].children[ 0 ].style.color = "#abc";
  106. stats.domElement.children[ 0 ].style.background = "transparent";
  107. stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
  108. // MODEL
  109. var loader = new THREE.JSONLoader();
  110. var callback = function( geometry ) { createScene( geometry, 0, 0, 0, 0, 100 ) };
  111. loader.load( "obj/lightmap/lightmap.js", callback );
  112. //
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. function createScene( geometry, x, y, z, b, s ) {
  121. var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
  122. mesh.position.set( x, y, z );
  123. mesh.scale.set( s, s, s );
  124. scene.add( mesh );
  125. }
  126. //
  127. function animate() {
  128. requestAnimationFrame( animate );
  129. render();
  130. stats.update();
  131. }
  132. function render() {
  133. var delta = clock.getDelta();
  134. controls.update( delta );
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>