webgl_materials_normalmap.html 5.6 KB

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