2
0
Эх сурвалжийг харах

Particles are working again

Panagiotis Christopoulos Charitos 12 жил өмнө
parent
commit
0183b765c9

+ 18 - 10
include/anki/scene/ParticleEmitter.h

@@ -91,7 +91,7 @@ protected:
 	F32 alpha = 1.0;
 
 private:
-	ParticleType type;
+	U8 type;
 };
 
 /// Simple particle for simple simulation
@@ -170,6 +170,19 @@ public:
 		SceneComponent::UpdateType) override;
 	/// @}
 
+	/// @name SpatialComponent virtuals
+	/// @{
+	const CollisionShape& getSpatialCollisionShape()
+	{
+		return aabb;
+	}
+
+	Vec3 getSpatialOrigin()
+	{
+		return getWorldTransform().getOrigin();
+	}
+	/// @}
+
 	/// @name RenderComponent virtuals
 	/// @{
 
@@ -183,16 +196,9 @@ public:
 	/// Implements  RenderComponent::getMaterial
 	const Material& getMaterial();
 
-	/// Overrides RenderComponent::getRenderWorldTransform
-	void getRenderWorldTransform(U index, Transform& trf) override
-	{
-		ANKI_ASSERT(index < 1);
-		trf = getWorldTransform();
-	}
-
 	Bool getHasWorldTransforms() override
 	{
-		return true;
+		return false;
 	}
 	/// @}
 
@@ -207,10 +213,12 @@ private:
 	// rotation is the identity
 	Bool identityRotation = true;
 
-	U32 aliveParticlesCount;
+	U32 aliveParticlesCount = 0;
+	U32 aliveParticlesCountDraw = 0;
 
 	Vao vao; ///< Hold the VBO
 	Vbo vbo; ///< Hold the vertex data
+	SceneVector<F32> clientBuffer;
 
 	void createParticlesSimulation(SceneGraph* scene);
 	void createParticlesSimpleSimulation(SceneGraph* scene);

+ 5 - 4
shaders/BsCommonFrag.glsl

@@ -35,8 +35,8 @@ void writeFais(in vec4 color)
 #	define particleAlpha_DEFINED
 void particleAlpha(in sampler2D tex, in float alpha)
 {
-	vec4 color = texture(tex, vTexCoord);
-	color.w *= alpha;
+	vec4 color = texture(tex, gl_PointCoord);
+	color.a *= alpha;
 	writeFais(color);
 }
 #endif
@@ -52,9 +52,10 @@ void particleSoft(in sampler2D depthMap, in sampler2D tex, in float alpha)
 	float delta = depth - gl_FragCoord.z;
 	float softalpha = clamp(delta * 100.0, 0.0, 1.0);
 
-	vec4 color = texture(tex, vTexCoord);
-	color.w *= alpha * softalpha;
+	vec4 color = texture(tex, gl_PointCoord);
+	color.a *= alpha * softalpha;
 	writeFais(color);
+	//writeFais(color * 0.0001 + vec4(1.0, 0.0, 1.0, 1.0));
 }
 #endif
 

+ 2 - 1
shaders/BsCommonVert.glsl

@@ -39,8 +39,9 @@ void setTexCoords(in vec2 x)
 #define particle_DEFINED
 void particle(in mat4 mvp)
 {
-	vTexCoord = texCoord;
 	gl_Position = mvp * vec4(position, 1);
+	vAlpha = alpha;
+	gl_PointSize = scale * float(RENDERING_WIDTH) / gl_Position.w;
 }
 
 //==============================================================================

+ 1 - 1
src/gl/Drawcall.cpp

@@ -15,7 +15,7 @@ Drawcall::Drawcall()
 //==============================================================================
 void Drawcall::enque()
 {
-	ANKI_ASSERT(primitiveType && instancesCount > 0);
+	ANKI_ASSERT(instancesCount > 0);
 	ANKI_ASSERT(drawCount > 0 && drawCount <= ANKI_MAX_MULTIDRAW_PRIMITIVES);
 
 	if(indicesType != 0)

+ 1 - 0
src/resource/ParticleEmitterResource.cpp

@@ -151,6 +151,7 @@ void ParticleEmitterResource::loadInternal(const XmlElement& rootel)
 		particle.startingPosDeviation);
 
 	xmlReadU(rootel, "maxNumberOfParticles", maxNumOfParticles);
+
 	xmlReadFloat(rootel, "emissionPeriod", emissionPeriod);
 	xmlReadU(rootel, "particlesPerEmittion", particlesPerEmittion);
 	U32 u = usePhysicsEngine;

+ 21 - 13
src/scene/ParticleEmitter.cpp

@@ -15,7 +15,8 @@ namespace anki {
 //==============================================================================
 F32 getRandom(F32 initial, F32 deviation)
 {
-	return (deviation == 0.0) ? initial
+	return (deviation == 0.0) 
+		? initial
 		: initial + randFloat(deviation) * 2.0 - deviation;
 }
 
@@ -43,7 +44,7 @@ Vec3 getRandom(const Vec3& initial, const Vec3& deviation)
 
 //==============================================================================
 ParticleBase::ParticleBase(ParticleType type_)
-	: type(type_)
+	: type((U8)type_)
 {}
 
 //==============================================================================
@@ -211,7 +212,8 @@ ParticleEmitter::ParticleEmitter(
 		SpatialComponent(this),
 		MoveComponent(this),
 		RenderComponent(this),
-		particles(getSceneAllocator())
+		particles(getSceneAllocator()),
+		clientBuffer(getSceneAllocator())
 {
 	addComponent(static_cast<MoveComponent*>(this));
 	addComponent(static_cast<SpatialComponent*>(this));
@@ -246,6 +248,8 @@ ParticleEmitter::ParticleEmitter(
 	vbo.create(GL_ARRAY_BUFFER, maxNumOfParticles * vertSize,
 		nullptr, GL_DYNAMIC_DRAW);
 
+	clientBuffer.resize(maxNumOfParticles * components, 0.0);
+
 	vao.create();
 	// Position
 	vao.attachArrayBufferVbo(&vbo, 0, 3, GL_FLOAT, GL_FALSE, vertSize, 0);
@@ -287,7 +291,7 @@ void ParticleEmitter::getRenderingData(
 	dc.instancesCount = 1;
 	dc.drawCount = 1;
 
-	dc.count = aliveParticlesCount;
+	dc.count = aliveParticlesCountDraw;
 	dc.offset = 0;
 }
 
@@ -357,8 +361,13 @@ void ParticleEmitter::createParticlesSimpleSimulation(SceneGraph* scene)
 void ParticleEmitter::frameUpdate(F32 prevUpdateTime, F32 crntTime, 
 	SceneNode::UpdateType uptype)
 {
-	if(uptype != SceneNode::ASYNC_UPDATE)
+	if(uptype == SceneNode::SYNC_UPDATE)
 	{
+		// In main thread update the client buffer
+
+		vbo.write(&clientBuffer[0]);
+		aliveParticlesCountDraw = aliveParticlesCount;
+
 		return;
 	}
 
@@ -370,11 +379,8 @@ void ParticleEmitter::frameUpdate(F32 prevUpdateTime, F32 crntTime,
 	Vec3 aabbmax(-std::numeric_limits<F32>::max());
 	aliveParticlesCount = 0;
 
-	// Create the alpha vectors
-	SceneFrameVector<F32> alpha(getSceneFrameAllocator());
-	alpha.reserve(particles.size());
-
-	F32* verts = (F32*)vbo.map(GL_MAP_WRITE_BIT);
+	F32* verts = &clientBuffer[0];
+	F32* verts_base = verts;
 
 	for(ParticleBase* p : particles)
 	{
@@ -417,12 +423,14 @@ void ParticleEmitter::frameUpdate(F32 prevUpdateTime, F32 crntTime,
 			verts[4] = sin((lifePercent) * getPi<F32>()) * p->alpha;
 
 			++aliveParticlesCount;
-			verts += 4;
+			verts += 5;
+
+			// Do checks
+			ANKI_ASSERT(
+				((PtrSize)verts - (PtrSize)verts_base) <= vbo.getSizeInBytes());
 		}
 	}
 
-	vbo.unmap();
-
 	if(aliveParticlesCount != 0)
 	{
 		aabb = Aabb(aabbmin - particle.size, aabbmax + particle.size);

+ 3 - 1
src/scene/SceneGraph.cpp

@@ -216,7 +216,9 @@ void SceneGraph::update(F32 prevUpdateTime, F32 crntTime, Renderer& renderer)
 			comp.reset();
 			comp.updateReal(node, prevUpdateTime, crntTime, 
 				SceneComponent::SYNC_UPDATE);
-		});	
+		});
+
+		node.frameUpdate(prevUpdateTime, crntTime, SceneNode::SYNC_UPDATE);
 	});
 
 	Threadpool& threadPool = ThreadpoolSingleton::get();

+ 7 - 4
testapp/Main.cpp

@@ -221,15 +221,18 @@ void init()
 		scene.getEventManager().newEvent(mevent, 0.0, 2.0, point, moveData);
 		mevent->enableBits(Event::EF_REANIMATE);
 
-		/*ParticleEmitter* pe;
-		scene.newSceneNode(pe,
+		ParticleEmitter* pe;
+		/*scene.newSceneNode(pe,
 			("pe" + std::to_string(i)).c_str(),
 			"particles/smoke.ankipart");
-		pe->setLocalOrigin(lightPos);
+		pe->setLocalOrigin(lightPos);*/
 
+		if(i == 0)
+		{
 		scene.newSceneNode(pe, ("pef" + std::to_string(i)).c_str(), 
 			"particles/fire.ankipart");
-		pe->setLocalOrigin(lightPos);*/
+		pe->setLocalOrigin(lightPos);
+		}
 	}
 #endif