EDIT-
MAYBE I FOUND THE REASON:
void main(void)
{
float i = 0;
while (i <= 1)
{
i + 1;
}
}
that simple 2D filter crashes the BGE
-EDIT
EDIT2-
oh, boy, that was cheap, the while loop somehow crashes the BGE, so I just replaced it with a for loop
Does anyone know why that is?
The âfixedâ code (it still isnât even close to perfect):
uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;
float resX = bgl_RenderedTextureWidth;
float resY = bgl_RenderedTextureHeight;
float currentX = (gl_TexCoord[0].st.x) * resX;
float currentY = (gl_TexCoord[0].st.y) * resY;
vec4 currentColor = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
float currentDepth = texture2D(bgl_DepthTexture, gl_TexCoord[0].st);
float divideBy = 1;
float skip = 1; // every value above 1 lowers the quality but speeds everything up (+ then you see a pattern if you do not make this value random)
float maxRad = 10;
float focus = 0.9412;
float blurAmount(float Depth) // not physically accurate (I guess)
{
float blur = abs(Depth / focus - 1) * maxRad;
return blur;
}
void main(void)
{
for (float helpY = currentY - maxRad; helpY <= currentY + maxRad; helpY += skip)
{
for (float helpX = currentX - maxRad; helpX <= currentX + maxRad; helpX += skip)
{ // the algorithm starts here
if (helpX != currentX && helpY != currentY && helpX > 0 && helpX < resX - 1 && helpY > 0 && helpY < resY - 1) //for preventing dark rims
{
float vecX = helpX / resX;
float vecY = helpY / resY;
float helpDepth = texture2D(bgl_DepthTexture, vec2(vecX, vecY)); //getting the depth and color of the "helpPixel"
if (currentDepth <= helpDepth) // if the current pixel is in front of the "helpPixel" then use the blur radius of the current pixel
{
float rad = blurAmount(currentDepth);
if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
{
vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
currentColor += helpColor;
divideBy += 1;
}
}
else // else use the blur radius of the "helpPixel"
{
float rad = blurAmount(helpDepth);
if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
{
vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
currentColor += helpColor;
divideBy += 1;
}
} // the algorithm ends here
}
}
}
currentColor /= divideBy;
gl_FragColor = currentColor;
}
-EDIT2
Last edit: i made an infinite loop and I didnât notice itâŚ
I wrote a depth of field shader that crashes the BGE when I want to run the game and I donât know why, I already proofread it several times but i canât find a mistake :spin: (notice: that is my first shader, so it might be that the script has a common mistake in it and Iâm just not aware of it):
uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;
float resX = bgl_RenderedTextureWidth;
float resY = bgl_RenderedTextureHeight;
float currentX = (gl_TexCoord[0].st.x) * resX;
float currentY = (gl_TexCoord[0].st.y) * resY;
vec4 currentColor = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
float currentDepth = texture2D(bgl_DepthTexture, gl_TexCoord[0].st);
float divideBy = 1;
float skip = 1; // every value above 1 lowers the quality but speeds everything up (+ then you see a pattern if you do not make this value random)
float maxRad = 10;
float focus = 0.9412;
float blurAmount(float Depth) // not physically accurate (I guess)
{
float blur = abs(Depth / focus - 1) * maxRad;
return blur;
}
void main(void)
{
float helpY = currentY - maxRad;
while (helpY <= currentY + maxRad)
{
float helpX = currentX - maxRad;
while (helpX <= currentX + maxRad)
{ // the algorithm starts here
if (helpX != currentX && helpY != currentY && helpX > 0 && helpX < resX - 1 && helpY > 0 && helpY < resY - 1) //for preventing dark rims
{
float vecX = helpX / resX;
float vecY = helpY / resY;
float helpDepth = texture2D(bgl_DepthTexture, vec2(vecX, vecY)); //getting the depth and color of the "helpPixel"
if (currentDepth <= helpDepth) // if the current pixel is in front of the "helpPixel" then use the blur radius of the current pixel
{
float rad = blurAmount(currentDepth);
if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
{
vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
currentColor += helpColor;
divideBy += 1;
}
}
else // else use the blur radius of the "helpPixel"
{
float rad = blurAmount(helpDepth);
if (sqrt((helpX - currentX)*(helpX - currentX) + (helpY - currentY)*(helpY - currentY)) <= rad)
{
vec4 helpColor = texture2D(bgl_RenderedTexture, vec2(vecX, vecY));
currentColor += helpColor;
divideBy += 1;
}
} // the algorithm ends here
}
helpX + skip;
}
helpY + skip;
}
currentColor /= divideBy;
gl_FragColor = currentColor;
}
Can you find the reason why it crashes the Game Engine? (notice: (maybe that is important) when i hit p or click on one of the start buttons i hear the music of my game for some time until it crashes, however it doesnât render even one single frame and my gpu gets used by 100% (so maybe this script has an infinite loop in it and i donât know it ))