How do I create multiple particlefx that are exact copies of each other? All of these particle fx will be spawned through collection factory at the same time. It will be used for parallax effect, like stars. If they’re random to each other, then the parallax change will be noticeable. Is it possible to make it more deterministic?
I guess you could render it to a render target and use that on multiple sprites.
Alrite that would work too, thanks!
Apparently it seems difficult for me to do. I’m not really sure how to render the particlefx to a render target, and have that render target rendered into a sprite. Can you help me on how I should do it?
My steps were like this but it just did not work:
- I created a custom particlefx material called parallax_particlefx.material, changed the tag “particle” to “parallax_particle”, then use it for my particlefx
- I created a custom sprite material called parallax_sprite.material, changed the tag “tile” to “parallax_sprite”, then use it for my dummy sprite to render to. The dummy sprite size is set manually to 640 x 640, and it has a default image of a 32 x 32 rectangular white dot
- I added some code that I got from API example on how to create render target and how to use it to my project’s default render script, with a little assumption over how the texture draw to the sprite works:
function init(self)
...
self.parallax_particle_pred = render.predicate({"parallax_particle"})
self.parallax_sprite_pred = render.predicate({"parallax_sprite"})
local color_params = { format = render.FORMAT_RGBA,
width = 640,
height = 640,
min_filter = render.FILTER_LINEAR,
mag_filter = render.FILTER_LINEAR,
u_wrap = render.WRAP_CLAMP_TO_EDGE,
v_wrap = render.WRAP_CLAMP_TO_EDGE }
local depth_params = { format = render.FORMAT_DEPTH,
width = 640,
height = 640,
u_wrap = render.WRAP_CLAMP_TO_EDGE,
v_wrap = render.WRAP_CLAMP_TO_EDGE }
self.parallax_render_target = render.render_target({
[render.BUFFER_COLOR_BIT] = color_params, [render.BUFFER_DEPTH_BIT] = depth_params })
end
function update(self)
... -- after render models
render.set_render_target(self.parallax_render_target)
render.draw(self.parallax_particle_pred)
render.set_render_target(render.RENDER_TARGET_DEFAULT)
render.enable_texture(0, self.parallax_render_target, render.BUFFER_COLOR_BIT)
render.draw(self.parallax_sprite_pred)
render.disable_texture(0)
... -- before the render sprites, label, particles
I’m honestly not sure what configuration I need when creating the render target, but I’m just ensuring that the render target size is as big as the dummy sprite size, assuming that matters. Secondly, as I said before I have zero idea how to render the render target to sprite, so I am assuming that texture 0 is what I need to set for the render target, that the sprite would use it (?), then draw it.
Well that doesn’t work, as in, none of the particles are rendered.
So I tried moving the enable texture/draw parallax sprite pred/disable texture into within the “render sprites, label, particles” part, but of course it also doesn’t work. Like this:
-- render sprites, label, particles
--
render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.enable_state(render.STATE_DEPTH_TEST)
render.enable_state(render.STATE_STENCIL_TEST)
render.enable_state(render.STATE_BLEND)
render.draw(self.tile_pred)
-- here still won't work
render.enable_texture(0, self.parallax_render_target, render.BUFFER_COLOR_BIT)
render.draw(self.parallax_sprite_pred)
render.disable_texture(0)
render.draw(self.particle_pred)
render.disable_state(render.STATE_STENCIL_TEST)
render.disable_state(render.STATE_DEPTH_TEST)
It’s too expensive to use particlefx for this anyway in mobile so I decided to just use simple repeating textures instead.
Also if anyone can point me a very simple example of render texture in Defold please let me know because I’m still not successful to make it work (the anaglyphic example is definitely not the simple example!), thanks!