webgl_materials_bumpmap.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  29. const statsEnabled = true;
  30. let container, stats, loader;
  31. let camera, scene, renderer;
  32. let mesh;
  33. let spotLight;
  34. let mouseX = 0;
  35. let mouseY = 0;
  36. let targetX = 0;
  37. let targetY = 0;
  38. const windowHalfX = window.innerWidth / 2;
  39. const windowHalfY = window.innerHeight / 2;
  40. init();
  41. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. //
  46. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  47. camera.position.z = 1200;
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0x060708 );
  50. // LIGHTS
  51. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  52. spotLight = new THREE.SpotLight( 0xffffbb, 2 );
  53. spotLight.position.set( 0.5, 0, 1 );
  54. spotLight.position.multiplyScalar( 700 );
  55. scene.add( spotLight );
  56. spotLight.castShadow = true;
  57. spotLight.shadow.mapSize.width = 2048;
  58. spotLight.shadow.mapSize.height = 2048;
  59. spotLight.shadow.camera.near = 200;
  60. spotLight.shadow.camera.far = 1500;
  61. spotLight.shadow.camera.fov = 40;
  62. spotLight.shadow.bias = - 0.005;
  63. //
  64. const mapHeight = new THREE.TextureLoader().load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg' );
  65. const material = new THREE.MeshPhongMaterial( {
  66. color: 0x552811,
  67. specular: 0x222222,
  68. shininess: 25,
  69. bumpMap: mapHeight,
  70. bumpScale: 12
  71. } );
  72. loader = new GLTFLoader();
  73. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  74. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  75. } );
  76. renderer = new THREE.WebGLRenderer();
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. renderer.shadowMap.enabled = true;
  81. renderer.outputEncoding = THREE.sRGBEncoding;
  82. //
  83. if ( statsEnabled ) {
  84. stats = new Stats();
  85. container.appendChild( stats.dom );
  86. }
  87. // EVENTS
  88. document.addEventListener( 'mousemove', onDocumentMouseMove );
  89. window.addEventListener( 'resize', onWindowResize );
  90. }
  91. function createScene( geometry, scale, material ) {
  92. mesh = new THREE.Mesh( geometry, material );
  93. mesh.position.y = - 50;
  94. mesh.scale.set( scale, scale, scale );
  95. mesh.castShadow = true;
  96. mesh.receiveShadow = true;
  97. scene.add( mesh );
  98. }
  99. //
  100. function onWindowResize() {
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. }
  105. function onDocumentMouseMove( event ) {
  106. mouseX = ( event.clientX - windowHalfX );
  107. mouseY = ( event.clientY - windowHalfY );
  108. }
  109. //
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. if ( statsEnabled ) stats.update();
  114. }
  115. function render() {
  116. targetX = mouseX * .001;
  117. targetY = mouseY * .001;
  118. if ( mesh ) {
  119. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  120. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  121. }
  122. renderer.render( scene, camera );
  123. }
  124. </script>
  125. </body>
  126. </html>