webgl_nodes_playground.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - node-editor 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 rel="stylesheet" href="fonts/open-sans/open-sans.css" type="text/css"/>
  8. <link rel="stylesheet" href="fonts/tabler-icons/tabler-icons.min.css" type="text/css"/>
  9. <link type="text/css" rel="stylesheet" href="main.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> - WebGL - 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/"
  45. }
  46. }
  47. </script>
  48. <script type="module">
  49. import * as THREE from 'three';
  50. import * as Nodes from 'three-nodes/Nodes.js';
  51. import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';
  52. import { NodeEditor } from './jsm/node-editor/NodeEditor.js';
  53. import { MeshEditor } from './jsm/node-editor/scene/MeshEditor.js';
  54. import Stats from './jsm/libs/stats.module.js';
  55. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  56. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  57. let stats;
  58. let camera, scene, renderer;
  59. let model;
  60. init();
  61. animate();
  62. function init() {
  63. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  64. camera.position.set( 0.0, 300, 400 * 3 );
  65. scene = new THREE.Scene();
  66. scene.background = new THREE.Color( 0x333333 );
  67. // Lights
  68. const topLight = new THREE.PointLight( 0xF4F6F0, 1 );
  69. topLight.position.set( 0, 100000, 100000 );
  70. scene.add( topLight );
  71. const backLight = new THREE.PointLight( 0x0c1445, 1.4 );
  72. backLight.position.set( - 100, 20, - 260 );
  73. scene.add( backLight );
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. document.body.appendChild( renderer.domElement );
  76. renderer.outputEncoding = THREE.sRGBEncoding;
  77. renderer.domElement.className = 'renderer';
  78. //
  79. stats = new Stats();
  80. document.body.appendChild( stats.dom );
  81. const controls = new OrbitControls( camera, renderer.domElement );
  82. controls.minDistance = 500;
  83. controls.maxDistance = 3000;
  84. window.addEventListener( 'resize', onWindowResize );
  85. onWindowResize();
  86. initEditor();
  87. }
  88. function initEditor() {
  89. const nodeEditor = new NodeEditor( scene );
  90. nodeEditor.addEventListener( 'new', () => {
  91. const materialEditor = new MeshEditor( model );
  92. nodeEditor.add( materialEditor );
  93. nodeEditor.centralizeNode( materialEditor );
  94. } );
  95. document.body.appendChild( nodeEditor.domElement );
  96. const loaderFBX = new FBXLoader();
  97. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  98. const defaultMaterial = new Nodes.MeshBasicNodeMaterial();
  99. defaultMaterial.colorNode = new Nodes.FloatNode( 0 );
  100. const sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 32, 16 ), defaultMaterial );
  101. sphere.name = 'Sphere';
  102. sphere.position.set( 500, 0, - 500 );
  103. scene.add( sphere );
  104. const box = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), defaultMaterial );
  105. box.name = 'Box';
  106. box.position.set( - 500, 0, - 500 );
  107. scene.add( box );
  108. const defaultPointsMaterial = new Nodes.PointsNodeMaterial();
  109. defaultPointsMaterial.colorNode = new Nodes.FloatNode( 0 );
  110. const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 100, 30, 100, 16 ), defaultPointsMaterial );
  111. torusKnot.name = 'Torus Knot ( Points )';
  112. torusKnot.position.set( 0, 0, - 500 );
  113. scene.add( torusKnot );
  114. model = object.children[ 0 ];
  115. model.position.set( 0, 0, 10 );
  116. model.scale.setScalar( 1 );
  117. model.material = defaultMaterial;
  118. scene.add( model );
  119. const materialEditor = new MeshEditor( model );
  120. nodeEditor.add( materialEditor );
  121. nodeEditor.centralizeNode( materialEditor );
  122. } );
  123. }
  124. function onWindowResize() {
  125. const width = window.innerWidth;
  126. const height = window.innerHeight / 2;
  127. camera.aspect = width / height;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( width, height );
  130. }
  131. //
  132. function animate() {
  133. requestAnimationFrame( animate );
  134. nodeFrame.update();
  135. render();
  136. stats.update();
  137. }
  138. function render() {
  139. //if ( model ) model.rotation.y = performance.now() / 5000;
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>