MeshEditor.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { LabelElement } from '../../libs/flow.module.js';
  2. import { Object3DEditor } from './Object3DEditor.js';
  3. import { Mesh } from 'three';
  4. export class MeshEditor extends Object3DEditor {
  5. constructor( mesh = null ) {
  6. if ( mesh === null ) {
  7. mesh = new Mesh();
  8. }
  9. super( mesh, 'Mesh' );
  10. this.material = null;
  11. this.defaultMaterial = null;
  12. this._initMaterial();
  13. this.updateDefault();
  14. this.restoreDefault();
  15. this.update();
  16. }
  17. get mesh() {
  18. return this.value;
  19. }
  20. _initMaterial() {
  21. const materialElement = new LabelElement( 'Material' ).setInputColor( 'forestgreen' ).setInput( 1 );
  22. materialElement.onValid( ( source, target, stage ) => {
  23. const object = target.getObject();
  24. if ( object && object.isMaterial !== true ) {
  25. if ( stage === 'dragged' ) {
  26. const name = target.node.getName();
  27. this.editor.tips.error( `"${name}" is not a Material.` );
  28. }
  29. return false;
  30. }
  31. } ).onConnect( () => {
  32. this.material = materialElement.getLinkedObject() || this.defaultMaterial;
  33. this.update();
  34. } );
  35. this.add( materialElement );
  36. }
  37. update() {
  38. super.update();
  39. const mesh = this.mesh;
  40. if ( mesh ) {
  41. mesh.material = this.material || this.defaultMaterial;
  42. }
  43. }
  44. updateDefault() {
  45. super.updateDefault();
  46. this.defaultMaterial = this.mesh.material;
  47. }
  48. restoreDefault() {
  49. super.restoreDefault();
  50. this.mesh.material = this.defaultMaterial;
  51. }
  52. }