PreviewEditor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { OrbitControls } from '../../controls/OrbitControls.js';
  2. import { ViewHelper } from '../../helpers/ViewHelper.js';
  3. import { Element, LabelElement, SelectInput } from '../../libs/flow.module.js';
  4. import { BaseNode } from '../core/BaseNode.js';
  5. import { MeshBasicNodeMaterial, ConstNode } from 'three/nodes';
  6. import { WebGLRenderer, PerspectiveCamera, Scene, Mesh, DoubleSide, SphereGeometry, BoxGeometry, PlaneGeometry, TorusKnotGeometry } from 'three';
  7. const nullValue = new ConstNode( 0 );
  8. const sceneDict = {};
  9. const getScene = ( name ) => {
  10. let scene = sceneDict[ name ];
  11. if ( scene === undefined ) {
  12. scene = new Scene();
  13. if ( name === 'box' ) {
  14. const box = new Mesh( new BoxGeometry( 1.3, 1.3, 1.3 ) );
  15. scene.add( box );
  16. } else if ( name === 'sphere' ) {
  17. const sphere = new Mesh( new SphereGeometry( 1, 32, 16 ) );
  18. scene.add( sphere );
  19. } else if ( name === 'plane' || name === 'sprite' ) {
  20. const plane = new Mesh( new PlaneGeometry( 2, 2 ) );
  21. scene.add( plane );
  22. } else if ( name === 'torus' ) {
  23. const torus = new Mesh( new TorusKnotGeometry( .7, .1, 100, 16 ) );
  24. scene.add( torus );
  25. }
  26. sceneDict[ name ] = scene;
  27. }
  28. return scene;
  29. };
  30. export class PreviewEditor extends BaseNode {
  31. constructor() {
  32. const width = 300;
  33. const height = 300;
  34. super( 'Preview', 0, null, height );
  35. const material = new MeshBasicNodeMaterial();
  36. material.colorNode = nullValue;
  37. material.side = DoubleSide;
  38. material.transparent = true;
  39. const previewElement = new Element();
  40. previewElement.dom.style[ 'padding-top' ] = 0;
  41. previewElement.dom.style[ 'padding-bottom' ] = 0;
  42. const sceneInput = new SelectInput( [
  43. { name: 'Box', value: 'box' },
  44. { name: 'Sphere', value: 'sphere' },
  45. { name: 'Plane', value: 'plane' },
  46. { name: 'Sprite', value: 'sprite' },
  47. { name: 'Torus', value: 'torus' }
  48. ], 'box' );
  49. const inputElement = new LabelElement( 'Input' ).setInput( 4 ).onConnect( () => {
  50. material.colorNode = inputElement.getLinkedObject() || nullValue;
  51. material.dispose();
  52. }, true );
  53. const canvas = document.createElement( 'canvas' );
  54. canvas.style.position = 'absolute';
  55. previewElement.dom.append( canvas );
  56. previewElement.setHeight( height );
  57. const renderer = new WebGLRenderer( {
  58. canvas,
  59. alpha: true
  60. } );
  61. renderer.autoClear = false;
  62. renderer.setSize( width, height, true );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. const camera = new PerspectiveCamera( 45, width / height, 0.1, 100 );
  65. camera.aspect = width / height;
  66. camera.updateProjectionMatrix();
  67. camera.position.set( - 2, 2, 2 );
  68. camera.lookAt( 0, 0, 0 );
  69. const controls = new OrbitControls( camera, previewElement.dom );
  70. controls.enableKeys = false;
  71. controls.update();
  72. const viewHelper = new ViewHelper( camera, previewElement.dom );
  73. this.sceneInput = sceneInput;
  74. this.viewHelper = viewHelper;
  75. this.material = material;
  76. this.camera = camera;
  77. this.renderer = renderer;
  78. this.add( inputElement )
  79. .add( new LabelElement( 'Object' ).add( sceneInput ) )
  80. .add( previewElement );
  81. }
  82. setEditor( editor ) {
  83. super.setEditor( editor );
  84. this.updateAnimationRequest();
  85. }
  86. updateAnimationRequest() {
  87. if ( this.editor !== null ) {
  88. requestAnimationFrame( () => this.update() );
  89. }
  90. }
  91. update() {
  92. const { viewHelper, material, renderer, camera, sceneInput } = this;
  93. this.updateAnimationRequest();
  94. const sceneName = sceneInput.getValue();
  95. const scene = getScene( sceneName );
  96. const mesh = scene.children[ 0 ];
  97. mesh.material = material;
  98. if ( sceneName === 'sprite' ) {
  99. mesh.lookAt( camera.position );
  100. }
  101. renderer.clear();
  102. renderer.render( scene, camera );
  103. viewHelper.render( renderer );
  104. }
  105. }