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