webgl_materials_nodes_playground.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  36. <script type="importmap">
  37. {
  38. "imports": {
  39. "three": "../build/three.module.js"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';
  46. import { NodeEditor } from './jsm/node-editor/NodeEditor.js';
  47. import { StandardMaterialEditor } from './jsm/node-editor/materials/StandardMaterialEditor.js';
  48. import Stats from './jsm/libs/stats.module.js';
  49. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  50. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  51. let stats;
  52. let camera, scene, renderer;
  53. let model;
  54. init();
  55. animate();
  56. function init() {
  57. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  58. camera.position.set( 0.0, 300, 400 * 3 );
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0x333333 );
  61. // Lights
  62. const topLight = new THREE.PointLight( 0xF4F6F0, 1 );
  63. topLight.position.set( 0, 100000, 100000 );
  64. scene.add( topLight );
  65. const backLight = new THREE.PointLight( 0x0c1445, 1.4 );
  66. backLight.position.set( - 100, 20, - 260 );
  67. scene.add( backLight );
  68. renderer = new THREE.WebGLRenderer( { antialias: true } );
  69. document.body.appendChild( renderer.domElement );
  70. renderer.outputEncoding = THREE.sRGBEncoding;
  71. renderer.domElement.className = 'renderer';
  72. //
  73. stats = new Stats();
  74. document.body.appendChild( stats.dom );
  75. const controls = new OrbitControls( camera, renderer.domElement );
  76. controls.minDistance = 500;
  77. controls.maxDistance = 3000;
  78. window.addEventListener( 'resize', onWindowResize );
  79. onWindowResize();
  80. initEditor();
  81. }
  82. function initEditor() {
  83. const nodeEditor = new NodeEditor();
  84. nodeEditor.addEventListener( 'new', () => {
  85. const materialEditor = new StandardMaterialEditor();
  86. materialEditor.setPosition( ( window.innerWidth / 2 ) - 150, 100 );
  87. nodeEditor.add( materialEditor );
  88. model.material = materialEditor.material;
  89. } );
  90. nodeEditor.addEventListener( 'load', () => {
  91. const materialEditor = nodeEditor.nodes[ 0 ];
  92. materialEditor.update(); // need move to deserialization
  93. model.material = materialEditor.material;
  94. } );
  95. document.body.appendChild( nodeEditor.domElement );
  96. const loaderFBX = new FBXLoader();
  97. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {
  98. const materialEditor = new StandardMaterialEditor();
  99. materialEditor.setPosition( ( window.innerWidth / 2 ) - 150, 100 ); // canvas position
  100. nodeEditor.add( materialEditor );
  101. model = object.children[ 0 ];
  102. model.position.set( 0, 0, 10 );
  103. model.scale.setScalar( 1 );
  104. model.material = materialEditor.material;
  105. scene.add( model );
  106. } );
  107. }
  108. function onWindowResize() {
  109. const width = window.innerWidth;
  110. const height = window.innerHeight / 2;
  111. camera.aspect = width / height;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( width, height );
  114. }
  115. //
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. nodeFrame.update();
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. //if ( model ) model.rotation.y = performance.now() / 5000;
  124. renderer.render( scene, camera );
  125. }
  126. </script>
  127. </body>
  128. </html>