webgl_materials_bumpmap.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - bump map [Lee Perry-Smith]</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:#000;
  10. color:#fff;
  11. padding:0;
  12. margin:0;
  13. font-weight: bold;
  14. overflow:hidden;
  15. }
  16. a { color: #ffffff; }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. color: #ffffff;
  21. padding: 5px;
  22. font-family:Monospace;
  23. font-size:13px;
  24. text-align:center;
  25. z-index:1000;
  26. }
  27. #webglmessage {
  28. background:rgb(200,100,0) !important;
  29. color:#fff;
  30. }
  31. #vt { display:none }
  32. #vt, #vt a { color:orange; }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="info">
  37. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl bump mapping without tangents -
  38. <a href="http://graphics.cs.williams.edu/data/meshes.xml#14" target="_blank" rel="noopener">Lee Perry-Smith</a> head
  39. </div>
  40. <script src="../build/three.js"></script>
  41. <script src="js/loaders/GLTFLoader.js"></script>
  42. <script src="js/WebGL.js"></script>
  43. <script src="js/libs/stats.min.js"></script>
  44. <script>
  45. if ( WEBGL.isWebGLAvailable() === false ) {
  46. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  47. }
  48. var statsEnabled = true;
  49. var container, stats, loader;
  50. var camera, scene, renderer;
  51. var mesh;
  52. var spotLight;
  53. var mouseX = 0;
  54. var mouseY = 0;
  55. var targetX = 0;
  56. var targetY = 0;
  57. var windowHalfX = window.innerWidth / 2;
  58. var windowHalfY = window.innerHeight / 2;
  59. init();
  60. animate();
  61. function init() {
  62. container = document.createElement( 'div' );
  63. document.body.appendChild( container );
  64. //
  65. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  66. camera.position.z = 1200;
  67. scene = new THREE.Scene();
  68. scene.background = new THREE.Color( 0x060708 );
  69. // LIGHTS
  70. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  71. spotLight = new THREE.SpotLight( 0xffffbb, 2 );
  72. spotLight.position.set( 0.5, 0, 1 );
  73. spotLight.position.multiplyScalar( 700 );
  74. scene.add( spotLight );
  75. spotLight.castShadow = true;
  76. spotLight.shadow.mapSize.width = 2048;
  77. spotLight.shadow.mapSize.height = 2048;
  78. spotLight.shadow.camera.near = 200;
  79. spotLight.shadow.camera.far = 1500;
  80. spotLight.shadow.camera.fov = 40;
  81. spotLight.shadow.bias = - 0.005;
  82. //
  83. var mapHeight = new THREE.TextureLoader().load( "models/gltf/LeePerrySmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg" );
  84. var material = new THREE.MeshPhongMaterial( {
  85. color: 0x552811,
  86. specular: 0x222222,
  87. shininess: 25,
  88. bumpMap: mapHeight,
  89. bumpScale: 12
  90. } );
  91. loader = new THREE.GLTFLoader();
  92. loader.load( "models/gltf/LeePerrySmith/LeePerrySmith.glb", function ( gltf ) {
  93. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  94. } );
  95. renderer = new THREE.WebGLRenderer();
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. container.appendChild( renderer.domElement );
  99. renderer.shadowMap.enabled = true;
  100. //
  101. renderer.gammaInput = true;
  102. renderer.gammaOutput = true;
  103. //
  104. if ( statsEnabled ) {
  105. stats = new Stats();
  106. container.appendChild( stats.dom );
  107. }
  108. // EVENTS
  109. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  110. window.addEventListener( 'resize', onWindowResize, false );
  111. }
  112. function createScene( geometry, scale, material ) {
  113. mesh = new THREE.Mesh( geometry, material );
  114. mesh.position.y = - 50;
  115. mesh.scale.set( scale, scale, scale );
  116. mesh.castShadow = true;
  117. mesh.receiveShadow = true;
  118. scene.add( mesh );
  119. }
  120. //
  121. function onWindowResize() {
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. }
  126. function onDocumentMouseMove( event ) {
  127. mouseX = ( event.clientX - windowHalfX );
  128. mouseY = ( event.clientY - windowHalfY );
  129. }
  130. //
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. render();
  134. if ( statsEnabled ) stats.update();
  135. }
  136. function render() {
  137. targetX = mouseX * .001;
  138. targetY = mouseY * .001;
  139. if ( mesh ) {
  140. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  141. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  142. }
  143. renderer.render( scene, camera );
  144. }
  145. </script>
  146. </body>
  147. </html>