فهرست منبع

[gradient] Fix editor cache

Clément Espeute 11 ماه پیش
والد
کامیت
6bd152b7d3
3فایلهای تغییر یافته به همراه31 افزوده شده و 44 حذف شده
  1. 2 2
      hide/comp/SceneEditor.hx
  2. 23 40
      hrt/impl/Gradient.hx
  3. 6 2
      hrt/prefab/DynamicShader.hx

+ 2 - 2
hide/comp/SceneEditor.hx

@@ -3728,7 +3728,7 @@ class SceneEditor {
 			ser.push(prefab.serialize());
 		}
 
-		view.setClipboard(haxe.Json.stringify(ser), "prefab", {source : view.state.path});
+		view.setClipboard(Ide.inst.toJSON(ser), "prefab", {source : view.state.path});
 	}
 
 	function getDataPath( prefabName : String, ?sourceFile : String ) {
@@ -3995,7 +3995,7 @@ class SceneEditor {
 		var lastIndex = lastElem.parent.children.indexOf(lastElem);
 		beginRebuild();
 		for(i => elt in elements) {
-			@:pirvateAccess var clone = hrt.prefab.Prefab.createFromDynamic(haxe.Json.parse(haxe.Json.stringify(elt.serialize())), null, elt.parent.shared);
+			@:pirvateAccess var clone = hrt.prefab.Prefab.createFromDynamic(haxe.Json.parse(Ide.inst.toJSON(elt.serialize())), null, elt.parent.shared);
 			var index = lastIndex+1+i;
 			elt.parent.children.insert(index, clone);
 			@:bypassAccessor clone.parent = elt.parent;

+ 23 - 40
hrt/impl/Gradient.hx

@@ -22,7 +22,6 @@ typedef GradientData = {
 
 #if editor
 typedef EditorCacheData = {
-	var oldHash : Int;
 	var tex : Texture;
 };
 #end
@@ -193,9 +192,22 @@ class Gradient {
     }
 
     public static function textureFromData(data : GradientData) : h3d.mat.Texture {
+        function genPixels() {
+            var xScale = data.isVertical ? 0 : 1;
+            var yScale = 1 - xScale;
+            var pixels = hxd.Pixels.alloc(data.resolution * xScale + 1 * yScale,1 * xScale + data.resolution * yScale, ARGB);
 
+            var vec = new Vector4();
+            for (x in 0...data.resolution) {
+                evalData(data, x / (data.resolution-1), vec);
+                pixels.setPixelF(x * xScale,x*yScale, vec);
+            }
+            return pixels;
+        }
+
+
+        #if !editor
         var hash = getDataHash(data);
-		#if !editor
 
         var cache = getCache();
         var entry = cache.get(hash);
@@ -204,59 +216,30 @@ class Gradient {
             return entry;
         }
 		#else
+
 		var cache = getEditorCache();
 		var entry = cache.get(data);
 		if (entry != null)
 		{
-            if (entry.oldHash != hash) {
-                entry.oldHash = hash;
-                entry.tex.realloc();
+            var texture = entry.tex;
+            var pixels = genPixels();
+            if (pixels.width != texture.width || pixels.height != texture.height) {
+                texture.resize(pixels.width, pixels.height);
             }
-            return entry.tex;
+            texture.uploadPixels(pixels);
+            return texture;
 		}
 		#end
 
-        function genPixels() {
-            #if editor
-            var newHash = Gradient.getDataHash(data);
-
-            var cache = getEditorCache();
-            var entry = cache.get(data);
-            if (entry != null) {
-                if (entry.oldHash != newHash) {
-                    throw "gradient data has changed between first generation and realloc";
-                }
-            }
-
-            // If this ever become an issue because we need this feature, we just need to deep copy 'data'
-            // and use this copy in the genPixels function. But at this moment we consider that it's a bug
-            #end
-            var xScale = data.isVertical ? 0 : 1;
-            var yScale = 1 - xScale;
-            var pixels = hxd.Pixels.alloc(data.resolution * xScale + 1 * yScale,1 * xScale + data.resolution * yScale, ARGB);
-
-            var vec = new Vector4();
-            for (x in 0...data.resolution) {
-                evalData(data, x / (data.resolution-1), vec);
-                pixels.setPixelF(x * xScale,x*yScale, vec);
-            }
-            return pixels;
-        }
-
-
         var texture = Texture.fromPixels(genPixels(), RGBA);
         texture.realloc = function() {
-            var pixels = genPixels();
-            if (pixels.width != texture.width || pixels.height != texture.height) {
-                texture.resize(pixels.width, pixels.height);
-            }
-            texture.uploadPixels(pixels);
+            texture.uploadPixels(genPixels());
         }
 
 		#if !editor
         cache.set(hash, texture);
 		#else
-		cache.set(data, {oldHash: hash, tex: texture});
+		cache.set(data, {tex: texture});
 		#end
 
         return texture;

+ 6 - 2
hrt/prefab/DynamicShader.hx

@@ -13,7 +13,6 @@ class DynamicShader extends Shader {
 
 	override function copy(other:Prefab) {
 		super.copy(other);
-		var shaderDef = Std.downcast(other, DynamicShader)?.getShaderDefinition();
 	}
 
 	override function setShaderParam(shader:hxsl.Shader, v:hxsl.Ast.TVar, value:Dynamic) {
@@ -111,7 +110,12 @@ class DynamicShader extends Shader {
 			// TODO: Where to init prefab default values?
 			for( v in shaderDef.inits ) {
 				if(!Reflect.hasField(props, v.variable.name)) {
-					Reflect.setField(props, v.variable.name, v.value);
+					var value = v.value;
+					#if editor
+					// deep copy gradients
+					value = haxe.Json.parse(hide.Ide.inst.toJSON(value));
+					#end
+					Reflect.setField(props, v.variable.name, value);
 				}
 			}
 			for(v in shaderDef.shader.data.vars) {