webgpu_nodes_playground.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 type="importmap">
  41. {
  42. "imports": {
  43. "three": "../build/three.module.js"
  44. }
  45. }
  46. </script>
  47. <script type="module">
  48. import ImportMaps from './jsm/capabilities/ImportMaps.js';
  49. if ( ImportMaps.isAvailable() === false ) {
  50. document.body.appendChild( ImportMaps.getErrorMessage() );
  51. }
  52. </script>
  53. <script type="module">
  54. import * as THREE from 'three';
  55. import WebGPU from './jsm/capabilities/WebGPU.js';
  56. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  57. import { NodeEditor } from './jsm/node-editor/NodeEditor.js';
  58. import { StandardMaterialEditor } from './jsm/node-editor/materials/StandardMaterialEditor.js';
  59. import * as Nodes from './jsm/renderers/nodes/Nodes.js';
  60. import Stats from './jsm/libs/stats.module.js';
  61. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  62. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  63. let stats;
  64. let camera, scene, renderer;
  65. let model;
  66. let nodeLights;
  67. init().then( animate ).catch( error => console.error( error ) );
  68. async function init() {
  69. if ( WebGPU.isAvailable() === false ) {
  70. document.body.appendChild( WebGPU.getErrorMessage() );
  71. throw new Error( 'No WebGPU support' );
  72. }
  73. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  74. camera.position.set( 0.0, 300, 400 * 3 );
  75. scene = new THREE.Scene();
  76. scene.background = new THREE.Color( 0x333333 );
  77. // Lights
  78. const topLight = new THREE.PointLight( 0xF4F6F0, 1 );
  79. topLight.position.set( 0, 100000, 100000 );
  80. scene.add( topLight );
  81. const backLight = new THREE.PointLight( 0x0c1445, 1.4 );
  82. backLight.position.set( - 100, 20, - 260 );
  83. scene.add( backLight );
  84. nodeLights = Nodes.LightsNode.fromLights( [ topLight, backLight ] );
  85. renderer = new WebGPURenderer();
  86. renderer.setPixelRatio( window.devicePixelRatio );
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. document.body.appendChild( renderer.domElement );
  89. renderer.outputEncoding = THREE.sRGBEncoding;
  90. renderer.domElement.className = 'renderer';
  91. //
  92. stats = new Stats();
  93. document.body.appendChild( stats.dom );
  94. const controls = new OrbitControls( camera, renderer.domElement );
  95. controls.minDistance = 500;
  96. controls.maxDistance = 3000;
  97. window.addEventListener( 'resize', onWindowResize );
  98. onWindowResize();
  99. initEditor();
  100. return renderer.init();
  101. }
  102. function initEditor() {
  103. const nodeEditor = new NodeEditor();
  104. nodeEditor.addEventListener( 'new', () => {
  105. const materialEditor = new StandardMaterialEditor();
  106. materialEditor.setPosition( ( window.innerWidth / 2 ) - 150, 100 );
  107. nodeEditor.add( materialEditor );
  108. model.material = materialEditor.material;
  109. model.material.lightNode = nodeLights;
  110. } );
  111. nodeEditor.addEventListener( 'load', () => {
  112. const materialEditor = nodeEditor.nodes[ 0 ];
  113. materialEditor.update(); // need move to deserialization
  114. model.material = materialEditor.material;
  115. model.material.lightNode = nodeLights;
  116. } );
  117. document.body.appendChild( nodeEditor.domElement );
  118. const loaderFBX = new FBXLoader();
  119. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  120. const materialEditor = new StandardMaterialEditor();
  121. materialEditor.setPosition( ( window.innerWidth / 2 ) - 150, 100 ); // canvas position
  122. nodeEditor.add( materialEditor );
  123. model = object.children[ 0 ];
  124. model.position.set( 0, 0, 10 );
  125. model.scale.setScalar( 1 );
  126. model.material = materialEditor.material;
  127. model.material.lightNode = nodeLights;
  128. scene.add( model );
  129. } );
  130. }
  131. function onWindowResize() {
  132. const width = window.innerWidth;
  133. const height = window.innerHeight / 2;
  134. camera.aspect = width / height;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( width, height );
  137. }
  138. //
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. render();
  142. stats.update();
  143. }
  144. function render() {
  145. //if ( model ) model.rotation.y = performance.now() / 5000;
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>