1
0

webgpu_parallax_uv.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { texture, parallaxUV, blendOverlay, uv } from 'three/tsl';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. let camera, scene, renderer;
  34. let controls;
  35. init();
  36. async function init() {
  37. // scene
  38. scene = new THREE.Scene();
  39. // camera
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, .1, 50 );
  41. camera.position.set( 10, 14, 10 );
  42. // environment
  43. const environmentTexture = await new THREE.CubeTextureLoader()
  44. .setPath( './textures/cube/Park2/' )
  45. .loadAsync( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
  46. scene.environment = environmentTexture;
  47. scene.background = environmentTexture;
  48. // textures
  49. const loader = new THREE.TextureLoader();
  50. const topTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_Color.jpg' );
  51. topTexture.colorSpace = THREE.SRGBColorSpace;
  52. const roughnessTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_Roughness.jpg' );
  53. roughnessTexture.colorSpace = THREE.NoColorSpace;
  54. const normalTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_NormalGL.jpg' );
  55. normalTexture.colorSpace = THREE.NoColorSpace;
  56. const displaceTexture = await loader.loadAsync( 'textures/ambientcg/Ice002_1K-JPG_Displacement.jpg' );
  57. displaceTexture.colorSpace = THREE.NoColorSpace;
  58. //
  59. const bottomTexture = await loader.loadAsync( 'textures/ambientcg/Ice003_1K-JPG_Color.jpg' );
  60. bottomTexture.colorSpace = THREE.SRGBColorSpace;
  61. bottomTexture.wrapS = THREE.RepeatWrapping;
  62. bottomTexture.wrapT = THREE.RepeatWrapping;
  63. // parallax effect
  64. const parallaxScale = .3;
  65. const offsetUV = texture( displaceTexture ).mul( parallaxScale );
  66. const parallaxUVOffset = parallaxUV( uv(), offsetUV );
  67. const parallaxResult = texture( bottomTexture, parallaxUVOffset );
  68. const iceNode = blendOverlay( texture( topTexture ), parallaxResult );
  69. // material
  70. const material = new THREE.MeshStandardNodeMaterial();
  71. material.colorNode = iceNode.mul( 5 ); // increase the color intensity to 5 ( contrast )
  72. material.roughnessNode = texture( roughnessTexture );
  73. material.normalMap = normalTexture;
  74. material.metalness = 0;
  75. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  76. const ground = new THREE.Mesh( geometry, material );
  77. ground.rotateX( - Math.PI / 2 );
  78. scene.add( ground );
  79. // renderer
  80. renderer = new THREE.WebGPURenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setAnimationLoop( animate );
  84. renderer.toneMapping = THREE.ReinhardToneMapping;
  85. renderer.toneMappingExposure = 6;
  86. document.body.appendChild( renderer.domElement );
  87. // controls
  88. controls = new OrbitControls( camera, renderer.domElement );
  89. controls.target.set( 0, 0, 0 );
  90. controls.maxDistance = 40;
  91. controls.minDistance = 10;
  92. controls.autoRotate = true;
  93. controls.autoRotateSpeed = - 1;
  94. controls.update();
  95. window.addEventListener( 'resize', onWindowResize );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. function animate() {
  103. controls.update();
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>
粤ICP备19079148号