webgl_materials_normalmap.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 Stats from 'three/addons/libs/stats.module.js';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  27. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  28. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  29. import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js';
  30. import { ColorCorrectionShader } from 'three/addons/shaders/ColorCorrectionShader.js';
  31. import { FXAAShader } from 'three/addons/shaders/FXAAShader.js';
  32. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  33. let container, stats, loader;
  34. let camera, scene, renderer;
  35. let mesh;
  36. let directionalLight, pointLight, ambientLight;
  37. let mouseX = 0;
  38. let mouseY = 0;
  39. let targetX = 0;
  40. let targetY = 0;
  41. const windowHalfX = window.innerWidth / 2;
  42. const windowHalfY = window.innerHeight / 2;
  43. let composer, effectFXAA;
  44. init();
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  49. camera.position.z = 12;
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x494949 );
  52. // LIGHTS
  53. ambientLight = new THREE.AmbientLight( 0xffffff );
  54. scene.add( ambientLight );
  55. pointLight = new THREE.PointLight( 0xffffff, 30 );
  56. pointLight.position.set( 0, 0, 6 );
  57. scene.add( pointLight );
  58. directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  59. directionalLight.position.set( 1, - 0.5, - 1 );
  60. scene.add( directionalLight );
  61. const textureLoader = new THREE.TextureLoader();
  62. const diffuseMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-COL.jpg' );
  63. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  64. const specularMap = textureLoader.load( 'models/gltf/LeePerrySmith/Map-SPEC.jpg' );
  65. specularMap.colorSpace = THREE.SRGBColorSpace;
  66. const normalMap = textureLoader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' );
  67. const material = new THREE.MeshPhongMaterial( {
  68. color: 0xefefef,
  69. specular: 0x222222,
  70. shininess: 35,
  71. map: diffuseMap,
  72. specularMap: specularMap,
  73. normalMap: normalMap,
  74. normalScale: new THREE.Vector2( 0.8, 0.8 )
  75. } );
  76. loader = new GLTFLoader();
  77. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  78. const geometry = gltf.scene.children[ 0 ].geometry;
  79. mesh = new THREE.Mesh( geometry, material );
  80. mesh.position.y = - 0.5;
  81. scene.add( mesh );
  82. } );
  83. renderer = new THREE.WebGLRenderer();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.setAnimationLoop( animate );
  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 outputPass = new OutputPass();
  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( outputPass );
  107. composer.addPass( effectFXAA );
  108. // EVENTS
  109. document.addEventListener( 'mousemove', onDocumentMouseMove );
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. //
  113. function onWindowResize() {
  114. const width = window.innerWidth;
  115. const height = window.innerHeight;
  116. camera.aspect = width / height;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( width, height );
  119. composer.setSize( width, height );
  120. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
  121. }
  122. function onDocumentMouseMove( event ) {
  123. mouseX = ( event.clientX - windowHalfX );
  124. mouseY = ( event.clientY - windowHalfY );
  125. }
  126. //
  127. function animate() {
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. targetX = mouseX * .001;
  133. targetY = mouseY * .001;
  134. if ( mesh ) {
  135. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  136. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  137. }
  138. composer.render();
  139. }
  140. </script>
  141. </body>
  142. </html>