webgpu_parallax_uv.html 4.2 KB

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