Browse Source

GPUEmitter particle life is increasing (was decreasing so curves and gradients where flipped). Normalize RelativeTransformSimulation.hx with lifeTime.

clementlandrin 2 months ago
parent
commit
bcf51c8137

+ 5 - 2
hrt/prefab/fx/gpuemitter/BaseSimulation.hx

@@ -8,6 +8,7 @@ class BaseSimulation extends ComputeUtils {
 		@param var particleBuffer : RWPartialBuffer<{
 			speed : Vec3,
 			life : Float,
+			lifeTime : Float,
 			random : Float,
 			color : Float,
 		}>;
@@ -28,6 +29,7 @@ class BaseSimulation extends ComputeUtils {
 		var dt : Float;
 		var speed : Vec3;
 		var life : Float;
+		var lifeTime : Float;
 		var particleRandom : Float;
 		var particleColor : Vec4;
 		var modelView : Mat4;
@@ -38,10 +40,11 @@ class BaseSimulation extends ComputeUtils {
 			dt = dtParam;
 			speed = particleBuffer[computeVar.globalInvocation.x].speed;
 			life = particleBuffer[computeVar.globalInvocation.x].life;
+			lifeTime = particleBuffer[computeVar.globalInvocation.x].lifeTime;
 			prevModelView = batchBuffer[computeVar.globalInvocation.x].modelView;
 			particleRandom = particleBuffer[computeVar.globalInvocation.x].random;
 			particleColor = int2rgba(floatBitsToInt(particleBuffer[computeVar.globalInvocation.x].color));
-			relativeTransform = scaleMatrix((life > 0.0 ? 1.0 : 0.0) * (computeVar.globalInvocation.x > curCount ? 0.0 : 1.0) * vec3(particleRandom * (maxSize - minSize) + minSize));
+			relativeTransform = scaleMatrix((life < lifeTime ? 1.0 : 0.0) * (computeVar.globalInvocation.x > curCount ? 0.0 : 1.0) * vec3(particleRandom * (maxSize - minSize) + minSize));
 		}
 
 		function main() {
@@ -56,7 +59,7 @@ class BaseSimulation extends ComputeUtils {
 				newPos = ((newPos - boundsPos) % boundsSize) + boundsPos;
 			modelView = relativeTransform * align * translationMatrix(newPos);
 			var idx = computeVar.globalInvocation.x;
-			particleBuffer[idx].life = life - dt;
+			particleBuffer[idx].life = life + dt;
 			particleBuffer[idx].speed = speed;
 			particleBuffer[idx].color = intBitsToFloat(rgba2int(particleColor));
 			batchBuffer[idx].modelView = modelView;

+ 4 - 2
hrt/prefab/fx/gpuemitter/BaseSpawn.hx

@@ -43,7 +43,9 @@ class BaseSpawn extends ComputeUtils {
 
 		function main() {
 			var idx = computeVar.globalInvocation.x;
-			if ( FORCED || (!INFINITE && particleBuffer[idx].life < 0.0) ) {
+			var curLife = particleBuffer[idx].life;
+			var curLifeTime = particleBuffer[idx].lifeTime;
+			if ( FORCED || (!INFINITE && curLife > curLifeTime ) ) {
 				var c = atomicAdd(atomic, 0, 1);
 				if ( FORCED || (!INFINITE && c < rate) ) {
 					batchBuffer[idx].modelView = modelView;
@@ -51,7 +53,7 @@ class BaseSpawn extends ComputeUtils {
 					if ( SPEED_NORMAL )
 						s = emitNormal;
 					particleBuffer[idx].speed = s * maxStartSpeed;
-					particleBuffer[idx].life = life;
+					particleBuffer[idx].life = 0.0;
 					// Keep in memory duration of particle to normalize curve update.
 					particleBuffer[idx].lifeTime = life;
 					particleBuffer[idx].color = intBitsToFloat(rgba2int(particleColor));

+ 1 - 2
hrt/prefab/fx/gpuemitter/GPUEmitterObject.hx

@@ -26,7 +26,6 @@ class ParticleShader extends hxsl.Shader {
 		var transformedPosition : Vec3;
 		function __init__vertex() {
 			{
-				// TODO : particleLife is decreasing, need to be flipped to guarantee compatibility with regular emitter shaders.
 				particleLife = particleBuffer[instanceID].life;
 				particleLifeTime = particleBuffer[instanceID].lifeTime;
 				particleRandom = particleBuffer[instanceID].random;
@@ -164,8 +163,8 @@ class GPUEmitterObject extends h3d.scene.MeshBatch {
 				// floats[i * stride] = 0.0;
 				// floats[i * stride + 1] = 0.0;
 				// floats[i * stride + 2] = 0.0;
-				floats[i * stride + 3] = 0.0; // life warmup
 				var l = hxd.Math.random() * (data.maxLifeTime - data.minLifeTime) + data.minLifeTime;
+				floats[i * stride + 3] = l; // life warmup
 				floats[i * stride + 4] = l; // lifeTime
 				floats[i * stride + 5] = hxd.Math.random(); // random
 				// color

+ 2 - 2
hrt/prefab/fx/gpuemitter/RelativeTransformSimulation.hx

@@ -29,6 +29,7 @@ class RelativeTransformSimulationShader extends ComputeUtils {
 		@param var curveTexture : Sampler2D;
 
 		var life : Float;
+		var lifeTime : Float;
 		var relativeTransform : Mat4;
 
 		function hasBit(n : Int) : Bool {
@@ -36,7 +37,7 @@ class RelativeTransformSimulationShader extends ComputeUtils {
 		}
 
 		function getCurve(n : Int) : Float {
-			return curveTexture.getLod(vec2(life, (n + 0.5) / 9.0), 0.0).r;
+			return curveTexture.getLod(vec2(life / lifeTime, (n + 0.5) / 9.0), 0.0).r;
 		}
 
 		function main() {
@@ -122,7 +123,6 @@ class RelativeTransformSimulation extends SimulationShader {
 
 		var curveNames = ["x", "y", "z", "scaleX", "scaleY", "scaleZ", "rotX", "rotY", "rotZ"];
 
-
 		var pixels = hxd.Pixels.alloc(width, height, s.curveTexture.format);
 		for ( c in curves ) {
 			if ( !c.shouldBeInstanciated() )