Hello everyone,
i am working on a game project and i face difficulties understanding how to use shader on sf::Text.
What i wanted to do is in the title and i dont realize if this is possible or not. I wanted to spin a text to have a fake 3D rotation effect. I Found a shader that does the spin effect in shadertoy and adapt it :
.frag
precision mediump float;
#define Mouse_Rotation ( Max_Rotation - Max_Rotation * 2.0 * (iMouse.xy / iResolution.xy) )
#define RotationCenter vec3(0.000, 0.000, 0.000)
#define Max_Rotation 0.65
/*
A simple rotating effect of a textured plane in 3D space.
I'm basically using a ray for each pixel that intersects the plane
as drawn in a 2D view below.
Created By Jaap Boerhof (Dec. 2017)
Forked by yohjimane (Jul. 2023)
Positioning Positioning at a certain angle:
at zero degrees rotation:
|------plane-----| coords: __--|
\ / (-1.0..1.0, 0.0) __-- /
\ / __-- /
\ / __-- /
\ / \ _-- /
\ / |\ /
\ / \ /
\ / \ /
\ / \ /
\/ Camera \ /
position \ /
at (0.0, -1.0) \ /
\/
*/
uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D texture;
vec2 rotate(vec2 v, vec2 o, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * (v-o) + o;
}
vec3 rotateZ(vec3 v, vec3 o, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return vec3(m * (v.xy - o.xy) + o.xy, v.z);
}
vec2 TransformPlane(vec2 uv, vec3 center, float XRot, float YRot, float ZRot) {
// First Rotate around Y axis
vec2 RayDirection = vec2(uv.x, 0.0);
vec2 A1 = vec2(0.0, -1.0);
vec2 B1 = RayDirection - A1;
vec2 C1 = rotate(vec2(-1.0, 0.0), vec2(center.x, 0.0), YRot);
vec2 D1 = rotate(vec2(1.0, 0.0), vec2(center.x, 0.0), YRot) - C1;
// calculate intersection point
float u = ( (C1.y + 1.0) * D1.x - C1.x * D1.y ) / (D1.x*B1.y-D1.y*B1.x);
// position on the XY plane after Y-axis rotation
float sx = u * B1.x;
float sy = u * uv.y;
// Now Rotate around X axis
RayDirection = vec2(sy, 0.0);
vec2 B2 = RayDirection - A1;
vec2 C2 = rotate(vec2(-1.0, 0.0), vec2(center.y, 0.0), XRot);
vec2 D2 = rotate(vec2(1.0, 0.0), vec2(center.y, 0.0), XRot) - C2;
// calculate intersection point
float v = ( (C2.y + 1.0) * D2.x - C2.x * D2.y ) / (D2.x*B2.y-D2.y*B2.x);
// the position after x and y rotations
vec3 pos = vec3(v * sx, v * B2.x, 0.0 );
// Now rotate the position around Z axis
vec3 finalPos = rotateZ(pos, center, ZRot);
// final position on the 3D plane after Z-axis rotation
return finalPos.xy;
}
void main()
{
vec2 uv = -1.0 + 2.0 * gl_FragCoord.xy / u_resolution.xy;
float R_X = 0.;
float R_Y = u_time;
float R_Z = 0.; // Adjust the value for Z-axis rotation
vec3 MyCoords = vec3(TransformPlane(uv, RotationCenter, R_X, R_Y, R_Z), 0.0);
vec2 MyTexCoord = (MyCoords.xy + vec2(1.0)) / 2.0;
//vec4 image1 = texture2D(texture, MyTexCoord);
//vec4 image2 = texture(iChannel1, MyTexCoord);
//fragColor = mix(image1, image2, (sin(iTime * 0.5) + 1.0) / 2.0);
gl_FragColor = texture2D(texture, MyTexCoord);
}
The shader looks fine and seems to work on a sprite but i don't know if i can use it to spin a text.
I tried to use a rendertexture and make a sprite out of it but with this i have a black background on my text
sf::RenderTexture rendertexture;
rendertexture.create(SCREEN_WIDTH,SCREEN_HEIGHT);
rendertexture.clear();
rendertexture.draw(myText);
sf::Sprite sprite(rendertexture.getTexture());
//window.draw(myText,&testRotation); This doesnt work
window.draw(sprite,&testRotation);
This doesnt work, i dont know if i need to use deeper things as vertex and stuff like that.
Thanks for reading this and let me know if you need sample of code or smthing else