Skip to content

Commit

Permalink
UPBGE: Implement Sky Texture for Environment Lighting
Browse files Browse the repository at this point in the history
	This is a basic IBL (Image-Based Lighting) implementation
	to allow using textures as a light source for the environment.

	However, shadows don't get influenced by the environment color/texture!
	We must fix this for Sky Color too.
  • Loading branch information
DCubix committed Aug 19, 2017
1 parent 47aa533 commit 6c8598b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
18 changes: 16 additions & 2 deletions source/blender/gpu/intern/gpu_material.c
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ void GPU_shaderesult_set(GPUShadeInput *shi, GPUShadeResult *shr)
if (((world->ao_env_energy != 0.0f) && (GPU_link_changed(shi->amb) || ma->amb != 0.0f) &&
(GPU_link_changed(shi->refl) || ma->ref != 0.0f)) || !(ma->constflag & MA_CONSTANT_WORLD))
{
if (world->aocolor != WO_AOPLAIN) {
if (world->aocolor == WO_AOSKYCOL) {
if (!(is_zero_v3(&world->horr) & is_zero_v3(&world->zenr)) || !(ma->constflag & MA_CONSTANT_WORLD)) {
GPUNodeLink *fcol, *f;
GPU_link(mat, "math_multiply", shi->amb, shi->refl, &f);
Expand All @@ -1998,7 +1998,21 @@ void GPU_shaderesult_set(GPUShadeInput *shi, GPUShadeResult *shr)
GPU_link(mat, "env_apply", shr->combined,
GPU_select_uniform(GPUWorld.horicol, GPU_DYNAMIC_HORIZON_COLOR, NULL, ma),
GPU_select_uniform(GPUWorld.zencol, GPU_DYNAMIC_ZENITH_COLOR, NULL, ma), fcol,
GPU_builtin(GPU_VIEW_MATRIX), shi->vn, &shr->combined);
GPU_builtin(GPU_VIEW_MATRIX), shi->vn, &shr->combined);
}
}
else if (world->aocolor == WO_AOSKYTEX) {
if (world->mtex && world->mtex[0] && world->mtex[0]->tex && world->mtex[0]->tex->ima) {
GPUNodeLink *fcol, *f;
Tex* tex = world->mtex[0]->tex;
GPU_link(mat, "math_multiply", shi->amb, shi->refl, &f);
GPU_link(mat, "math_multiply", f, GPU_select_uniform(&GPUWorld.envlightenergy, GPU_DYNAMIC_ENVLIGHT_ENERGY, NULL, ma), &f);
GPU_link(mat, "shade_mul_value", f, shi->rgb, &fcol);
GPU_link(mat, "env_apply_tex", shr->combined, fcol,
GPU_cube_map(tex->ima, &tex->iuser, false),
GPU_builtin(GPU_VIEW_NORMAL),
GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
&shr->combined);
}
}
else {
Expand Down
7 changes: 7 additions & 0 deletions source/blender/gpu/shaders/gpu_shader_material.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,13 @@ void env_apply(vec4 col, vec3 hor, vec3 zen, vec4 f, mat4 vm, vec3 vn, out vec4
outcol = col + f * vec4(mix(hor, zen, skyfac), 0);
}

void env_apply_tex(vec4 col, vec4 f, samplerCube wtex, vec3 vn, mat4 ivm, out vec4 outcol)
{
vec3 ndir = normalize(ivm * vec4(vn, 0.0)).xyz;
vec4 cubeD = textureCubeLod(wtex, ndir, 9.0);
outcol = col + f * cubeD * (1.0 / 3.141592654);
}

void shade_maddf(vec4 col, float f, vec4 col1, out vec4 outcol)
{
outcol = col + f * col1;
Expand Down
2 changes: 1 addition & 1 deletion source/tools
Submodule tools updated from b11375 to 4ace84

0 comments on commit 6c8598b

Please sign in to comment.