havenphillip Posted October 1, 2018 Share Posted October 1, 2018 I have a shader that allows me to move a texture1 within the masked space of a texture0 but I'm only able to move it in a line. I need it to rotate. How can I do that? Here's the fragment shader: #version 400 uniform vec4 drawcolor; uniform sampler2D texture0; uniform sampler2D texture1; uniform float t; // e.g. 0.4 uniform vec2 bar = vec2(1.0,1.0); uniform bool isbackbuffer; in vec2 vTexCoords0; out vec4 fragData0; void main(void) { vec2 UV = (bar); if (isbackbuffer) UV.xy = 1.0 - UV.xy; vec4 mask=texture(texture0,vTexCoords0); if (mask.r < 0.1 && mask.a >0) { vec4 t0 = texture(texture0, vTexCoords0); vec4 t1 = texture(texture1, vTexCoords0+UV); mask = mix(t0, t1, vec4(vTexCoords0.x >= t)); } else { discard; } fragData0 = mask; } //vec2 turn = vec2(-(cos(2)*(UV.x/2) - sin(2)*(UV.y/2)),(-sin(2)*(UV.x/2) - cos(2)*(UV.y/2))); Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted October 2, 2018 Share Posted October 2, 2018 You could use sin() and cos() to get an xy position based on an angle. UV.x = sin(angle) UV.y = cos(angle) Quote Link to comment Share on other sites More sharing options...
havenphillip Posted October 2, 2018 Author Share Posted October 2, 2018 Since the time I asked this question I made a little progress. I have it rotating now using the vertex, but I can't get it to rotate from the center. It centers at the top-left. Here it is: Fragment: #version 400 uniform vec4 drawcolor; uniform sampler2D texture0; uniform sampler2D texture1; uniform float t; // e.g. 0.4 in vec2 vTexCoords0; in vec2 vTexCoords1; out vec4 fragData0; void main(void) { vec4 mask=texture(texture0,vTexCoords1); if (mask.r < 0.1 && mask.a > 0.0) { vec4 t0 = texture(texture0, vTexCoords1); vec4 t1 = texture(texture1, vTexCoords0); mask = mix(t0, t1, vec4(vTexCoords1.x >= t)); } else { discard; } fragData0 = mask; } Vertex: #version 400 uniform mat4 projectionmatrix; uniform mat4 drawmatrix; uniform vec2 position[4]; uniform vec2 texcoords[4]; uniform float turn = 0.0; in vec3 vertex_position; in vec2 vertex_texcoords0; out vec2 vTexCoords0; out vec2 vTexCoords1; void main(void) { vec2 pos = position[gl_VertexID]; mat2 rot = mat2( cos( radians(turn)), -sin( radians(turn)), sin( radians(turn)), cos(radians(turn))); gl_Position = projectionmatrix * (drawmatrix * vec4(pos, 0.0, 1.0)); vTexCoords1 = texcoords[gl_VertexID]; vTexCoords0 = texcoords[gl_VertexID] * rot; } Quote Link to comment Share on other sites More sharing options...
Ma-Shell Posted October 3, 2018 Share Posted October 3, 2018 In order to move the rotation center, you need to translate the point, such that the new rotation center lays at (0, 0), then apply the rotation matrix and then translate it back. This means, you need to change the last line to something similar to vTexCoords0 = (texcoords[gl_VertexID] - vec2(0.5, 0.5)) * rot + vec2(0.5, 0.5); I haven't tried it out, so you might need to switch the minus and the plus or play around a little bit but something like this should do the trick Quote Link to comment Share on other sites More sharing options...
GorzenDev Posted October 3, 2018 Share Posted October 3, 2018 i think this will get you the answer you need. Quote Link to comment Share on other sites More sharing options...
havenphillip Posted October 4, 2018 Author Share Posted October 4, 2018 Ah yes that worked perfectly, Ma-Shell. Thank you so much! GorzenDev that's the post I started from. The only thing was I needed to rotate the texture without rotating the quad/mask texture itself. Here's the script I'm using with the vertex and fragment of the shader for those wandering by: Script: Script.pos = Vec2(49,60) -- Vec2 "Position" Script.size = Vec2(210,215) -- Vec2 "Size" function Script:Start() self.shader = Shader:Load("HUD Elements/rotate attempt/weird.shader") self.intex = Texture:Load("Materials/Developer/bluegrid.tex") self.mask= Texture:Load("Materials/Developer/Flat Black.tex") self.angle=0 end function Script:UpdateWorld() self.angle = Time:GetCurrent()/1000 end function Script:PostRender(context) local a = self.a local x = self.pos.x local y = self.pos.y local w = self.size.x local h = self.size.y shd = context:GetShader() self.intex:Bind(1) self.shader:SetFloat("t", -1.0) self.shader:SetFloat("angle", self.angle) context:SetBlendMode(1) context:SetColor(1,1,1,1) context:SetShader(self.shader) context:DrawImage(self.mask, x, y , w, h) context:SetShader(shd) end ------------------------------------------------------------------------------ Fragment of shader: #version 400 uniform vec4 drawcolor; uniform sampler2D texture0; uniform sampler2D texture1; uniform float t; in vec2 vTexCoords0; in vec2 vTexCoords1; out vec4 fragData0; void main(void) { vec4 mask=texture(texture0,vTexCoords0); if (mask.r < 0.1 && mask.a > 0.0) { vec4 t0 = texture(texture0, vTexCoords0); vec4 t1 = texture(texture1, vTexCoords1); mask = mix(t0, t1, vec4(vTexCoords1.x >= t)); } else { discard; } fragData0 = mask; } ------------------------------------------------------------------------------ Vertex of shader: #version 400 uniform mat4 projectionmatrix; uniform mat4 drawmatrix; uniform vec2 position[5]; uniform vec2 texcoords[4]; uniform float angle = 0.0; out vec2 vTexCoords0; out vec2 vTexCoords1; void main(void) { float s = sin(angle); float c = cos(angle); mat2 RotationMatrix = mat2(c,-s,s,c); vec2 pos = position[gl_VertexID]; gl_Position = projectionmatrix * (drawmatrix * vec4(pos, 0.0, 1.0)); vTexCoords0 = texcoords[gl_VertexID]; vTexCoords1 = (texcoords[gl_VertexID] - vec2(0.5, 0.5)) * RotationMatrix + vec2(0.5, 0.5);; } 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.