Factorize uvmapping of celestial objects

This commit is contained in:
yannk 2022-04-13 13:33:55 +02:00
parent 3dd5d7edd8
commit 4e94507ac0

View file

@ -51,6 +51,18 @@ float hash(vec3 p) {
return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
}
// Function to create projection plane for texture at a specific position
vec2 place_object(vec3 position, vec3 eyedir){
//We define a local plane tangent to the skydome at the given position
//We work with everything normalized
vec3 n1 = normalize(cross(position,vec3(0.0,1.0,0.0))) ;
vec3 n2 = normalize(cross(position,n1)) ;
//We project EYEDIR on this plane with an approximate correction for projection
float x = dot(eyedir,n1) * 0.89 ;
float y = dot(eyedir,n2) * 0.89 ;
return vec2(x, y);
}
void sky() {
if (LIGHT0_ENABLED) {
float zenith_angle = clamp( dot(UP, normalize(LIGHT0_DIRECTION)), -1.0, 1.0 );
@ -110,49 +122,33 @@ void sky() {
float zabr_scale = radians(zabr_arc) ;
float stigi_scale = radians(stigi_arc) ;
// Calculate respective plane with UV to place celestial object textures
vec2 samayun_uv = place_object(samayun_position, EYEDIR) ;
vec2 zabr_uv = place_object(zabr_position, EYEDIR) ;
vec2 stigi_uv = place_object(stigi_position, EYEDIR) ;
// Adding Samayun
if (length(EYEDIR - normalize(samayun_position)) < samayun_scale / 2.0) { // we are in the area of the sky where samayun is placed
//We define a local plane tangent to the skydome at samayun_position
//We work with everything normalized
vec3 n1 = normalize(cross(samayun_position,vec3(0.0,1.0,0.0))) ;
vec3 n2 = normalize(cross(samayun_position,n1)) ;
//We project EYEDIR on this plane with an approximate correction for projection
float x = dot(EYEDIR,n1) * 0.89 ;
float y = dot(EYEDIR,n2) * 0.89 ;
// If zabr is nearer at this place, do nothing
if (length(EYEDIR - normalize(zabr_position)) < zabr_scale / 2.0 && length(zabr_position) < length(samayun_position)){
} else { // // Add samayun to the sky
COLOR += texture(samayun, vec2(x,y) / samayun_scale + vec2(0.5)).rgb * texture(samayun, vec2(x,y) / samayun_scale + vec2(0.5)).a;
COLOR += texture(samayun, samayun_uv / samayun_scale + vec2(0.5)).rgb * texture(samayun, samayun_uv / samayun_scale + vec2(0.5)).a;
}
}
// Adding zabr
if (length(EYEDIR - normalize(zabr_position)) < zabr_scale / 2.0) { // we are in the area of the sky where zabr is placed
//We define a local plane tangent to the skydome at zabr_position
//We work with everything normalized
vec3 z_n1 = normalize(cross(zabr_position,vec3(0.0,1.0,0.0))) ;
vec3 z_n2 = normalize(cross(zabr_position,z_n1)) ;
//We project EYEDIR on this plane with an approximate correction for projection
float z_x = dot(EYEDIR,z_n1) * 0.89 ;
float z_y = dot(EYEDIR,z_n2) * 0.89 ;
// If samayun is nearer at this place, do nothing
if (length(EYEDIR - normalize(samayun_position)) < samayun_scale / 2.0 && length(samayun_position) < length(zabr_position)){
} else { // Add zabr to the sky
COLOR += texture(zabr, vec2(z_x,z_y) / zabr_scale + vec2(0.5)).rgb * texture(zabr, vec2(z_x,z_y) / zabr_scale + vec2(0.5)).a;
COLOR += texture(zabr, zabr_uv / zabr_scale + vec2(0.5)).rgb * texture(zabr, zabr_uv / zabr_scale + vec2(0.5)).a;
}
}
// Adding stigi
if (length(EYEDIR - normalize(stigi_position)) < stigi_scale / 2.0) { // we are in the area of the sky where stigi is placed
//We define a local plane tangent to the skydome at stigi_position
//We work with everything normalized
vec3 s_n1 = normalize(cross(stigi_position,vec3(0.0,1.0,0.0))) ;
vec3 s_n2 = normalize(cross(stigi_position,s_n1)) ;
//We project EYEDIR on this plane with an approximate correction for projection
float s_x = dot(EYEDIR,s_n1) * 0.89 ;
float s_y = dot(EYEDIR,s_n2) * 0.89 ;
// If samayun is nearer at this place, do nothing
if (length(EYEDIR - normalize(samayun_position)) < samayun_scale / 2.0 && length(samayun_position) < length(stigi_position)){
} else { // Add stigi to the sky
COLOR += texture(stigi, vec2(s_x,s_y) / stigi_scale + vec2(0.5)).rgb * texture(stigi, vec2(s_x,s_y) / stigi_scale + vec2(0.5)).a;
COLOR += texture(stigi, stigi_uv / stigi_scale + vec2(0.5)).rgb * texture(stigi, stigi_uv / stigi_scale + vec2(0.5)).a;
}
}
}