|
@@ -0,0 +1,153 @@
|
|
|
|
+<!DOCTYPE html>
|
|
|
|
+<html lang="en">
|
|
|
|
+ <head>
|
|
|
|
+ <title>three.js webgpu - parallax uv</title>
|
|
|
|
+ <meta charset="utf-8">
|
|
|
|
+ <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
|
|
+ <link type="text/css" rel="stylesheet" href="main.css">
|
|
|
|
+ </head>
|
|
|
|
+ <body>
|
|
|
|
+
|
|
|
|
+ <div id="info">
|
|
|
|
+ <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - parallax uv<br />
|
|
|
|
+ Textures by <a href="https://ambientcg.com/view?id=Ice002" target="_blank" rel="noopener">ambientCG</a><br />
|
|
|
|
+
|
|
|
|
+ </div>
|
|
|
|
+
|
|
|
|
+ <script type="importmap">
|
|
|
|
+ {
|
|
|
|
+ "imports": {
|
|
|
|
+ "three": "../build/three.module.js",
|
|
|
|
+ "three/addons/": "./jsm/",
|
|
|
|
+ "three/nodes": "./jsm/nodes/Nodes.js"
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ </script>
|
|
|
|
+
|
|
|
|
+ <script type="module">
|
|
|
|
+
|
|
|
|
+ import * as THREE from 'three';
|
|
|
|
+ import { MeshStandardNodeMaterial, texture, parallaxUV, uv } from 'three/nodes';
|
|
|
|
+
|
|
|
|
+ import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
|
|
|
|
+
|
|
|
|
+ import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
|
|
+
|
|
|
|
+ let camera, scene, renderer;
|
|
|
|
+
|
|
|
|
+ let controls;
|
|
|
|
+
|
|
|
|
+ init();
|
|
|
|
+
|
|
|
|
+ function init() {
|
|
|
|
+
|
|
|
|
+ // scene
|
|
|
|
+
|
|
|
|
+ scene = new THREE.Scene();
|
|
|
|
+
|
|
|
|
+ // camera
|
|
|
|
+
|
|
|
|
+ camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, .1, 50 );
|
|
|
|
+ camera.position.set( 10, 14, 10 );
|
|
|
|
+
|
|
|
|
+ // environment
|
|
|
|
+
|
|
|
|
+ const environmentTexture = new THREE.CubeTextureLoader()
|
|
|
|
+ .setPath( './textures/cube/Park2/' )
|
|
|
|
+ .load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ scene.environment = environmentTexture;
|
|
|
|
+ scene.background = environmentTexture;
|
|
|
|
+
|
|
|
|
+ // textures
|
|
|
|
+
|
|
|
|
+ const loader = new THREE.TextureLoader();
|
|
|
|
+
|
|
|
|
+ const topTexture = loader.load( 'textures/ambientcg/Ice002_1K-JPG_Color.jpg' );
|
|
|
|
+ topTexture.colorSpace = THREE.SRGBColorSpace;
|
|
|
|
+
|
|
|
|
+ const roughnessTexture = loader.load( 'textures/ambientcg/Ice002_1K-JPG_Roughness.jpg' );
|
|
|
|
+ roughnessTexture.colorSpace = THREE.SRGBColorSpace;
|
|
|
|
+
|
|
|
|
+ const normalTexture = loader.load( 'textures/ambientcg/Ice002_1K-JPG_NormalGL.jpg' );
|
|
|
|
+ normalTexture.colorSpace = THREE.NoColorSpace;
|
|
|
|
+
|
|
|
|
+ const displaceTexture = loader.load( 'textures/ambientcg/Ice002_1K-JPG_Displacement.jpg' );
|
|
|
|
+ displaceTexture.colorSpace = THREE.SRGBColorSpace;
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+ const bottomTexture = loader.load( 'textures/ambientcg/Ice003_1K-JPG_Color.jpg' );
|
|
|
|
+ bottomTexture.colorSpace = THREE.SRGBColorSpace;
|
|
|
|
+ bottomTexture.wrapS = THREE.RepeatWrapping;
|
|
|
|
+ bottomTexture.wrapT = THREE.RepeatWrapping;
|
|
|
|
+
|
|
|
|
+ // paralax effect
|
|
|
|
+
|
|
|
|
+ const parallaxScale = .3;
|
|
|
|
+ const offsetUV = texture( displaceTexture ).mul( parallaxScale );
|
|
|
|
+
|
|
|
|
+ const parallaxUVOffset = parallaxUV( uv(), offsetUV );
|
|
|
|
+ const parallaxResult = texture( bottomTexture, parallaxUVOffset );
|
|
|
|
+
|
|
|
|
+ const iceNode = texture( topTexture ).overlay( parallaxResult );
|
|
|
|
+
|
|
|
|
+ // material
|
|
|
|
+
|
|
|
|
+ const material = new MeshStandardNodeMaterial();
|
|
|
|
+ material.colorNode = iceNode.mul( 5 ); // increase the color intensity to 5 ( contrast )
|
|
|
|
+ material.roughnessNode = texture( roughnessTexture );
|
|
|
|
+ material.normalMap = normalTexture;
|
|
|
|
+ material.metalness = 0;
|
|
|
|
+
|
|
|
|
+ const geometry = new THREE.BoxGeometry( 10, 10, 10 );
|
|
|
|
+
|
|
|
|
+ const ground = new THREE.Mesh( geometry, material );
|
|
|
|
+ ground.rotateX( - Math.PI / 2 );
|
|
|
|
+ scene.add( ground );
|
|
|
|
+
|
|
|
|
+ // renderer
|
|
|
|
+
|
|
|
|
+ renderer = new WebGPURenderer( { antialias: true } );
|
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
+ renderer.setAnimationLoop( animate );
|
|
|
|
+ renderer.toneMapping = THREE.ReinhardToneMapping;
|
|
|
|
+ renderer.toneMappingExposure = 6;
|
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
|
+
|
|
|
|
+ // controls
|
|
|
|
+
|
|
|
|
+ controls = new OrbitControls( camera, renderer.domElement );
|
|
|
|
+ controls.target.set( 0, 0, 0 );
|
|
|
|
+ controls.maxDistance = 40;
|
|
|
|
+ controls.minDistance = 10;
|
|
|
|
+ controls.autoRotate = true;
|
|
|
|
+ controls.autoRotateSpeed = - 1;
|
|
|
|
+ controls.update();
|
|
|
|
+
|
|
|
|
+ window.addEventListener( 'resize', onWindowResize );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function onWindowResize() {
|
|
|
|
+
|
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
|
+
|
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function animate() {
|
|
|
|
+
|
|
|
|
+ controls.update();
|
|
|
|
+
|
|
|
|
+ renderer.render( scene, camera );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ </script>
|
|
|
|
+ </body>
|
|
|
|
+</html>
|