Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Update mesh texture and normal map shader question — Gideros Forum

Update mesh texture and normal map shader question

piepie Member
edited April 2016 in General questions
Hi,
after some tests I am trying to insert normal map shader into my game.
I have a "world" object which manages textures and objects, it renders the double texture (classic + normal = allTextures) then set it as texture to a Mesh. (tilemapmesh)
I added a method to update the texture because sometimes the elements on this world-layer change.
It all works well if I remove the mesh and create a new one, but it seems to be CPU intensive because my frame rate drops a little.
If I just clearTexture and set it again with the newly rendered one, something "goes wrong" ( gideros player crash without warning at the first update call)

From my understanding it should be "easier" to just update a texture instead of removing a mesh, collect it as garbage and draw it again. Why it isn't for me? :) Can you see what am I doing wrong?


Here's my relevant code portion:

removing mesh
	local nmrt = RenderTarget.new(xp2, hh, true) --xp2 is the powerof2 size of the double texture (allTextures)
	nmrt:draw(allTextures)
if self.tilemapmesh then --update texture
--Remove mesh and create a new one, works but it's "slow"
		self.tilemapmesh:removeFromParent()
		self.tilemapmesh = nil
		collectgarbage()
end
 
 
 --create mesh
		self.tilemapmesh = Mesh.new()
		self.tilemapmesh:setVertexArray(x, y, x+w, y, x+w, y+h, x, y+h)
		self.tilemapmesh:setTextureCoordinateArray(x, y, x + w, y, x + w, y + h, x, y + h)
		self.tilemapmesh:setIndexArray(1, 2, 3, 1, 3, 4)
		self.tilemapmesh:setTexture(nmrt)
		self.tilemapmesh:setShader(self.effect)
		self:addChild(self.tilemapmesh)

just updating texture
	local nmrt = RenderTarget.new(xp2, hh, true) --xp2 is the powerof2 size of the double texture (allTextures)
	nmrt:draw(allTextures)
if self.tilemapmesh then --update texture
 
--just update mesh texture 
		self.tilemapmesh:clearTexture()
		self.tilemapmesh:setTexture(nmrt)
 
else --create mesh
		self.tilemapmesh = Mesh.new()
		self.tilemapmesh:setVertexArray(x, y, x+w, y, x+w, y+h, x, y+h)
		self.tilemapmesh:setTextureCoordinateArray(x, y, x + w, y, x + w, y + h, x, y + h)
		self.tilemapmesh:setIndexArray(1, 2, 3, 1, 3, 4)
		self.tilemapmesh:setTexture(nmrt)
		self.tilemapmesh:setShader(self.effect)
		self:addChild(self.tilemapmesh)
end
Thank you :)

Dislikes: Yan

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • hgy29hgy29 Maintainer
    Since you set a new texture right after, then clearTexture is not needed, right ? What happens if you remove it ?
  • piepie Member
    @hgy29 unfortunately the same player crash
  • piepie Member
    @hgy29 I think I got it:
    I have to collectgarbage() after setTexture(), and of course you're right I don't need clearTexture()
    FPS is still lower than expected, from my past experience I think it's collectgarbage's fault but it's needed here, I can't do without it unless I want to see my texture memory double at each passage. Can you think of any alternative?
    Thank you :)
  • hgy29hgy29 Maintainer
    Accepted Answer
    Can't you just reuse the same rendertarget ?

    Likes: SinisterSoft, pie

    +1 -1 (+2 / -0 )Share on Facebook
  • piepie Member
    Thanks for reminding me that I tend to overcomplicate things..it seems to work perfectly. :)>-
  • piepie Member
    edited May 2016
    I'm sorry to bother you again but I still have an issue: on top of my working normalmapped world, I am placing another Sprite with some objects and then I renderTarget everything.
    I think I am doing something wrong, but if I look at the whole texture (classic+normal) I'd say it's fine.
    The shader works well on the layer below, but on the top layer the normal map is "white", I see it change its nuance when I move the lights but I am unable to understand what's happening: any suggestion on what could be the problem? It seems that it's not blending with the "classic texture".

    thank you :)

    [edit: fixed with a dirty delay: I was rendering the texture BEFORE all the objects were placed on the "classic side"]
    grr.png
    343 x 303 - 134K

    Likes: hgy29

    grr.png 134.4K
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.