webgl_nodes_playground.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/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 { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  52. import { NodeEditor } from 'three/addons/node-editor/NodeEditor.js';
  53. import { MeshEditor } from 'three/addons/node-editor/scene/MeshEditor.js';
  54. import Stats from 'three/addons/libs/stats.module.js';
  55. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  56. import { FBXLoader } from 'three/addons/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, 1000, 1000 );
  70. scene.add( topLight );
  71. const backLight = new THREE.PointLight( 0x0c1445, 1 );
  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.toneMapping = THREE.LinearToneMapping;
  78. renderer.toneMappingExposure = 4000;
  79. renderer.physicallyCorrectLights = true;
  80. renderer.domElement.className = 'renderer';
  81. //
  82. stats = new Stats();
  83. document.body.appendChild( stats.dom );
  84. const controls = new OrbitControls( camera, renderer.domElement );
  85. controls.minDistance = 500;
  86. controls.maxDistance = 3000;
  87. window.addEventListener( 'resize', onWindowResize );
  88. onWindowResize();
  89. initEditor();
  90. }
  91. function initEditor() {
  92. const nodeEditor = new NodeEditor( scene );
  93. nodeEditor.addEventListener( 'new', () => {
  94. const materialEditor = new MeshEditor( model );
  95. nodeEditor.add( materialEditor );
  96. nodeEditor.centralizeNode( materialEditor );
  97. } );
  98. document.body.appendChild( nodeEditor.domElement );
  99. const loaderFBX = new FBXLoader();
  100. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  101. const defaultMaterial = new Nodes.MeshBasicNodeMaterial();
  102. defaultMaterial.colorNode = new Nodes.UniformNode( 0 );
  103. const sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 32, 16 ), defaultMaterial );
  104. sphere.name = 'Sphere';
  105. sphere.position.set( 500, 0, - 500 );
  106. scene.add( sphere );
  107. const box = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), defaultMaterial );
  108. box.name = 'Box';
  109. box.position.set( - 500, 0, - 500 );
  110. scene.add( box );
  111. const defaultPointsMaterial = new Nodes.PointsNodeMaterial();
  112. defaultPointsMaterial.colorNode = new Nodes.UniformNode( 0 );
  113. const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 100, 30, 100, 16 ), defaultPointsMaterial );
  114. torusKnot.name = 'Torus Knot ( Points )';
  115. torusKnot.position.set( 0, 0, - 500 );
  116. scene.add( torusKnot );
  117. model = object.children[ 0 ];
  118. model.position.set( 0, 0, 10 );
  119. model.scale.setScalar( 1 );
  120. model.material = defaultMaterial;
  121. scene.add( model );
  122. const materialEditor = new MeshEditor( model );
  123. nodeEditor.add( materialEditor );
  124. nodeEditor.centralizeNode( materialEditor );
  125. } );
  126. }
  127. function onWindowResize() {
  128. const width = window.innerWidth;
  129. const height = window.innerHeight / 2;
  130. camera.aspect = width / height;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( width, height );
  133. }
  134. //
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. nodeFrame.update();
  138. render();
  139. stats.update();
  140. }
  141. function render() {
  142. //if ( model ) model.rotation.y = performance.now() / 5000;
  143. renderer.render( scene, camera );
  144. }
  145. </script>
  146. </body>
  147. </html>