webgpu_parallax_uv.html 5.6 KB

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