This page explains creation and destruction. For creation, this page describes loading and operating on ResGraphicsFiles, generating instances, and registering to a scene tree. For destruction, this page describes precautions when destroying generated instances.
ResGraphicsFileThe first step is to load the particle binary (a bcptl file) and prepare the ResGraphicsFile. The demo uses the demo::Utility::LoadResources function. Call the gfx::ResGraphicsFile::Setup function once the file is done loading. Generation up to this point is the same as for the SkeletalModel and Model objects. See Model Display for more details.
ResGraphicsFile OperationsYou can configure the following gfx::ResGraphicsFile settings for particles.
ResGraphicsFile Textures into VRAM Load textures into VRAM for faster processing. You must configure this setting before calling the gfx::ResGraphicsFile::Setup function. See Using VRAM for details.
Use a material class dedicated to particles for faster processing. See Using ParticleMaterialActivator for details.
If the binary was output with separate shader resources, you must load these and configure them.
// Sample code for attaching shader resources loaded from external files.
nw::demo::ResourceSet* shaderResource;
nw::gfx::ResGraphicsFile* graphicsFile;
// Load shader resource from file.
{
shaderResource = deviceAllocator->AllocAndConstruct<nw::demo::ResourceSet>(1);
shaderResource.buffer =
nw::demo::Utility::LoadFile(deviceAllocator, "nwgfx_ParticleDefaultShader.bcsdr");
shaderResource.resource =
nw::gfx::ResGraphicsFile(&(shaderResource->buffer.front()));
}
// Set up the resource, and if there are no shader resources, specify the separately loaded shader resources and set up again.
//
nw::gfx::Result result = graphicsFile.Setup(deviceAllocator);
if(result.IsFailure())
{
if (result.GetDescription() & nw::gfx::RESOURCE_RESULT_NOT_FOUND_SHADER)
{
result = graphicsFile.Setup(deviceAllocator, shaderResource);
}
}
Generate instances of the ParticleModel and ParticleEmitter classes from the loaded ResGraphicsFile. Call the nw::gfx::SceneBuilder function to generate instances. Then call the gfx::SceneHelper::ResolveReference function to resolve references for the generated instances. Generation up to this point is the same as for the SkeletalModel and Model objects. See Model Display for more details.
For particles, you must also call the gfx::ParticleUtil::SetupParticleObject function to resolve internal references and initialize random numbers.
References are resolved within an array given as an argument. You must take care within the array to avoid ParticleSet/ParticleEmitter name collisions. The array is passed as an argument in order to explicitly limit the scope of references. This is useful when creating multiple instances from a single resource.
Initialize the ParticleEmitter and ParticleSet random number seeds using the random number from nw::gfx::ParticleContext.
There are basically three different particle classes: ParticleModel, ParticleSet, and ParticleEmitter. The reference relationships for each are as follows. Access is only possible in the direction of the arrows.
Has a: [ParticleModel] → [ParticleSet] ← [ParticleEmitter] References
The gfx::ParticleUtil::SetupParticleObject function internally resolves ParticleEmitter references to the ParticleSet instance to be emitted.
The particle animation begins playing once you register all initialized instances to the scene tree. You must call SceneInitializer or SceneTraverser after registering.
You must take care when destroying any ParticleModel or ParticleEmitter instances, and when releasing any render buffers. After issuing any rendering commands, do not destroy any instances until the drawing is actually complete. Destroying an instance after issuing a rendering command is extremely dangerous as the GPU might try to access the render buffer belonging to the destroyed instance. The same goes for textures. When command buffers are doubled, the GPU attempts to access the render buffer the frame after a rendering command is issued, so you must skip one frame before destroying any instances.
CONFIDENTIAL