webgl_materials_normalmap_object_space.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - normal map [object space]</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> - webgl object-space normalmap demo<br />
  12. Nefertiti Bust by
  13. <a href="http://www.cultlab3d.de/" target="_blank" rel="noopener">CultLab3D</a><br />
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  28. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  29. let renderer, scene, camera;
  30. init();
  31. function init() {
  32. // renderer
  33. renderer = new THREE.WebGLRenderer();
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. document.body.appendChild( renderer.domElement );
  36. renderer.outputEncoding = THREE.sRGBEncoding;
  37. // scene
  38. scene = new THREE.Scene();
  39. // camera
  40. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  41. camera.position.set( - 10, 0, 23 );
  42. scene.add( camera );
  43. // controls
  44. const controls = new OrbitControls( camera, renderer.domElement );
  45. controls.addEventListener( 'change', render );
  46. controls.minDistance = 10;
  47. controls.maxDistance = 50;
  48. controls.enablePan = false;
  49. // ambient
  50. scene.add( new THREE.AmbientLight( 0xffffff, .2 ) );
  51. // light
  52. const light = new THREE.PointLight( 0xffffff, 1.5 );
  53. camera.add( light );
  54. // model
  55. new GLTFLoader().load( 'models/gltf/Nefertiti/Nefertiti.glb', function ( gltf ) {
  56. gltf.scene.traverse( function ( child ) {
  57. if ( child.isMesh ) {
  58. // glTF currently supports only tangent-space normal maps.
  59. // this model has been modified to demonstrate the use of an object-space normal map.
  60. child.material.normalMapType = THREE.ObjectSpaceNormalMap;
  61. // attribute normals are not required with an object-space normal map. remove them.
  62. child.geometry.deleteAttribute( 'normal' );
  63. //
  64. child.material.side = THREE.DoubleSide;
  65. child.scale.multiplyScalar( 0.5 );
  66. // recenter
  67. new THREE.Box3().setFromObject( child ).getCenter( child.position ).multiplyScalar( - 1 );
  68. scene.add( child );
  69. }
  70. } );
  71. render();
  72. } );
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. render();
  80. }
  81. function render() {
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>