webgl_materials_lightmap.html 4.8 KB

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