123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - portal</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 - portal
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { pass, color, mx_worley_noise_float, timerLocal, screenUV, vec2, uv, normalWorld, mx_fractal_noise_vec3 } from 'three/tsl';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- let camera, sceneMain, scenePortal, renderer;
- let clock;
- const mixers = [];
- init();
- function init() {
- //
- sceneMain = new THREE.Scene();
- sceneMain.background = new THREE.Color( 0x222222 );
- sceneMain.backgroundNode = normalWorld.y.mix( color( 0x0066ff ), color( 0xff0066 ) );
- scenePortal = new THREE.Scene();
- scenePortal.backgroundNode = mx_worley_noise_float( normalWorld.mul( 20 ).add( vec2( 0, timerLocal().oneMinus() ) ) ).mul( color( 0x0066ff ) );
- //
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 30 );
- camera.position.set( 2.5, 1, 3 );
- camera.position.multiplyScalar( .8 );
- camera.lookAt( 0, 1, 0 );
- clock = new THREE.Clock();
- // lights
- const light = new THREE.PointLight( 0xffffff, 1 );
- light.position.set( 0, 1, 5 );
- light.power = 17000;
- sceneMain.add( new THREE.HemisphereLight( 0xff0066, 0x0066ff, 7 ) );
- sceneMain.add( light );
- scenePortal.add( light.clone() );
- // models
- const loader = new GLTFLoader();
- loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
- const createModel = ( colorNode = null ) => {
- let object;
-
- if ( mixers.length === 0 ) {
- object = gltf.scene;
- } else {
- object = gltf.scene.clone();
- const children = object.children[ 0 ].children;
- const applyFX = ( index ) => {
- children[ index ].material = children[ index ].material.clone();
- children[ index ].material.colorNode = colorNode;
- children[ index ].material.wireframe = true;
- };
- applyFX( 0 );
- applyFX( 1 );
- }
- const mixer = new THREE.AnimationMixer( object );
- const action = mixer.clipAction( gltf.animations[ 6 ] );
- action.play();
- mixers.push( mixer );
- return object;
- };
- const colorNode = mx_fractal_noise_vec3( uv().mul( 20 ).add( timerLocal() ) );
- const modelMain = createModel();
- const modelPortal = createModel( colorNode );
- // model portal
- sceneMain.add( modelMain );
- scenePortal.add( modelPortal );
- } );
- // portal
- const geometry = new THREE.PlaneGeometry( 1.7, 2 );
- const material = new THREE.MeshBasicNodeMaterial();
- material.colorNode = pass( scenePortal, camera ).context( { getUV: () => screenUV } );
- material.opacityNode = uv().distance( .5 ).remapClamp( .3, .5 ).oneMinus();
- material.side = THREE.DoubleSide;
- material.transparent = true;
- const plane = new THREE.Mesh( geometry, material );
- plane.position.set( 0, 1, .8 );
- sceneMain.add( plane );
- // renderer
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- renderer.toneMapping = THREE.LinearToneMapping;
- renderer.toneMappingExposure = 0.15;
- document.body.appendChild( renderer.domElement );
- //
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 1, 0 );
- controls.update();
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- const delta = clock.getDelta();
- for ( const mixer of mixers ) {
- mixer.update( delta );
- }
- renderer.render( sceneMain, camera );
- }
- </script>
- </body>
- </html>
|