webgl_materials_normalmap.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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="http://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. var container, stats, loader;
  33. var camera, scene, renderer;
  34. var mesh;
  35. var directionalLight, pointLight, ambientLight;
  36. var mouseX = 0;
  37. var mouseY = 0;
  38. var targetX = 0;
  39. var targetY = 0;
  40. var windowHalfX = window.innerWidth / 2;
  41. var windowHalfY = window.innerHeight / 2;
  42. var composer, effectFXAA;
  43. init();
  44. animate();
  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 = 1200;
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x111111 );
  52. // LIGHTS
  53. ambientLight = new THREE.AmbientLight( 0x444444 );
  54. scene.add( ambientLight );
  55. pointLight = new THREE.PointLight( 0xffffff, 1.25, 1000 );
  56. pointLight.position.set( 0, 0, 600 );
  57. scene.add( pointLight );
  58. directionalLight = new THREE.DirectionalLight( 0xffffff );
  59. directionalLight.position.set( 1, - 0.5, - 1 );
  60. scene.add( directionalLight );
  61. var textureLoader = new THREE.TextureLoader();
  62. var diffuseMap = textureLoader.load( "models/gltf/LeePerrySmith/Map-COL.jpg" );
  63. diffuseMap.encoding = THREE.sRGBEncoding;
  64. var specularMap = textureLoader.load( "models/gltf/LeePerrySmith/Map-SPEC.jpg" );
  65. specularMap.encoding = THREE.sRGBEncoding;
  66. var normalMap = textureLoader.load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
  67. var material = new THREE.MeshPhongMaterial( {
  68. color: 0xdddddd,
  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. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  79. } );
  80. renderer = new THREE.WebGLRenderer();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. renderer.gammaOutput = true;
  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. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  94. effectBleach.uniforms[ 'opacity' ].value = 0.4;
  95. effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
  96. effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
  97. composer = new EffectComposer( renderer );
  98. composer.addPass( renderModel );
  99. composer.addPass( effectFXAA );
  100. composer.addPass( effectBleach );
  101. composer.addPass( effectColor );
  102. // EVENTS
  103. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. }
  106. function createScene( geometry, scale, material ) {
  107. mesh = new THREE.Mesh( geometry, material );
  108. mesh.position.y = - 50;
  109. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  110. scene.add( mesh );
  111. }
  112. //
  113. function onWindowResize() {
  114. var width = window.innerWidth;
  115. var 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. requestAnimationFrame( animate );
  129. render();
  130. stats.update();
  131. }
  132. function render() {
  133. targetX = mouseX * .001;
  134. targetY = mouseY * .001;
  135. if ( mesh ) {
  136. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  137. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  138. }
  139. composer.render();
  140. }
  141. </script>
  142. </body>
  143. </html>