webgl_materials_bumpmap.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. 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, 0.1, 100 );
  46. camera.position.z = 12;
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 0x060708 );
  49. // LIGHTS
  50. scene.add( new THREE.HemisphereLight( 0x8d7c7c, 0x494966, 3 ) );
  51. spotLight = new THREE.SpotLight( 0xffffde, 200 );
  52. spotLight.position.set( 3.5, 0, 7 );
  53. scene.add( spotLight );
  54. spotLight.castShadow = true;
  55. spotLight.shadow.mapSize.width = 2048;
  56. spotLight.shadow.mapSize.height = 2048;
  57. spotLight.shadow.camera.near = 2;
  58. spotLight.shadow.camera.far = 15;
  59. spotLight.shadow.camera.fov = 40;
  60. spotLight.shadow.bias = - 0.005;
  61. //
  62. const mapHeight = new THREE.TextureLoader().load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg' );
  63. const material = new THREE.MeshPhongMaterial( {
  64. color: 0x9c6e49,
  65. specular: 0x666666,
  66. shininess: 25,
  67. bumpMap: mapHeight,
  68. bumpScale: 0.1
  69. } );
  70. loader = new GLTFLoader();
  71. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  72. createScene( gltf.scene.children[ 0 ].geometry, 1, material );
  73. } );
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. renderer.shadowMap.enabled = true;
  79. //
  80. stats = new Stats();
  81. container.appendChild( stats.dom );
  82. // EVENTS
  83. document.addEventListener( 'mousemove', onDocumentMouseMove );
  84. window.addEventListener( 'resize', onWindowResize );
  85. }
  86. function createScene( geometry, scale, material ) {
  87. mesh = new THREE.Mesh( geometry, material );
  88. mesh.position.y = - 0.5;
  89. mesh.scale.set( scale, scale, scale );
  90. mesh.castShadow = true;
  91. mesh.receiveShadow = true;
  92. scene.add( mesh );
  93. }
  94. //
  95. function onWindowResize() {
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. }
  100. function onDocumentMouseMove( event ) {
  101. mouseX = ( event.clientX - windowHalfX );
  102. mouseY = ( event.clientY - windowHalfY );
  103. }
  104. //
  105. function animate() {
  106. requestAnimationFrame( animate );
  107. render();
  108. stats.update();
  109. }
  110. function render() {
  111. targetX = mouseX * .001;
  112. targetY = mouseY * .001;
  113. if ( mesh ) {
  114. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  115. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  116. }
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>