1
0

webgl_materials_bumpmap.html 4.0 KB

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