2
0

webgl_materials_normalmap.html 5.5 KB

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