webgl_materials_normalmap_object_space.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. let renderer, scene, camera;
  31. init();
  32. function init() {
  33. // renderer
  34. renderer = new THREE.WebGLRenderer();
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. document.body.appendChild( renderer.domElement );
  37. renderer.outputEncoding = THREE.sRGBEncoding;
  38. // scene
  39. scene = new THREE.Scene();
  40. // camera
  41. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.set( - 10, 0, 23 );
  43. scene.add( camera );
  44. // controls
  45. const controls = new OrbitControls( camera, renderer.domElement );
  46. controls.addEventListener( 'change', render );
  47. controls.minDistance = 10;
  48. controls.maxDistance = 50;
  49. controls.enablePan = false;
  50. // ambient
  51. scene.add( new THREE.AmbientLight( 0xffffff, .2 ) );
  52. // light
  53. const light = new THREE.PointLight( 0xffffff, 1.5 );
  54. camera.add( light );
  55. // model
  56. new GLTFLoader().load( 'models/gltf/Nefertiti/Nefertiti.glb', function ( gltf ) {
  57. gltf.scene.traverse( function ( child ) {
  58. if ( child.isMesh ) {
  59. // glTF currently supports only tangent-space normal maps.
  60. // this model has been modified to demonstrate the use of an object-space normal map.
  61. child.material.normalMapType = THREE.ObjectSpaceNormalMap;
  62. // attribute normals are not required with an object-space normal map. remove them.
  63. child.geometry.deleteAttribute( 'normal' );
  64. //
  65. child.material.side = THREE.DoubleSide;
  66. child.scale.multiplyScalar( 0.5 );
  67. // recenter
  68. new THREE.Box3().setFromObject( child ).getCenter( child.position ).multiplyScalar( - 1 );
  69. scene.add( child );
  70. }
  71. } );
  72. render();
  73. } );
  74. window.addEventListener( 'resize', onWindowResize );
  75. }
  76. function onWindowResize() {
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. render();
  81. }
  82. function render() {
  83. renderer.render( scene, camera );
  84. }
  85. </script>
  86. </body>
  87. </html>