webgl_materials_normalmap.html 5.8 KB

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