CmdSetMaterial.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Created by Daniel on 21.07.15.
  3. */
  4. CmdSetMaterial = function ( object, newMaterial ) {
  5. Cmd.call( this );
  6. this.type = 'CmdSetMaterial';
  7. this.object = object;
  8. this.objectUuid = object !== undefined ? object.uuid : undefined;
  9. this.oldMaterial = object !== undefined ? object.material : undefined;
  10. this.newMaterial = newMaterial;
  11. meta = {
  12. geometries: {},
  13. materials: {},
  14. textures: {},
  15. images: {}
  16. };
  17. this.oldMaterialJSON = object !== undefined ? object.material.toJSON( meta ) : undefined;
  18. this.newMaterialJSON = newMaterial !== undefined ? newMaterial.toJSON( meta ) : undefined;
  19. };
  20. CmdSetMaterial.prototype = {
  21. execute: function () {
  22. this.object.material = this.newMaterial;
  23. this.editor.signals.materialChanged.dispatch( this.newMaterial );
  24. },
  25. undo: function () {
  26. this.object.material = this.oldMaterial;
  27. this.editor.signals.materialChanged.dispatch( this.oldMaterial );
  28. },
  29. toJSON: function () {
  30. var output = Cmd.prototype.toJSON.call( this );
  31. output.objectUuid = this.objectUuid;
  32. output.oldMaterial = this.oldMaterialJSON;
  33. output.newMaterial = this.newMaterialJSON;
  34. return output;
  35. },
  36. fromJSON: function ( json ) {
  37. Cmd.prototype.fromJSON.call( this, json );
  38. this.object = this.editor.objectByUuid( json.objectUuid );
  39. this.objectUuid = json.objectUuid;
  40. this.oldMaterial = this.parseMaterial( json.oldMaterial );
  41. this.newMaterial = this.parseMaterial( json.newMaterial );
  42. this.oldMaterialJSON = json.oldMaterial;
  43. this.newMaterialJSON = json.newMaterial;
  44. },
  45. parseMaterial: function ( data ) {
  46. var loader = new THREE.ObjectLoader();
  47. return loader.parseMaterials( [ data ] )[ data.uuid ];
  48. }
  49. };