Browse Source

ShaderTarget: fix leak

LeoVgr 8 months ago
parent
commit
4ff5843dea
2 changed files with 19 additions and 7 deletions
  1. 14 4
      hrt/prefab/fx/FX.hx
  2. 5 3
      hrt/prefab/fx/ShaderTarget.hx

+ 14 - 4
hrt/prefab/fx/FX.hx

@@ -31,6 +31,7 @@ class FXAnimation extends h3d.scene.Object {
 	public var customAnims : Array<BaseFX.CustomAnimation> = [];
 	public var constraints : Array<hrt.prefab.l3d.Constraint>;
 	public var effects : Array<hrt.prefab.rfx.RendererFX>;
+	public var shaderTargets : Array<hrt.prefab.fx.ShaderTarget.ShaderTargetObj>;
 
 	var evaluator : Evaluator;
 	var parentFX : FXAnimation;
@@ -39,8 +40,6 @@ class FXAnimation extends h3d.scene.Object {
 	var randSeed : Int;
 	var firstSync = true;
 
-	var onRemoveFun : Void -> Void = null;
-
 	public function new(?parent) {
 		super(parent);
 		randSeed = #if editor 0 #else Std.random(0xFFFFFF) #end;
@@ -53,6 +52,10 @@ class FXAnimation extends h3d.scene.Object {
 	function init(def: FX, ?root: PrefabElement) {
 		if(root == null)
 			root = def;
+
+		for (shaderTarget in def.flatten(hrt.prefab.fx.ShaderTarget))
+			shaderTarget.applyShaderTarget(def, shaderTarget.target);
+
 		initObjAnimations(root);
 		initEmitters(root);
 		hrt.prefab.fx.BaseFX.BaseFXTools.getCustomAnimations(root, customAnims, null);
@@ -515,8 +518,6 @@ class FXAnimation extends h3d.scene.Object {
 	}
 
 	override function onRemove() {
-		if ( onRemoveFun != null)
-			onRemoveFun();
 		if ( effects != null ) {
 			var scene = getScene();
 			if ( scene != null ) {
@@ -528,6 +529,10 @@ class FXAnimation extends h3d.scene.Object {
 			}
 		}
 
+		if (shaderTargets != null)
+			for (st in shaderTargets)
+				st.onRemove();
+
 		super.onRemove();
 	}
 }
@@ -621,6 +626,11 @@ class FX extends Object3D implements BaseFX {
 		fxAnim.init(this, root);
 	}
 
+	public function setTarget(target : h3d.scene.Object) {
+		for (shaderTarget in this.findAll(hrt.prefab.fx.ShaderTarget))
+			shaderTarget.target = target;
+	}
+
 	function createInstance(parent: h3d.scene.Object) : FXAnimation {
 		return new FXAnimation(parent);
 	}

+ 5 - 3
hrt/prefab/fx/ShaderTarget.hx

@@ -20,9 +20,9 @@ class ShaderTargetObj extends h3d.scene.Object {
 		if (fxAnim == null)
 			return;
 
-		@:privateAccess fxAnim.onRemoveFun = () -> {
-			this.remove();
-		}
+		if (fxAnim.shaderTargets == null)
+			fxAnim.shaderTargets = [];
+		fxAnim.shaderTargets.push(this);
 	}
 
 	override function onRemove() {
@@ -62,6 +62,8 @@ class ShaderTarget extends Object3D {
 	}
 
 	public function applyShaderTarget(fx : hrt.prefab.fx.FX, target : h3d.scene.Object) {
+		if (target == null)
+			return;
 		var o = new hrt.prefab.fx.ShaderTarget.ShaderTargetObj(target);
 		o.priority = this.priority;
 		o.tag = this.tag;