 # How to get distance for attenuation - LWJGL

I am creating a 3d lwjgl game and am trying to implement biomes, but I can’t think of a way to limit how far an area is tinted for that biome. I got how to tint it and am calculating the distance now, but I do not know how to. Does anyone here feel like telling me? Also could you explain what exactly the distance variable is as I only have a basic knowledge of it.

Code for calculating attenuation:

``````float biomeAttFactor = biomeAttenuation.x + (biomeAttenuation.y * biomeDistance) + (biomeAttenuation.z * biomeDistance * biomeDistance);
``````

Same way you get light attenutation:

attenuation = 1.0f / (constantAttenuation + dist_to_center * (linearAttenuation + dist_to_center * quadraticAttenuation));

I read that it was different than that, but other than that I am asking how to calculate the distance. Thanks though that will probably be helpful later With the 3D distance formula

``````
public static float distance3d(float ax, float ay, float az, float bx, float by, float bz) {
float deltax = bx - ax;
float deltay = by - ay;
float deltaz = bz - az;
return (float) Math.sqrt(deltax * deltax + deltay * deltay + deltaz * deltaz);
}

``````

So in your case you would do (tile_center.x, tile_center.y, tile_center.z, biome_center.x, biome_center.y, biome_center.z)

By looking at your way I think my engine is either outdated or just bad as I have to use a .txt file for the most part.

``````
#version 400 core

in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
in float visibility;

out vec4 out_Color;

uniform sampler2D backgroundTexture;
uniform sampler2D rTexture;
uniform sampler2D gTexture;
uniform sampler2D bTexture;
uniform sampler2D blendMap;

uniform vec3 lightColour;
uniform vec3 attenuation;
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;

uniform vec3 biomeColour;
uniform vec3 biomeAttenuation;
uniform vec3 biomePosition;

void main(void){

vec4 blendMapColour = texture(blendMap, pass_textureCoordinates);

float backTextureAmount = 1 - (blendMapColour.r + blendMapColour.g + blendMapColour.b);
vec2 tiledCoords = pass_textureCoordinates * 40.0;
vec4 backgroundTextureColour = texture(backgroundTexture, tiledCoords) * backTextureAmount;
vec4 rTextureColour = texture(rTexture, tiledCoords) * blendMapColour.r;
vec4 gTextureColour = texture(gTexture, tiledCoords) * blendMapColour.g;
vec4 bTextureColour = texture(bTexture, tiledCoords) * blendMapColour.b;

vec4 totalColour = backgroundTextureColour + rTextureColour + gTextureColour + bTextureColour;

vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);

vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);

float biomeDistance =length(biomePosition);
float biomeAttFactor = biomeAttenuation.x + (biomeAttenuation.y * biomeDistance) + (biomeAttenuation.z * biomeDistance * biomeDistance);

vec3 finalBiomeColour = biomeColour / biomeAttFactor;

for(int i = 0; i < 4; i++) {

float distance = length(toLightVector[i]);
float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);

vec3 unitLightVector = normalize(toLightVector[i]);

float nDotl = dot(unitNormal,unitLightVector);
float brightness = max(nDotl,0.0);

vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);

float specularFactor = dot(reflectedLightDirection , unitVectorToCamera);
specularFactor = max(specularFactor,0.0);
float dampedFactor = pow(specularFactor,shineDamper);
totalDiffuse = totalDiffuse + (brightness * lightColour[i])/attFactor;
totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColour[i])/attFactor;
}
totalDiffuse = max(totalDiffuse, 0.2);

out_Color =  vec4(totalDiffuse,1.0) * totalColour + vec4(totalSpecular,1.0);
out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);
out_Color = mix(vec4(finalBiomeColour, 1.0), out_Color, visibility);

}

``````

Im assuming that your length(biomePosition) gets you the magnitude of the position of the biome?
That shouldn’t be correct because that gives you the distance from (0, 0, 0) to your biome position. You want the distance between the fragment and the biome position so you should use distance3d

You seem to know a lot about things such as length() and distance3d() it seems. Do you know of anywhere I can easily find these things? I have been looking, but with no luck. Also if you do not could you show me how to use distance3d() as lwjgl does not recognize it as anything. Also no biomePosition is the center of the biome.

EDIT: When messing around with it I saw that when I got the length that it did increase in brightness the closer to 0,0 it got, but whenever I changed it to other coords the brightness stayed constant at all sides of the map (I changed the location to where the light would normally seem to reflect pure white)

If length() doesn’t work for biomeDistance why is it working just fine for regular distance?

A vector’s length, or magnitude, is the distance from the origin (0, 0, 0) to the vector.

For example, finding the length of (6, 10, 22) would result in sqrt(6 * 6 + 10 * 10 + 22 * 22)