Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
pixel texture rotation (pixel:setTextureRotation?) — Gideros Forum

pixel texture rotation (pixel:setTextureRotation?)

MoKaLuxMoKaLux Member
edited October 2020 in General questions
is it possible to rotate a pixel texture please?
Tiled_Flow = Core.class(Sprite)
 
function Tiled_Flow:init()
	local tex = Texture.new("mytex.png", false, {wrap = TextureBase.REPEAT})
	self.img = Pixel.new(tex, 64, 64)
	self:addChild(self.img)
	self.flowx, self.flowy = 0, 0
	self.flowspeedx, self.flowspeedy = 1, 1
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
 
function Tiled_Flow:onEnterFrame(e)
	self.flowx += self.flowspeedx or 0
	self.flowy += self.flowspeedy or 0
	self.img:setTexturePosition(self.flowx, self.flowy) -- this moves the texture on x and y
	-- is it possible to rotate the texture as well?
end
I tried using matrix but couldn't figure it out :'(
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • MoKaLuxMoKaLux Member
    edited October 2020
    For example I tried this but it doesn't rotate the texture:
    Tiled_Flow = Core.class(Sprite)
     
    function Tiled_Flow:init()
    	local tex = Texture.new("mytex.png", false, {wrap = TextureBase.REPEAT})
    	self.img = Pixel.new(tex, 64, 64)
    	self:addChild(self.img)
    	self.flowx, self.flowy = 0, 0
    	self.flowspeedx, self.flowspeedy = 1, 1
    	self.m = Matrix.new()
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function Tiled_Flow:onEnterFrame(e)
    	self.flowx += self.flowspeedx or 0
    	self.flowy += self.flowspeedy or 0
    	self.img:setTexturePosition(self.flowx, self.flowy)
    	-- the pixel texture won't rotate :-(
    	self.m:setRotationX(self.flowx * 8)
    	self.img:setTextureMatrix(self.m)
    end
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    edited October 2020 Accepted Answer
    I guess the only way is to write a shader?
    I tried to use lua shaders, but I have no idea how to get/set mat4 elements in vertex shader.
    @SinisterSoft can you help with that?)
    Or something like that:
    local tex = Texture.new("tileTexture.png", true, {wrap = Texture.REPEAT})
     
    local spr = Pixel.new(tex, 512, 512) -- fake texture
    spr:setAnchorPoint(.5, .5)
     
    local view = Viewport.new()
    view:setClip(0, 0, 128, 128)
    view:setContent(spr)
    stage:addChild(view)
     
    local time = 0
    stage:addEventListener("enterFrame", function(e)
    	local dt = e.deltaTime
    	time += dt
     
    	spr:setRotation(spr:getRotation() + 50 * dt)
    	spr:setPosition(
    		spr:getX() + math.cos(time) * 100 * dt,
    		spr:getY() + math.sin(time) * 100 * dt
    	)
    end)
  • MoKaLuxMoKaLux Member
    edited October 2020
    thanks a bunch rrraptor, I wanted to add some "quick" effect but this will have to wait until I dive into shaders then :smile:
    Oh I will also try your Viewport solution <3
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    Accepted Answer
    MoKaLux said:

    For example I tried this but it doesn't rotate the texture:

    Tiled_Flow = Core.class(Sprite)
    	-- the pixel texture won't rotate :-(
    	self.m:setRotationX(self.flowx * 8)
    	self.img:setTextureMatrix(self.m)
    There are two issues here: first setTextureMatrix doesn't do anything in gideros (I have just looked at the code and the function miss the important part, I have fixed it now). Second issue is that you should call setRotationZ(), not setRotationX()
    +1 -1 (+2 / -0 )Share on Facebook
  • I forgot to mention that I did try to rotate on X, Y and Z :)
    Thank you for fixing it (and having updated reactPhysics3d <3 )
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    Accepted Answer
    Here is the same effect using luashaders:
    --!NEEDS:luashader/luashader.lua
     
    local function vertexShader(vVertex,vColor,vTexCoord)
    	local vertex = hF4(vVertex,0.0,1.0)
    	fTexCoord=(tMatrix*hF4(vTexCoord,0,1)).xy
    	return vMatrix*vertex
    end
     
    local function fragmentShader()
    	 local frag=lF4(fColor)*texture2D(fTexture, fTexCoord)
    	 if (frag.a==0.0) then discard() end
    	 return frag
    end	
    local myTextureShader=Shader.lua(vertexShader,fragmentShader,0,
    	{
    	{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
    	{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
    	{name="fTexture",type=Shader.CTEXTURE,vertex=false},
    	{name="tMatrix",type=Shader.CMATRIX,vertex=true},
    	},
    	{
    	{name="vVertex",type=Shader.DFLOAT,mult=2,slot=0,offset=0},
    	{name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0},
    	{name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0},
    	},
    	{
    	{name="fTexCoord",type=Shader.CFLOAT2},
    	}
    	)
     
    local tex = Texture.new("mytex.png", true, {wrap = Texture.REPEAT})
     
    local spr = Pixel.new(tex, 512, 512) -- fake texture
    spr:setAnchorPoint(.5, .5)
    spr:setShader(myTextureShader)
     
    stage:addChild(spr)
    stage:addEventListener("enterFrame", function(e) 
    	local mat=Matrix.new()
    	mat:setRotationZ(10 * os:timer())
    	mat:setPosition(os:timer(),os:timer())
    	spr:setShaderConstant("tMatrix",Shader.CMATRIX,1,mat:getMatrix())
    end)
    +1 -1 (+2 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    Accepted Answer
    Does this work with textures that wrap? If so, it could be very nice for effects.

    Likes: MoKaLux

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Does this work with textures that wrap? If so, it could be very nice for effects.

    yes, that's what I am after, I want to simulate a rolling water effect :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited December 2020
    There are two issues here: first setTextureMatrix doesn't do anything in gideros (I have just looked at the code and the function miss the important part, I have fixed it now). Second issue is that you should call setRotationZ(), not setRotationX()
    feedback: it works now :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.