SetTextureParametersCommand.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { Command } from '../Command.js';
  2. const VECTOR_KEYS = [ 'offset', 'repeat', 'center' ];
  3. class SetTextureParametersCommand extends Command {
  4. /**
  5. * @param {Editor} editor
  6. * @param {THREE.Texture} texture
  7. * @param {Object} newParameters
  8. * @constructor
  9. */
  10. constructor( editor, texture = null, newParameters = {} ) {
  11. super( editor );
  12. this.type = 'SetTextureParametersCommand';
  13. this.name = editor.strings.getKey( 'command/SetTextureParameters' );
  14. this.texture = texture;
  15. this.oldParameters = ( texture !== null ) ? extractParameters( texture, newParameters ) : {};
  16. this.newParameters = newParameters;
  17. }
  18. execute() {
  19. applyParameters( this.texture, this.newParameters );
  20. this.editor.signals.sceneGraphChanged.dispatch();
  21. }
  22. undo() {
  23. applyParameters( this.texture, this.oldParameters );
  24. this.editor.signals.sceneGraphChanged.dispatch();
  25. }
  26. toJSON() {
  27. const output = super.toJSON( this );
  28. output.textureUuid = this.texture.uuid;
  29. output.oldParameters = this.oldParameters;
  30. output.newParameters = this.newParameters;
  31. return output;
  32. }
  33. fromJSON( json ) {
  34. super.fromJSON( json );
  35. this.texture = findTextureByUuid( this.editor, json.textureUuid );
  36. this.oldParameters = json.oldParameters;
  37. this.newParameters = json.newParameters;
  38. }
  39. }
  40. function extractParameters( texture, reference ) {
  41. const result = {};
  42. for ( const key in reference ) {
  43. const value = texture[ key ];
  44. if ( VECTOR_KEYS.includes( key ) ) {
  45. result[ key ] = { x: value.x, y: value.y };
  46. } else {
  47. result[ key ] = value;
  48. }
  49. }
  50. return result;
  51. }
  52. function applyParameters( texture, parameters ) {
  53. for ( const key in parameters ) {
  54. const value = parameters[ key ];
  55. if ( VECTOR_KEYS.includes( key ) ) {
  56. texture[ key ].set( value.x, value.y );
  57. } else {
  58. texture[ key ] = value;
  59. }
  60. }
  61. texture.needsUpdate = true;
  62. }
  63. function findTextureByUuid( editor, uuid ) {
  64. let result = null;
  65. editor.scene.traverse( ( object ) => {
  66. if ( object.material === undefined ) return;
  67. const materials = Array.isArray( object.material ) ? object.material : [ object.material ];
  68. for ( const material of materials ) {
  69. for ( const key in material ) {
  70. const value = material[ key ];
  71. if ( value && value.isTexture === true && value.uuid === uuid ) {
  72. result = value;
  73. }
  74. }
  75. }
  76. } );
  77. return result;
  78. }
  79. export { SetTextureParametersCommand };
粤ICP备19079148号