webgl_nodes_playground.html 4.8 KB

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