webgl_materials_nodes_playground.html 3.9 KB

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