webgl_materials_nodes_playground.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. </head>
  11. <body>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js"
  16. }
  17. }
  18. </script>
  19. <style>
  20. body {
  21. overflow: hidden;
  22. width: 100%;
  23. height: 100%;
  24. }
  25. .renderer {
  26. position: absolute;
  27. top: 0;
  28. left: 0;
  29. height: 50%;
  30. width: 100%;
  31. }
  32. flow {
  33. position: absolute;
  34. top: 50%;
  35. left: 0;
  36. height: 50%;
  37. width: 100%;
  38. background: #222;
  39. box-shadow: inset 0 0 20px 0px #000000;
  40. }
  41. </style>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';
  45. import { NodeEditor } from './jsm/node-editor/NodeEditor.js';
  46. import { StandardMaterialEditor } from './jsm/node-editor/materials/StandardMaterialEditor.js';
  47. import Stats from './jsm/libs/stats.module.js';
  48. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  49. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  50. let stats;
  51. let camera, scene, renderer;
  52. let model;
  53. init();
  54. animate();
  55. function init() {
  56. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  57. camera.position.set( 0.0, 300, 400 * 3 );
  58. scene = new THREE.Scene();
  59. scene.background = new THREE.Color( 0x333333 );
  60. // Lights
  61. //scene.add( new THREE.AmbientLight( 0x111111 ) );
  62. const directionalLight = new THREE.DirectionalLight( 0xF4F6F0, 0.8 );
  63. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  64. scene.add( directionalLight );
  65. const pointLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x0c1445 } ) );
  66. pointLight.add( new THREE.PointLight( 0x0c1445, 0.7, 500 ) );
  67. scene.add( pointLight );
  68. pointLight.position.x = - 100;
  69. pointLight.position.y = 20;
  70. pointLight.position.z = - 260;
  71. renderer = new THREE.WebGLRenderer( { antialias: true } );
  72. document.body.appendChild( renderer.domElement );
  73. renderer.outputEncoding = THREE.sRGBEncoding;
  74. renderer.domElement.className = 'renderer';
  75. //
  76. stats = new Stats();
  77. document.body.appendChild( stats.dom );
  78. const controls = new OrbitControls( camera, renderer.domElement );
  79. controls.minDistance = 500;
  80. controls.maxDistance = 3000;
  81. window.addEventListener( 'resize', onWindowResize );
  82. onWindowResize();
  83. initEditor();
  84. }
  85. function initEditor() {
  86. const nodeEditor = new NodeEditor();
  87. nodeEditor.addEventListener( 'new', () => {
  88. const materialEditor = new StandardMaterialEditor();
  89. materialEditor.setPosition( ( window.innerWidth / 2 ) - 150, 100 );
  90. nodeEditor.add( materialEditor );
  91. model.material = materialEditor.material;
  92. } );
  93. nodeEditor.addEventListener( 'load', () => {
  94. const materialEditor = nodeEditor.nodes[ 0 ];
  95. materialEditor.update(); // need move to deserialization
  96. model.material = materialEditor.material;
  97. } );
  98. document.body.appendChild( nodeEditor.domElement );
  99. const loaderFBX = new FBXLoader();
  100. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  101. const materialEditor = new StandardMaterialEditor();
  102. materialEditor.setPosition( ( window.innerWidth / 2 ) - 150, 100 ); // canvas position
  103. nodeEditor.add( materialEditor );
  104. model = object.children[ 0 ];
  105. model.position.set( 0, 0, 10 );
  106. model.scale.setScalar( 1 );
  107. model.material = materialEditor.material;
  108. scene.add( model );
  109. } );
  110. }
  111. function onWindowResize() {
  112. const width = window.innerWidth;
  113. const height = window.innerHeight / 2;
  114. camera.aspect = width / height;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( width, height );
  117. }
  118. //
  119. function animate() {
  120. requestAnimationFrame( animate );
  121. nodeFrame.update();
  122. render();
  123. stats.update();
  124. }
  125. function render() {
  126. //if ( model ) model.rotation.y = performance.now() / 5000;
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>