webgpu_nodes_playground.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgpu - node playground</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. <link rel="stylesheet" href="fonts/open-sans/open-sans.css" type="text/css"/>
  9. <link rel="stylesheet" href="fonts/tabler-icons/tabler-icons.min.css" type="text/css"/>
  10. <style>
  11. body {
  12. overflow: hidden;
  13. width: 100%;
  14. height: 100%;
  15. }
  16. .renderer {
  17. position: absolute;
  18. top: 0;
  19. left: 0;
  20. height: 100%;
  21. width: 100%;
  22. }
  23. flow {
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. height: 100%;
  28. width: 100%;
  29. box-shadow: inset 0 0 20px 0px #000000;
  30. pointer-events: none;
  31. }
  32. flow f-canvas {
  33. pointer-events: auto;
  34. }
  35. flow f-canvas:not(.focusing) {
  36. background: #191919ed;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  42. <script type="importmap">
  43. {
  44. "imports": {
  45. "three": "../build/three.module.js",
  46. "three/addons/": "./jsm/",
  47. "three/nodes": "./jsm/nodes/Nodes.js"
  48. }
  49. }
  50. </script>
  51. <script type="module">
  52. import * as THREE from 'three';
  53. import * as Nodes from 'three/nodes';
  54. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  55. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  56. import { NodeEditor } from 'three/addons/node-editor/NodeEditor.js';
  57. import { MeshEditor } from 'three/addons/node-editor/scene/MeshEditor.js';
  58. import { StandardMaterialEditor } from 'three/addons/node-editor/materials/StandardMaterialEditor.js';
  59. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  60. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  61. // Use PreviewEditor in WebGL for now
  62. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  63. let camera, scene, renderer;
  64. let model;
  65. let nodeEditor;
  66. init().then( animate ).catch( error => console.error( error ) );
  67. async function init() {
  68. if ( WebGPU.isAvailable() === false ) {
  69. document.body.appendChild( WebGPU.getErrorMessage() );
  70. throw new Error( 'No WebGPU support' );
  71. }
  72. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  73. camera.position.set( 0.0, 300, 400 * 3 );
  74. scene = new THREE.Scene();
  75. scene.background = new THREE.Color( 0x333333 );
  76. // Lights
  77. const topLight = new THREE.PointLight( 0xF4F6F0, 1 );
  78. topLight.position.set( 0, 1000, 1000 );
  79. scene.add( topLight );
  80. const backLight = new THREE.PointLight( 0x0c1445, 1 );
  81. backLight.position.set( - 100, 20, - 260 );
  82. scene.add( backLight );
  83. renderer = new WebGPURenderer();
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. document.body.appendChild( renderer.domElement );
  87. renderer.outputEncoding = THREE.sRGBEncoding;
  88. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 4000 );
  89. renderer.domElement.className = 'renderer';
  90. //
  91. const controls = new OrbitControls( camera, renderer.domElement );
  92. controls.minDistance = 500;
  93. controls.maxDistance = 3000;
  94. window.addEventListener( 'resize', onWindowResize );
  95. initEditor();
  96. onWindowResize();
  97. return renderer.init();
  98. }
  99. function initEditor() {
  100. nodeEditor = new NodeEditor( scene );
  101. const reset = () => {
  102. const meshEditor = new MeshEditor( model );
  103. const materialEditor = new StandardMaterialEditor();
  104. nodeEditor.add( meshEditor );
  105. nodeEditor.add( materialEditor );
  106. nodeEditor.centralizeNode( meshEditor );
  107. const { x, y } = meshEditor.getPosition();
  108. meshEditor.setPosition( x + 250, y );
  109. materialEditor.setPosition( x - 250, y );
  110. meshEditor.material.connect( materialEditor );
  111. };
  112. nodeEditor.addEventListener( 'new', reset );
  113. document.body.appendChild( nodeEditor.domElement );
  114. const loaderFBX = new FBXLoader();
  115. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  116. const defaultMaterial = new Nodes.MeshBasicNodeMaterial();
  117. defaultMaterial.colorNode = new Nodes.UniformNode( 0 );
  118. const sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 32, 16 ), defaultMaterial );
  119. sphere.name = 'Sphere';
  120. sphere.position.set( 500, 0, - 500 );
  121. scene.add( sphere );
  122. const box = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), defaultMaterial );
  123. box.name = 'Box';
  124. box.position.set( - 500, 0, - 500 );
  125. scene.add( box );
  126. const defaultPointsMaterial = new Nodes.PointsNodeMaterial();
  127. defaultPointsMaterial.colorNode = new Nodes.UniformNode( 0 );
  128. const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 100, 30, 100, 16 ), defaultPointsMaterial );
  129. torusKnot.name = 'Torus Knot ( Points )';
  130. torusKnot.position.set( 0, 0, - 500 );
  131. scene.add( torusKnot );
  132. model = object.children[ 0 ];
  133. model.position.set( 0, 0, 10 );
  134. model.scale.setScalar( 1 );
  135. model.material = defaultMaterial;
  136. scene.add( model );
  137. reset();
  138. } );
  139. }
  140. function onWindowResize() {
  141. const width = window.innerWidth;
  142. const height = window.innerHeight;
  143. camera.aspect = width / height;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( width, height );
  146. nodeEditor.setSize( width, height );
  147. }
  148. //
  149. function animate() {
  150. requestAnimationFrame( animate );
  151. nodeFrame.update();
  152. render();
  153. }
  154. function render() {
  155. //if ( model ) model.rotation.y = performance.now() / 5000;
  156. renderer.render( scene, camera );
  157. }
  158. </script>
  159. </body>
  160. </html>