webgl_materials_normalmap.html 5.9 KB

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