webgl_materials_normalmap.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. let container, stats, loader;
  37. let camera, scene, renderer;
  38. let mesh;
  39. let directionalLight, pointLight, ambientLight;
  40. let mouseX = 0;
  41. let mouseY = 0;
  42. let targetX = 0;
  43. let targetY = 0;
  44. const windowHalfX = window.innerWidth / 2;
  45. const windowHalfY = window.innerHeight / 2;
  46. let composer, effectFXAA;
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  53. camera.position.z = 1200;
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x111111 );
  56. // LIGHTS
  57. ambientLight = new THREE.AmbientLight( 0x444444 );
  58. scene.add( ambientLight );
  59. pointLight = new THREE.PointLight( 0xffffff, 2, 1000 );
  60. pointLight.position.set( 0, 0, 600 );
  61. scene.add( pointLight );
  62. directionalLight = new THREE.DirectionalLight( 0xffffff );
  63. directionalLight.position.set( 1, - 0.5, - 1 );
  64. scene.add( directionalLight );
  65. const textureLoader = new THREE.TextureLoader();
  66. const diffuseMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-COL.jpg' );
  67. diffuseMap.encoding = THREE.sRGBEncoding;
  68. const specularMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-SPEC.jpg' );
  69. specularMap.encoding = THREE.sRGBEncoding;
  70. const normalMap = textureLoader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' );
  71. const material = new THREE.MeshPhongMaterial( {
  72. color: 0xdddddd,
  73. specular: 0x222222,
  74. shininess: 35,
  75. map: diffuseMap,
  76. specularMap: specularMap,
  77. normalMap: normalMap,
  78. normalScale: new THREE.Vector2( 0.8, 0.8 )
  79. } );
  80. loader = new GLTFLoader();
  81. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  82. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  83. } );
  84. renderer = new THREE.WebGLRenderer();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. container.appendChild( renderer.domElement );
  87. //
  88. stats = new Stats();
  89. container.appendChild( stats.dom );
  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 gammaCorrection = new ShaderPass( GammaCorrectionShader );
  96. effectFXAA = new ShaderPass( FXAAShader );
  97. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  98. effectBleach.uniforms[ 'opacity' ].value = 0.2;
  99. effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
  100. effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
  101. const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { type: THREE.HalfFloatType, depthTexture: new THREE.DepthTexture() } );
  102. composer = new EffectComposer( renderer, renderTarget );
  103. composer.addPass( renderModel );
  104. composer.addPass( effectBleach );
  105. composer.addPass( effectColor );
  106. composer.addPass( gammaCorrection );
  107. composer.addPass( effectFXAA );
  108. // EVENTS
  109. document.addEventListener( 'mousemove', onDocumentMouseMove );
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function createScene( geometry, scale, material ) {
  113. mesh = new THREE.Mesh( geometry, material );
  114. mesh.position.y = - 50;
  115. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  116. scene.add( mesh );
  117. }
  118. //
  119. function onWindowResize() {
  120. const width = window.innerWidth;
  121. const height = window.innerHeight;
  122. camera.aspect = width / height;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( width, height );
  125. composer.setSize( width, height );
  126. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
  127. }
  128. function onDocumentMouseMove( event ) {
  129. mouseX = ( event.clientX - windowHalfX );
  130. mouseY = ( event.clientY - windowHalfY );
  131. }
  132. //
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. render();
  136. stats.update();
  137. }
  138. function render() {
  139. targetX = mouseX * .001;
  140. targetY = mouseY * .001;
  141. if ( mesh ) {
  142. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  143. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  144. }
  145. composer.render();
  146. }
  147. </script>
  148. </body>
  149. </html>