webgl_materials_normalmap.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 material = new THREE.MeshPhongMaterial( {
  63. color: 0xdddddd,
  64. specular: 0x222222,
  65. shininess: 35,
  66. map: textureLoader.load( "models/gltf/LeePerrySmith/Map-COL.jpg" ),
  67. specularMap: textureLoader.load( "models/gltf/LeePerrySmith/Map-SPEC.jpg" ),
  68. normalMap: textureLoader.load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" ),
  69. normalScale: new THREE.Vector2( 0.8, 0.8 )
  70. } );
  71. loader = new GLTFLoader();
  72. loader.load( "models/gltf/LeePerrySmith/LeePerrySmith.glb", function ( gltf ) {
  73. createScene( gltf.scene.children[ 0 ].geometry, 100, material );
  74. } );
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. //
  79. renderer.gammaInput = true;
  80. renderer.gammaOutput = true;
  81. //
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. // COMPOSER
  85. renderer.autoClear = false;
  86. var renderModel = new RenderPass( scene, camera );
  87. var effectBleach = new ShaderPass( BleachBypassShader );
  88. var effectColor = new ShaderPass( ColorCorrectionShader );
  89. effectFXAA = new ShaderPass( FXAAShader );
  90. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  91. effectBleach.uniforms[ 'opacity' ].value = 0.4;
  92. effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
  93. effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
  94. composer = new EffectComposer( renderer );
  95. composer.addPass( renderModel );
  96. composer.addPass( effectFXAA );
  97. composer.addPass( effectBleach );
  98. composer.addPass( effectColor );
  99. // EVENTS
  100. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  101. window.addEventListener( 'resize', onWindowResize, false );
  102. }
  103. function createScene( geometry, scale, material ) {
  104. mesh = new THREE.Mesh( geometry, material );
  105. mesh.position.y = - 50;
  106. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  107. scene.add( mesh );
  108. }
  109. //
  110. function onWindowResize() {
  111. var width = window.innerWidth;
  112. var height = window.innerHeight;
  113. camera.aspect = width / height;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( width, height );
  116. composer.setSize( width, height );
  117. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
  118. }
  119. function onDocumentMouseMove( event ) {
  120. mouseX = ( event.clientX - windowHalfX );
  121. mouseY = ( event.clientY - windowHalfY );
  122. }
  123. //
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. stats.update();
  128. }
  129. function render() {
  130. targetX = mouseX * .001;
  131. targetY = mouseY * .001;
  132. if ( mesh ) {
  133. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  134. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  135. }
  136. composer.render();
  137. }
  138. </script>
  139. </body>
  140. </html>