2
0

webgpu_nodes_playground.html 5.6 KB

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