123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js - webgpu - node playground</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">
- <link rel="stylesheet" href="fonts/open-sans/open-sans.css" type="text/css"/>
- <link rel="stylesheet" href="fonts/tabler-icons/tabler-icons.min.css" type="text/css"/>
- <style>
- body {
- overflow: hidden;
- width: 100%;
- height: 100%;
- }
- .renderer {
- position: absolute;
- top: 0;
- left: 0;
- height: 100%;
- width: 100%;
- }
- flow {
- position: absolute;
- top: 0;
- left: 0;
- height: 100%;
- width: 100%;
- box-shadow: inset 0 0 20px 0px #000000;
- pointer-events: none;
- }
- flow f-canvas {
- pointer-events: auto;
- }
- flow f-canvas:not(.focusing) {
- background: #191919ed;
- }
- </style>
- </head>
- <body>
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <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 { toneMapping, uniform, MeshBasicNodeMaterial, PointsNodeMaterial } from 'three/nodes';
- import WebGPU from 'three/addons/capabilities/WebGPU.js';
- import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
- import { NodeEditor } from 'three/addons/node-editor/NodeEditor.js';
- import { MeshEditor } from 'three/addons/node-editor/scene/MeshEditor.js';
- import { StandardMaterialEditor } from 'three/addons/node-editor/materials/StandardMaterialEditor.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
- // Use PreviewEditor in WebGL for now
- import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
- let camera, scene, renderer;
- let model;
- let nodeEditor;
- init();
- function init() {
- if ( WebGPU.isAvailable() === false ) {
- document.body.appendChild( WebGPU.getErrorMessage() );
- throw new Error( 'No WebGPU support' );
- }
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.01, 100 );
- camera.position.set( 0.0, 3, 4 * 3 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x333333 );
- // Lights
- const topLight = new THREE.PointLight( 0xF4F6F0, 1, 100 );
- topLight.power = 4500;
- topLight.position.set( 0, 10, 10 );
- scene.add( topLight );
- const backLight = new THREE.PointLight( 0x0c1445, 1, 100 );
- backLight.power = 1000;
- backLight.position.set( - 1, .2, - 2.6 );
- scene.add( backLight );
- renderer = new WebGPURenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( render );
- renderer.outputEncoding = THREE.sRGBEncoding;
- renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, 1 );
- document.body.appendChild( renderer.domElement );
- renderer.domElement.className = 'renderer';
- //
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 5;
- controls.maxDistance = 30;
- window.addEventListener( 'resize', onWindowResize );
- initEditor();
- onWindowResize();
- }
- function initEditor() {
- nodeEditor = new NodeEditor( scene );
- const reset = () => {
- const meshEditor = new MeshEditor( model );
- const materialEditor = new StandardMaterialEditor();
- nodeEditor.add( meshEditor );
- nodeEditor.add( materialEditor );
- nodeEditor.centralizeNode( meshEditor );
- const { x, y } = meshEditor.getPosition();
- meshEditor.setPosition( x + 250, y );
- materialEditor.setPosition( x - 250, y );
- meshEditor.material.connect( materialEditor );
- };
- nodeEditor.addEventListener( 'new', reset );
- document.body.appendChild( nodeEditor.domElement );
- const loaderFBX = new FBXLoader();
- loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
- const defaultMaterial = new MeshBasicNodeMaterial();
- defaultMaterial.colorNode = uniform( 0 );
- const sphere = new THREE.Mesh( new THREE.SphereGeometry( 2, 32, 16 ), defaultMaterial );
- sphere.name = 'Sphere';
- sphere.position.set( 5, 0, - 5 );
- scene.add( sphere );
- const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), defaultMaterial );
- box.name = 'Box';
- box.position.set( - 5, 0, - 5 );
- scene.add( box );
- const defaultPointsMaterial = new PointsNodeMaterial();
- defaultPointsMaterial.colorNode = uniform( 0 );
- const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 1, .3, 100, 16 ), defaultPointsMaterial );
- torusKnot.name = 'Torus Knot ( Points )';
- torusKnot.position.set( 0, 0, - 5 );
- scene.add( torusKnot );
- model = object.children[ 0 ];
- model.position.set( 0, 0, .1 );
- model.scale.setScalar( .01 );
- model.material = defaultMaterial;
- scene.add( model );
- reset();
- } );
- }
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- nodeEditor.setSize( width, height );
- }
- //
- function render() {
- //if ( model ) model.rotation.y = performance.now() / 5000;
- nodeFrame.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|