webgl_materials_bumpmap.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - bump mapping without tangents<br/>
  12. <a href="https://casual-effects.com/data/" target="_blank" rel="noopener">Lee Perry-Smith</a> head
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from './jsm/libs/stats.module.js';
  27. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  28. const statsEnabled = true;
  29. let container, stats, loader;
  30. let camera, scene, renderer;
  31. let mesh;
  32. let spotLight;
  33. let mouseX = 0;
  34. let mouseY = 0;
  35. let targetX = 0;
  36. let targetY = 0;
  37. const windowHalfX = window.innerWidth / 2;
  38. const windowHalfY = window.innerHeight / 2;
  39. init();
  40. animate();
  41. function init() {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. //
  45. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  46. camera.position.z = 1200;
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 0x060708 );
  49. // LIGHTS
  50. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  51. spotLight = new THREE.SpotLight( 0xffffbb, 2 );
  52. spotLight.position.set( 0.5, 0, 1 );
  53. spotLight.position.multiplyScalar( 700 );
  54. scene.add( spotLight );
  55. spotLight.castShadow = true;
  56. spotLight.shadow.mapSize.width = 2048;
  57. spotLight.shadow.mapSize.height = 2048;
  58. spotLight.shadow.camera.near = 200;
  59. spotLight.shadow.camera.far = 1500;
  60. spotLight.shadow.camera.fov = 40;
  61. spotLight.shadow.bias = - 0.005;
  62. //
  63. const mapHeight = new THREE.TextureLoader().load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg' );
  64. const material = new THREE.MeshPhongMaterial( {
  65. color: 0x552811,
  66. specular: 0x222222,
  67. shininess: 25,
  68. bumpMap: mapHeight,
  69. bumpScale: 12
  70. } );
  71. loader = new GLTFLoader();
  72. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  73. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  74. } );
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. container.appendChild( renderer.domElement );
  79. renderer.shadowMap.enabled = true;
  80. renderer.outputEncoding = THREE.sRGBEncoding;
  81. //
  82. if ( statsEnabled ) {
  83. stats = new Stats();
  84. container.appendChild( stats.dom );
  85. }
  86. // EVENTS
  87. document.addEventListener( 'mousemove', onDocumentMouseMove );
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. function createScene( geometry, scale, material ) {
  91. mesh = new THREE.Mesh( geometry, material );
  92. mesh.position.y = - 50;
  93. mesh.scale.set( scale, scale, scale );
  94. mesh.castShadow = true;
  95. mesh.receiveShadow = true;
  96. scene.add( mesh );
  97. }
  98. //
  99. function onWindowResize() {
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. }
  104. function onDocumentMouseMove( event ) {
  105. mouseX = ( event.clientX - windowHalfX );
  106. mouseY = ( event.clientY - windowHalfY );
  107. }
  108. //
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. render();
  112. if ( statsEnabled ) stats.update();
  113. }
  114. function render() {
  115. targetX = mouseX * .001;
  116. targetY = mouseY * .001;
  117. if ( mesh ) {
  118. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  119. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  120. }
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>