webgl_materials_parallaxmap.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - parallax mapping</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">Three.js</a> parallax mapping <br />
  12. Oryginal shaders authors:
  13. <a href="http://sunandblackcat.com/tipFullView.php?topicid=28">Igor Dyhta</a>,
  14. <a href="http://mmikkelsen3d.blogspot.sk/2012/02/parallaxpoc-mapping-and-no-tangent.html">Morten S. Mikkelsen</a><br />
  15. Texture by <a href="http://agf81.deviantart.com/">AGF81</a>
  16. </div>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import Stats from './jsm/libs/stats.module.js';
  20. import { GUI } from './jsm/libs/dat.gui.module.js';
  21. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  22. import { ParallaxShader } from './jsm/shaders/ParallaxShader.js';
  23. var camera, scene, material, renderer, stats;
  24. var effectController = {
  25. 'mode': 'relief',
  26. 'scale': 0.005,
  27. 'minLayers': 20,
  28. 'maxLayers': 25
  29. };
  30. init();
  31. initGUI();
  32. animate();
  33. function init() {
  34. var container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
  37. camera.position.z = 2;
  38. scene = new THREE.Scene();
  39. //
  40. var shader = ParallaxShader;
  41. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  42. var parameters = {
  43. fragmentShader: shader.fragmentShader,
  44. vertexShader: shader.vertexShader,
  45. uniforms: uniforms
  46. };
  47. //
  48. var textureLoader = new THREE.TextureLoader();
  49. material = new THREE.ShaderMaterial( parameters );
  50. material.map = textureLoader.load( 'textures/brick_diffuse.jpg' );
  51. material.bumpMap = textureLoader.load( 'textures/brick_bump.jpg' );
  52. material.map.anisotropy = 4;
  53. material.bumpMap.anisotropy = 4;
  54. uniforms[ 'map' ].value = material.map;
  55. uniforms[ 'bumpMap' ].value = material.bumpMap;
  56. //
  57. var geometry = new THREE.BoxBufferGeometry( 1.0, 1.0, 1.0 );
  58. var mesh = new THREE.Mesh( geometry, material );
  59. scene.add( mesh );
  60. //
  61. renderer = new THREE.WebGLRenderer( { antialias: true } );
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. container.appendChild( renderer.domElement );
  65. renderer.outputEncoding = THREE.sRGBEncoding;
  66. //
  67. var controls = new OrbitControls( camera, renderer.domElement );
  68. //
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. //
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. function guiChanged() {
  75. var uniforms = material.uniforms;
  76. uniforms[ 'parallaxScale' ].value = - 1.0 * effectController.scale;
  77. uniforms[ 'parallaxMinLayers' ].value = effectController.minLayers;
  78. uniforms[ 'parallaxMaxLayers' ].value = effectController.maxLayers;
  79. material.defines = {};
  80. material.defines[ ParallaxShader.modes[ effectController.mode ] ] = '';
  81. material.needsUpdate = true;
  82. }
  83. function initGUI() {
  84. var gui = new GUI();
  85. gui.add( effectController, 'mode', Object.keys( ParallaxShader.modes ) ).onChange( guiChanged );
  86. gui.add( effectController, 'scale', 0.0, 0.01, 0.001 ).onChange( guiChanged );
  87. gui.add( effectController, 'minLayers', 1.0, 30, 1 ).onChange( guiChanged );
  88. gui.add( effectController, 'maxLayers', 1.0, 30, 1 ).onChange( guiChanged );
  89. guiChanged();
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. stats.update();
  100. }
  101. function render() {
  102. renderer.render( scene, camera );
  103. }
  104. </script>
  105. </body>
  106. </html>