webgl_nodes_playground.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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, 0.01, 100 );
  67. camera.position.set( 0.0, 3, 4 * 3 );
  68. scene = new THREE.Scene();
  69. scene.background = new THREE.Color( 0x333333 );
  70. // Lights
  71. const topLight = new THREE.PointLight( 0xF4F6F0, 1, 100 );
  72. topLight.power = 4500;
  73. topLight.position.set( 0, 10, 10 );
  74. scene.add( topLight );
  75. const backLight = new THREE.PointLight( 0x0c1445, 1, 100 );
  76. backLight.power = 1000;
  77. backLight.position.set( - 1, .2, - 2.6 );
  78. scene.add( backLight );
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. document.body.appendChild( renderer.domElement );
  81. renderer.outputEncoding = THREE.sRGBEncoding;
  82. renderer.toneMapping = THREE.LinearToneMapping;
  83. renderer.toneMappingExposure = 1;
  84. renderer.physicallyCorrectLights = true;
  85. renderer.domElement.className = 'renderer';
  86. //
  87. const controls = new OrbitControls( camera, renderer.domElement );
  88. controls.minDistance = 5;
  89. controls.maxDistance = 30;
  90. window.addEventListener( 'resize', onWindowResize );
  91. initEditor();
  92. onWindowResize();
  93. }
  94. function initEditor() {
  95. nodeEditor = new NodeEditor( scene );
  96. const reset = () => {
  97. const meshEditor = new MeshEditor( model );
  98. const materialEditor = new StandardMaterialEditor();
  99. nodeEditor.add( meshEditor );
  100. nodeEditor.add( materialEditor );
  101. nodeEditor.centralizeNode( meshEditor );
  102. const { x, y } = meshEditor.getPosition();
  103. meshEditor.setPosition( x + 250, y );
  104. materialEditor.setPosition( x - 250, y );
  105. meshEditor.material.connect( materialEditor );
  106. };
  107. nodeEditor.addEventListener( 'new', reset );
  108. document.body.appendChild( nodeEditor.domElement );
  109. const loaderFBX = new FBXLoader();
  110. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  111. const defaultMaterial = new Nodes.MeshBasicNodeMaterial();
  112. defaultMaterial.colorNode = new Nodes.UniformNode( 0 );
  113. const sphere = new THREE.Mesh( new THREE.SphereGeometry( 2, 32, 16 ), defaultMaterial );
  114. sphere.name = 'Sphere';
  115. sphere.position.set( 5, 0, - 5 );
  116. scene.add( sphere );
  117. const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), defaultMaterial );
  118. box.name = 'Box';
  119. box.position.set( - 5, 0, - 5 );
  120. scene.add( box );
  121. const defaultPointsMaterial = new Nodes.PointsNodeMaterial();
  122. defaultPointsMaterial.colorNode = new Nodes.UniformNode( 0 );
  123. defaultPointsMaterial.size = 0.01;
  124. const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 1, .3, 100, 16 ), defaultPointsMaterial );
  125. torusKnot.name = 'Torus Knot ( Points )';
  126. torusKnot.position.set( 0, 0, - 5 );
  127. scene.add( torusKnot );
  128. model = object.children[ 0 ];
  129. model.position.set( 0, 0, .1 );
  130. model.scale.setScalar( .01 );
  131. model.material = defaultMaterial;
  132. scene.add( model );
  133. reset();
  134. } );
  135. }
  136. function onWindowResize() {
  137. const width = window.innerWidth;
  138. const height = window.innerHeight;
  139. camera.aspect = width / height;
  140. camera.updateProjectionMatrix();
  141. renderer.setSize( width, height );
  142. nodeEditor.setSize( width, height );
  143. }
  144. //
  145. function animate() {
  146. requestAnimationFrame( animate );
  147. nodeFrame.update();
  148. render();
  149. }
  150. function render() {
  151. //if ( model ) model.rotation.y = performance.now() / 5000;
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>