webgpu_nodes_playground.html 5.9 KB

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