webgl_nodes_playground.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - node 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: 100%;
  21. width: 100%;
  22. }
  23. flow {
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. height: 100%;
  28. width: 100%;
  29. box-shadow: inset 0 0 20px 0px #000000;
  30. pointer-events: none;
  31. }
  32. flow f-canvas {
  33. pointer-events: auto;
  34. }
  35. flow f-canvas:not(.focusing) {
  36. background: #191919ed;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  42. <script type="importmap">
  43. {
  44. "imports": {
  45. "three": "../build/three.module.js",
  46. "three/addons/": "./jsm/",
  47. "three/nodes": "./jsm/nodes/Nodes.js"
  48. }
  49. }
  50. </script>
  51. <script type="module">
  52. import * as THREE from 'three';
  53. import * as Nodes from 'three/nodes';
  54. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  55. import { NodeEditor } from 'three/addons/node-editor/NodeEditor.js';
  56. import { MeshEditor } from 'three/addons/node-editor/scene/MeshEditor.js';
  57. import { StandardMaterialEditor } from 'three/addons/node-editor/materials/StandardMaterialEditor.js';
  58. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  59. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  60. let camera, scene, renderer;
  61. let model;
  62. let nodeEditor;
  63. init();
  64. animate();
  65. function init() {
  66. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  67. camera.position.set( 0.0, 300, 400 * 3 );
  68. scene = new THREE.Scene();
  69. scene.background = new THREE.Color( 0x333333 );
  70. // Lights
  71. const topLight = new THREE.PointLight( 0xF4F6F0, 1 );
  72. topLight.position.set( 0, 1000, 1000 );
  73. scene.add( topLight );
  74. const backLight = new THREE.PointLight( 0x0c1445, 1 );
  75. backLight.position.set( - 100, 20, - 260 );
  76. scene.add( backLight );
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. document.body.appendChild( renderer.domElement );
  79. renderer.outputEncoding = THREE.sRGBEncoding;
  80. renderer.toneMapping = THREE.LinearToneMapping;
  81. renderer.toneMappingExposure = 4000;
  82. renderer.physicallyCorrectLights = true;
  83. renderer.domElement.className = 'renderer';
  84. //
  85. const controls = new OrbitControls( camera, renderer.domElement );
  86. controls.minDistance = 500;
  87. controls.maxDistance = 3000;
  88. window.addEventListener( 'resize', onWindowResize );
  89. initEditor();
  90. onWindowResize();
  91. }
  92. function initEditor() {
  93. nodeEditor = new NodeEditor( scene );
  94. const reset = () => {
  95. const meshEditor = new MeshEditor( model );
  96. const materialEditor = new StandardMaterialEditor();
  97. nodeEditor.add( meshEditor );
  98. nodeEditor.add( materialEditor );
  99. nodeEditor.centralizeNode( meshEditor );
  100. const { x, y } = meshEditor.getPosition();
  101. meshEditor.setPosition( x + 250, y );
  102. materialEditor.setPosition( x - 250, y );
  103. meshEditor.material.connect( materialEditor );
  104. };
  105. nodeEditor.addEventListener( 'new', reset );
  106. document.body.appendChild( nodeEditor.domElement );
  107. const loaderFBX = new FBXLoader();
  108. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  109. const defaultMaterial = new Nodes.MeshBasicNodeMaterial();
  110. defaultMaterial.colorNode = new Nodes.UniformNode( 0 );
  111. const sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 32, 16 ), defaultMaterial );
  112. sphere.name = 'Sphere';
  113. sphere.position.set( 500, 0, - 500 );
  114. scene.add( sphere );
  115. const box = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), defaultMaterial );
  116. box.name = 'Box';
  117. box.position.set( - 500, 0, - 500 );
  118. scene.add( box );
  119. const defaultPointsMaterial = new Nodes.PointsNodeMaterial();
  120. defaultPointsMaterial.colorNode = new Nodes.UniformNode( 0 );
  121. const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 100, 30, 100, 16 ), defaultPointsMaterial );
  122. torusKnot.name = 'Torus Knot ( Points )';
  123. torusKnot.position.set( 0, 0, - 500 );
  124. scene.add( torusKnot );
  125. model = object.children[ 0 ];
  126. model.position.set( 0, 0, 10 );
  127. model.scale.setScalar( 1 );
  128. model.material = defaultMaterial;
  129. scene.add( model );
  130. reset();
  131. } );
  132. }
  133. function onWindowResize() {
  134. const width = window.innerWidth;
  135. const height = window.innerHeight;
  136. camera.aspect = width / height;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( width, height );
  139. nodeEditor.setSize( width, height );
  140. }
  141. //
  142. function animate() {
  143. requestAnimationFrame( animate );
  144. nodeFrame.update();
  145. render();
  146. }
  147. function render() {
  148. //if ( model ) model.rotation.y = performance.now() / 5000;
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>