webgl_materials_normalmap_object_space.html 2.9 KB

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