CmdSetMaterial.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. /**
  6. * @param object THREE.Object3D
  7. * @param newMaterial THREE.Material
  8. * @constructor
  9. */
  10. CmdSetMaterial = function ( object, newMaterial ) {
  11. Cmd.call( this );
  12. this.type = 'CmdSetMaterial';
  13. this.name = 'New Material';
  14. this.object = object;
  15. this.oldMaterial = ( object !== undefined ) ? object.material : undefined;
  16. this.newMaterial = newMaterial;
  17. };
  18. CmdSetMaterial.prototype = {
  19. execute: function () {
  20. this.object.material = this.newMaterial;
  21. this.editor.signals.materialChanged.dispatch( this.newMaterial );
  22. },
  23. undo: function () {
  24. this.object.material = this.oldMaterial;
  25. this.editor.signals.materialChanged.dispatch( this.oldMaterial );
  26. },
  27. toJSON: function () {
  28. var output = Cmd.prototype.toJSON.call( this );
  29. output.objectUuid = this.object.uuid;
  30. output.oldMaterial = serializeMaterial( this.oldMaterial );
  31. output.newMaterial = serializeMaterial( this.newMaterial );
  32. return output;
  33. function serializeMaterial ( material ) {
  34. if ( material === undefined ) return null;
  35. var meta = {
  36. geometries: {},
  37. materials: {},
  38. textures: {},
  39. images: {}
  40. };
  41. var json = {};
  42. json.materials = [ material.toJSON( meta ) ];
  43. var textures = extractFromCache( meta.textures );
  44. var images = extractFromCache( meta.images );
  45. if ( textures.length > 0 ) json.textures = textures;
  46. if ( images.length > 0 ) json.images = images;
  47. return json;
  48. }
  49. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  50. // extract data from the cache hash
  51. // remove metadata on each item
  52. // and return as array
  53. function extractFromCache ( cache ) {
  54. var values = [];
  55. for ( var key in cache ) {
  56. var data = cache[ key ];
  57. delete data.metadata;
  58. values.push( data );
  59. }
  60. return values;
  61. }
  62. },
  63. fromJSON: function ( json ) {
  64. Cmd.prototype.fromJSON.call( this, json );
  65. this.object = this.editor.objectByUuid( json.objectUuid );
  66. this.oldMaterial = parseMaterial( json.oldMaterial );
  67. this.newMaterial = parseMaterial( json.newMaterial );
  68. function parseMaterial ( json ) {
  69. var loader = new THREE.ObjectLoader();
  70. var images = loader.parseImages( json.images );
  71. var textures = loader.parseTextures( json.textures, images );
  72. var materials = loader.parseMaterials( json.materials, textures );
  73. return materials[ json.materials[ 0 ].uuid ];
  74. }
  75. }
  76. };