webgpu_parallax_uv.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - parallax uv</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. <meta property="og:title" content="three.js webgpu - parallax uv">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_parallax_uv.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_parallax_uv.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Parallax UV</span>
  18. </div>
  19. <small>
  20. Textures by <a href="https://ambientcg.com/view?id=Ice002" target="_blank" rel="noopener">ambientCG</a>.
  21. HDR by <a href="https://hdri-skies.com/free-hdris/" target="_blank" rel="noopener">HDRI Skies</a>.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { texture, parallaxUV, blendOverlay, uv, normalMap, uniform } from 'three/tsl';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  39. import { Inspector } from 'three/addons/inspector/Inspector.js';
  40. let camera, scene, renderer;
  41. let controls;
  42. init();
  43. async function init() {
  44. // scene
  45. scene = new THREE.Scene();
  46. // camera
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, .1, 100 );
  48. camera.position.set( 15, 7, 15 );
  49. // environment
  50. const environmentTexture = await new HDRLoader()
  51. .loadAsync( './textures/equirectangular/752-hdri-skies-com_1k.hdr' );
  52. environmentTexture.mapping = THREE.EquirectangularReflectionMapping;
  53. scene.environment = environmentTexture;
  54. scene.background = environmentTexture;
  55. scene.backgroundBlurriness = 0.4;
  56. // textures
  57. const loader = new THREE.TextureLoader();
  58. const topTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_Color.jpg' );
  59. topTexture.colorSpace = THREE.SRGBColorSpace;
  60. topTexture.wrapS = THREE.RepeatWrapping;
  61. topTexture.wrapT = THREE.RepeatWrapping;
  62. const roughnessTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_Roughness.jpg' );
  63. roughnessTexture.colorSpace = THREE.NoColorSpace;
  64. roughnessTexture.wrapS = THREE.RepeatWrapping;
  65. roughnessTexture.wrapT = THREE.RepeatWrapping;
  66. const normalTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_NormalGL.jpg' );
  67. normalTexture.colorSpace = THREE.NoColorSpace;
  68. normalTexture.wrapS = THREE.RepeatWrapping;
  69. normalTexture.wrapT = THREE.RepeatWrapping;
  70. const displaceTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_Displacement.jpg' );
  71. displaceTexture.colorSpace = THREE.NoColorSpace;
  72. displaceTexture.wrapS = THREE.RepeatWrapping;
  73. displaceTexture.wrapT = THREE.RepeatWrapping;
  74. //
  75. const bottomTexture = await loader.loadAsync( 'textures/ambientcg/Ice003_1K-JPG_Color.jpg' );
  76. bottomTexture.colorSpace = THREE.SRGBColorSpace;
  77. bottomTexture.wrapS = THREE.RepeatWrapping;
  78. bottomTexture.wrapT = THREE.RepeatWrapping;
  79. // parallax effect
  80. const scaleUV = uniform( 3 );
  81. const scaledUV = uv().mul( scaleUV );
  82. const parallaxScale = uniform( 0.5 ); // parallax scale
  83. const offsetUV = texture( displaceTexture, scaledUV ).mul( parallaxScale );
  84. const parallaxUVOffset = parallaxUV( scaledUV, offsetUV );
  85. const parallaxResult = texture( bottomTexture, parallaxUVOffset );
  86. const iceNode = blendOverlay( texture( topTexture, scaledUV ), parallaxResult );
  87. // material
  88. const material = new THREE.MeshStandardNodeMaterial();
  89. material.colorNode = iceNode.mul( 5 ); // increase the color intensity to 5 ( contrast )
  90. material.roughnessNode = texture( roughnessTexture, scaledUV );
  91. material.normalNode = normalMap( texture( normalTexture, scaledUV ) );
  92. material.metalness = 0;
  93. const geometry = new THREE.CircleGeometry( 25, 64 );
  94. const ground = new THREE.Mesh( geometry, material );
  95. ground.rotateX( - Math.PI / 2 );
  96. scene.add( ground );
  97. // renderer
  98. renderer = new THREE.WebGPURenderer( { antialias: true } );
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.setAnimationLoop( animate );
  102. renderer.toneMapping = THREE.ReinhardToneMapping;
  103. renderer.toneMappingExposure = 6;
  104. renderer.inspector = new Inspector();
  105. document.body.appendChild( renderer.domElement );
  106. // gui
  107. const gui = renderer.inspector.createParameters( 'Scene' );
  108. gui.add( scene, 'backgroundBlurriness', 0, 1 ).name( 'Background Blurriness' );
  109. gui.add( parallaxScale, 'value', .2, .5 ).name( 'Parallax Scale' );
  110. gui.add( scaleUV, 'value', 1, 5 ).name( 'UV Scale' );
  111. // controls
  112. controls = new OrbitControls( camera, renderer.domElement );
  113. controls.target.set( 0, 0, 0 );
  114. controls.maxDistance = 40;
  115. controls.minDistance = 10;
  116. controls.autoRotate = true;
  117. controls.autoRotateSpeed = - 1;
  118. controls.update();
  119. window.addEventListener( 'resize', onWindowResize );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function animate() {
  127. controls.update();
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>
粤ICP备19079148号