webgl_materials_normalmap.html 5.7 KB

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