Quellcode durchsuchen

Texture serialization for particle editor

David Catuhe vor 5 Jahren
Ursprung
Commit
610d5bc1ec

+ 23 - 21
inspector/src/components/actionTabs/tabs/propertyGrids/materials/texturePropertyGridComponent.tsx

@@ -68,20 +68,25 @@ export class TexturePropertyGridComponent extends React.Component<ITextureProper
         const texture = this.props.texture;
         const texture = this.props.texture;
         Tools.ReadFile(file, (data) => {
         Tools.ReadFile(file, (data) => {
             var blob = new Blob([data], { type: "octet/stream" });
             var blob = new Blob([data], { type: "octet/stream" });
-            var url = URL.createObjectURL(blob);
-
-            if (texture.isCube) {
-                let extension: string | undefined = undefined;
-                if (file.name.toLowerCase().indexOf(".dds") > 0) {
-                    extension = ".dds";
-                } else if (file.name.toLowerCase().indexOf(".env") > 0) {
-                    extension = ".env";
-                }
 
 
-                (texture as CubeTexture).updateURL(url, extension, () => this.foreceRefresh());
-            } else {
-                (texture as Texture).updateURL(url, null, () => this.foreceRefresh());
-            }
+            var reader = new FileReader();
+            reader.readAsDataURL(blob); 
+            reader.onloadend = () => {
+                let base64data = reader.result as string;     
+
+                if (texture.isCube) {
+                    let extension: string | undefined = undefined;
+                    if (file.name.toLowerCase().indexOf(".dds") > 0) {
+                        extension = ".dds";
+                    } else if (file.name.toLowerCase().indexOf(".env") > 0) {
+                        extension = ".env";
+                    }
+
+                    (texture as CubeTexture).updateURL(base64data, extension, () => this.foreceRefresh());
+                } else {
+                    (texture as Texture).updateURL(base64data, null, () => this.foreceRefresh());
+                }
+            };
 
 
         }, undefined, true);
         }, undefined, true);
     }    
     }    
@@ -115,20 +120,17 @@ export class TexturePropertyGridComponent extends React.Component<ITextureProper
 
 
         let extension = "";
         let extension = "";
         let url = (texture as Texture).url;
         let url = (texture as Texture).url;
+        let textureUrl = (!url || url.substring(0, 4) === "data" || url.substring(0, 4) === "blob") ? "" : url;
 
 
-        if (url) {
-            for (var index = url.length - 1; index >= 0; index--) {
-                if (url[index] === ".") {
+        if (textureUrl) {
+            for (var index = textureUrl.length - 1; index >= 0; index--) {
+                if (textureUrl[index] === ".") {
                     break;
                     break;
                 }
                 }
-                extension = url[index] + extension;
+                extension = textureUrl[index] + extension;
             }
             }
-        } else {
-            url = "";
         }
         }
 
 
-        let textureUrl = (url.substring(0, 4) === "data" || url.substring(0, 4) === "blob") ? "" : url;
-
         return (
         return (
             <div className="pane">
             <div className="pane">
                 <LineContainerComponent globalState={this.props.globalState} title="PREVIEW">
                 <LineContainerComponent globalState={this.props.globalState} title="PREVIEW">

+ 3 - 3
inspector/src/components/actionTabs/tabs/propertyGrids/particleSystems/particleSystemPropertyGridComponent.tsx

@@ -157,7 +157,7 @@ export class ParticleSystemPropertyGridComponent extends React.Component<IPartic
 
 
     saveToFile() {        
     saveToFile() {        
         const system = this.props.system;
         const system = this.props.system;
-        let content = JSON.stringify(system.serialize());
+        let content = JSON.stringify(system.serialize(true));
 
 
         Tools.Download(new Blob([content]), "particleSystem.json");
         Tools.Download(new Blob([content]), "particleSystem.json");
     }
     }
@@ -202,7 +202,7 @@ export class ParticleSystemPropertyGridComponent extends React.Component<IPartic
 
 
     saveToSnippet() {
     saveToSnippet() {
         const system = this.props.system;
         const system = this.props.system;
-        let content = JSON.stringify(system.serialize());
+        let content = JSON.stringify(system.serialize(true));
 
 
         var xmlHttp = new XMLHttpRequest();
         var xmlHttp = new XMLHttpRequest();
         xmlHttp.onreadystatechange = () => {
         xmlHttp.onreadystatechange = () => {
@@ -306,7 +306,7 @@ export class ParticleSystemPropertyGridComponent extends React.Component<IPartic
                     <CheckBoxLineComponent label="Is billboard" target={system} propertyName="isBillboardBased" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                     <CheckBoxLineComponent label="Is billboard" target={system} propertyName="isBillboardBased" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                     <CheckBoxLineComponent label="Is local" target={system} propertyName="isLocal" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                     <CheckBoxLineComponent label="Is local" target={system} propertyName="isLocal" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                     <CheckBoxLineComponent label="Force depth write" target={system} propertyName="forceDepthWrite" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                     <CheckBoxLineComponent label="Force depth write" target={system} propertyName="forceDepthWrite" onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
-                    <SliderLineComponent label="Update speed" target={system} propertyName="updateSpeed" minimum={0} maximum={1} step={0.01} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
+                    <SliderLineComponent label="Update speed" target={system} propertyName="updateSpeed" minimum={0} maximum={0.1} decimalCount={3} step={0.001} onPropertyChangedObservable={this.props.onPropertyChangedObservable} />
                 </LineContainerComponent>                      
                 </LineContainerComponent>                      
                 <LineContainerComponent globalState={this.props.globalState} title="COMMANDS">
                 <LineContainerComponent globalState={this.props.globalState} title="COMMANDS">
                     {this.renderControls()}
                     {this.renderControls()}

+ 3 - 2
src/Particles/IParticleSystem.ts

@@ -290,10 +290,11 @@ export interface IParticleSystem {
      */
      */
     clone(name: string, newEmitter: any): Nullable<IParticleSystem>;
     clone(name: string, newEmitter: any): Nullable<IParticleSystem>;
     /**
     /**
-     * Serializes the particle system to a JSON object.
+     * Serializes the particle system to a JSON object     
+     * @param serializeTexture defines if the texture must be serialized as well
      * @returns the JSON object
      * @returns the JSON object
      */
      */
-    serialize(): any;
+    serialize(serializeTexture: boolean): any;
     /**
     /**
      * Rebuild the particle system
      * Rebuild the particle system
      */
      */

+ 4 - 3
src/Particles/gpuParticleSystem.ts

@@ -1658,13 +1658,14 @@ export class GPUParticleSystem extends BaseParticleSystem implements IDisposable
     }
     }
 
 
     /**
     /**
-     * Serializes the particle system to a JSON object.
+     * Serializes the particle system to a JSON object
+     * @param serializeTexture defines if the texture must be serialized as well
      * @returns the JSON object
      * @returns the JSON object
      */
      */
-    public serialize(): any {
+    public serialize(serializeTexture = false): any {
         var serializationObject: any = {};
         var serializationObject: any = {};
 
 
-        ParticleSystem._Serialize(serializationObject, this);
+        ParticleSystem._Serialize(serializationObject, this, serializeTexture);
         serializationObject.activeParticleCount = this.activeParticleCount;
         serializationObject.activeParticleCount = this.activeParticleCount;
 
 
         return serializationObject;
         return serializationObject;

+ 14 - 7
src/Particles/particleSystem.ts

@@ -2068,13 +2068,14 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
     }
     }
 
 
     /**
     /**
-     * Serializes the particle system to a JSON object.
+     * Serializes the particle system to a JSON object
+     * @param serializeTexture defines if the texture must be serialized as well
      * @returns the JSON object
      * @returns the JSON object
      */
      */
-    public serialize(): any {
+    public serialize(serializeTexture = false): any {
         var serializationObject: any = {};
         var serializationObject: any = {};
 
 
-        ParticleSystem._Serialize(serializationObject, this);
+        ParticleSystem._Serialize(serializationObject, this, serializeTexture);
 
 
         serializationObject.textureMask = this.textureMask.asArray();
         serializationObject.textureMask = this.textureMask.asArray();
         serializationObject.customShader = this.customShader;
         serializationObject.customShader = this.customShader;
@@ -2102,7 +2103,7 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
     }
     }
 
 
     /** @hidden */
     /** @hidden */
-    public static _Serialize(serializationObject: any, particleSystem: IParticleSystem) {
+    public static _Serialize(serializationObject: any, particleSystem: IParticleSystem, serializeTexture: boolean) {
         serializationObject.name = particleSystem.name;
         serializationObject.name = particleSystem.name;
         serializationObject.id = particleSystem.id;
         serializationObject.id = particleSystem.id;
 
 
@@ -2123,8 +2124,12 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
         }
         }
 
 
         if (particleSystem.particleTexture) {
         if (particleSystem.particleTexture) {
-            serializationObject.textureName = particleSystem.particleTexture.name;
-            serializationObject.invertY = particleSystem.particleTexture._invertY;
+            if (serializeTexture) {
+                serializationObject.texture = particleSystem.particleTexture.serialize();
+            } else {
+                serializationObject.textureName = particleSystem.particleTexture.name;
+                serializationObject.invertY = particleSystem.particleTexture._invertY;
+            }
         }
         }
 
 
         serializationObject.isLocal = particleSystem.isLocal;
         serializationObject.isLocal = particleSystem.isLocal;
@@ -2395,7 +2400,9 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
     /** @hidden */
     /** @hidden */
     public static _Parse(parsedParticleSystem: any, particleSystem: IParticleSystem, scene: Scene, rootUrl: string) {
     public static _Parse(parsedParticleSystem: any, particleSystem: IParticleSystem, scene: Scene, rootUrl: string) {
         // Texture
         // Texture
-        if (parsedParticleSystem.textureName) {
+        if (parsedParticleSystem.texture) {
+            particleSystem.particleTexture = Texture.Parse(parsedParticleSystem.texture, scene, rootUrl) as Texture;
+        } else if (parsedParticleSystem.textureName) {
             particleSystem.particleTexture = new Texture(rootUrl + parsedParticleSystem.textureName, scene, false, parsedParticleSystem.invertY !== undefined ? parsedParticleSystem.invertY : true);
             particleSystem.particleTexture = new Texture(rootUrl + parsedParticleSystem.textureName, scene, false, parsedParticleSystem.invertY !== undefined ? parsedParticleSystem.invertY : true);
             particleSystem.particleTexture.name = parsedParticleSystem.textureName;
             particleSystem.particleTexture.name = parsedParticleSystem.textureName;
         }
         }