webgl_materials_lightmap.html 5.1 KB

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