webgl_materials_normalmap.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  29. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  30. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  31. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  32. import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js';
  33. import { ColorCorrectionShader } from 'three/addons/shaders/ColorCorrectionShader.js';
  34. import { FXAAShader } from 'three/addons/shaders/FXAAShader.js';
  35. import { GammaCorrectionShader } from 'three/addons/shaders/GammaCorrectionShader.js';
  36. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  37. let container, stats, loader;
  38. let camera, scene, renderer;
  39. let mesh;
  40. let directionalLight, pointLight, ambientLight;
  41. let mouseX = 0;
  42. let mouseY = 0;
  43. let targetX = 0;
  44. let targetY = 0;
  45. const windowHalfX = window.innerWidth / 2;
  46. const windowHalfY = window.innerHeight / 2;
  47. let composer, effectFXAA;
  48. init();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  54. camera.position.z = 1200;
  55. scene = new THREE.Scene();
  56. scene.background = new THREE.Color( 0x111111 );
  57. // LIGHTS
  58. ambientLight = new THREE.AmbientLight( 0x444444 );
  59. scene.add( ambientLight );
  60. pointLight = new THREE.PointLight( 0xffffff, 2, 1000 );
  61. pointLight.position.set( 0, 0, 600 );
  62. scene.add( pointLight );
  63. directionalLight = new THREE.DirectionalLight( 0xffffff );
  64. directionalLight.position.set( 1, - 0.5, - 1 );
  65. scene.add( directionalLight );
  66. const textureLoader = new THREE.TextureLoader();
  67. const diffuseMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-COL.jpg' );
  68. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  69. const specularMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-SPEC.jpg' );
  70. specularMap.colorSpace = THREE.SRGBColorSpace;
  71. const normalMap = textureLoader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' );
  72. const material = new THREE.MeshPhongMaterial( {
  73. color: 0xdddddd,
  74. specular: 0x222222,
  75. shininess: 35,
  76. map: diffuseMap,
  77. specularMap: specularMap,
  78. normalMap: normalMap,
  79. normalScale: new THREE.Vector2( 0.8, 0.8 )
  80. } );
  81. loader = new GLTFLoader();
  82. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  83. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  84. } );
  85. renderer = new THREE.WebGLRenderer();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. container.appendChild( renderer.domElement );
  88. //
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. // COMPOSER
  92. renderer.autoClear = false;
  93. const renderModel = new RenderPass( scene, camera );
  94. const effectBleach = new ShaderPass( BleachBypassShader );
  95. const effectColor = new ShaderPass( ColorCorrectionShader );
  96. const gammaCorrection = new ShaderPass( GammaCorrectionShader );
  97. effectFXAA = new ShaderPass( FXAAShader );
  98. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  99. effectBleach.uniforms[ 'opacity' ].value = 0.2;
  100. effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
  101. effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
  102. const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { type: THREE.HalfFloatType, depthTexture: new THREE.DepthTexture() } );
  103. composer = new EffectComposer( renderer, renderTarget );
  104. composer.addPass( renderModel );
  105. composer.addPass( effectBleach );
  106. composer.addPass( effectColor );
  107. composer.addPass( gammaCorrection );
  108. composer.addPass( effectFXAA );
  109. // EVENTS
  110. document.addEventListener( 'mousemove', onDocumentMouseMove );
  111. window.addEventListener( 'resize', onWindowResize );
  112. }
  113. function createScene( geometry, scale, material ) {
  114. mesh = new THREE.Mesh( geometry, material );
  115. mesh.position.y = - 50;
  116. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  117. scene.add( mesh );
  118. }
  119. //
  120. function onWindowResize() {
  121. const width = window.innerWidth;
  122. const height = window.innerHeight;
  123. camera.aspect = width / height;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( width, height );
  126. composer.setSize( width, height );
  127. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
  128. }
  129. function onDocumentMouseMove( event ) {
  130. mouseX = ( event.clientX - windowHalfX );
  131. mouseY = ( event.clientY - windowHalfY );
  132. }
  133. //
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. render();
  137. stats.update();
  138. }
  139. function render() {
  140. targetX = mouseX * .001;
  141. targetY = mouseY * .001;
  142. if ( mesh ) {
  143. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  144. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  145. }
  146. composer.render();
  147. }
  148. </script>
  149. </body>
  150. </html>