webgl_materials_normalmap.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - normal 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. <meta property="og:title" content="three.js webgl - materials - normal map [Lee Perry-Smith]">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_materials_normalmap.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_materials_normalmap.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl normalmap demo.<br/>
  16. <a href="https://casual-effects.com/data/" target="_blank" rel="noopener">Lee Perry-Smith</a> head.
  17. </div>
  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 { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  31. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  32. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  33. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  34. import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js';
  35. import { ColorCorrectionShader } from 'three/addons/shaders/ColorCorrectionShader.js';
  36. import { FXAAPass } from 'three/addons/postprocessing/FXAAPass.js';
  37. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  38. let container, loader;
  39. let camera, scene, renderer, controls;
  40. let mesh;
  41. let directionalLight, pointLight, ambientLight;
  42. let composer;
  43. const params = {
  44. enableNormalMap: true,
  45. normalScale: 1
  46. };
  47. init();
  48. function init() {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 0.1, 100 );
  52. camera.position.z = 12;
  53. scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0x494949 );
  55. // LIGHTS
  56. ambientLight = new THREE.AmbientLight( 0xffffff );
  57. scene.add( ambientLight );
  58. pointLight = new THREE.PointLight( 0xffffff, 30 );
  59. pointLight.position.set( 0, 0, 6 );
  60. scene.add( pointLight );
  61. directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  62. directionalLight.position.set( 1, - 0.5, - 1 );
  63. scene.add( directionalLight );
  64. const textureLoader = new THREE.TextureLoader();
  65. const diffuseMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-COL.jpg' );
  66. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  67. const specularMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-SPEC.jpg' );
  68. specularMap.colorSpace = THREE.SRGBColorSpace;
  69. const normalMap = textureLoader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' );
  70. const material = new THREE.MeshPhongMaterial( {
  71. color: 0xefefef,
  72. specular: 0x222222,
  73. shininess: 35,
  74. map: diffuseMap,
  75. specularMap: specularMap,
  76. normalMap: normalMap,
  77. normalScale: new THREE.Vector2( params.normalScale, params.normalScale )
  78. } );
  79. loader = new GLTFLoader();
  80. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  81. const geometry = gltf.scene.children[ 0 ].geometry;
  82. mesh = new THREE.Mesh( geometry, material );
  83. mesh.position.y = - 0.5;
  84. scene.add( mesh );
  85. } );
  86. renderer = new THREE.WebGLRenderer();
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. renderer.setAnimationLoop( animate );
  89. container.appendChild( renderer.domElement );
  90. // COMPOSER
  91. renderer.autoClear = false;
  92. const renderModel = new RenderPass( scene, camera );
  93. const effectBleach = new ShaderPass( BleachBypassShader );
  94. const effectColor = new ShaderPass( ColorCorrectionShader );
  95. const outputPass = new OutputPass();
  96. const effectFXAA = new FXAAPass();
  97. effectBleach.uniforms[ 'opacity' ].value = 0.2;
  98. effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
  99. effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
  100. const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { type: THREE.HalfFloatType, depthTexture: new THREE.DepthTexture() } );
  101. composer = new EffectComposer( renderer, renderTarget );
  102. composer.addPass( renderModel );
  103. composer.addPass( effectBleach );
  104. composer.addPass( effectColor );
  105. composer.addPass( outputPass );
  106. composer.addPass( effectFXAA );
  107. // EVENTS
  108. window.addEventListener( 'resize', onWindowResize );
  109. // GUI
  110. const gui = new GUI();
  111. gui.add( params, 'enableNormalMap' ).name( 'enable normal map' ).onChange( ( value ) => {
  112. material.normalMap = ( value === true ) ? normalMap : null;
  113. material.needsUpdate = true;
  114. } );
  115. gui.add( params, 'normalScale', 0, 2 ).name( 'normal scale' ).onChange( ( value )=> material.normalScale.setScalar( value ) );
  116. gui.open();
  117. // CONTROLS
  118. controls = new OrbitControls( camera, renderer.domElement );
  119. controls.minDistance = 8;
  120. controls.maxDistance = 50;
  121. controls.enablePan = false;
  122. controls.enableDamping = true;
  123. }
  124. //
  125. function onWindowResize() {
  126. const width = window.innerWidth;
  127. const height = window.innerHeight;
  128. camera.aspect = width / height;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( width, height );
  131. composer.setSize( width, height );
  132. }
  133. //
  134. function animate() {
  135. controls.update();
  136. composer.render();
  137. }
  138. </script>
  139. </body>
  140. </html>
粤ICP备19079148号