File: lib_envlight.glsl

package info (click to toggle)
wings3d 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,404 kB
  • sloc: erlang: 132,169; ansic: 3,779; lisp: 1,434; sh: 805; makefile: 794; cpp: 244; xml: 50
file content (50 lines) | stat: -rw-r--r-- 1,508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Fragment shader for camera lighting
//
// Author: Dan Gudmundsson
//

#include "lib_base.glsl"

// Moved to lib_base.glsl
// #ifdef GL_ARB_shader_texture_lod
// #extension GL_ARB_shader_texture_lod : enable
// #endif

uniform sampler2D EnvBrdfMap;
uniform sampler2D EnvDiffMap;
uniform sampler2D EnvSpecMap;
uniform float bg_rotate;

vec2 vec2uv(vec3 vec)
{
  float u = atan(vec.x, vec.z);
  float v = acos(vec.y);
  u = 0.5f+0.5*u/M_PI;
  v = v/M_PI;
  // http://vcg.isti.cnr.it/~tarini/no-seams/
  // float u1 = fract(u+0.5)-0.5;
  // u = (fwidth(u)) < ((fwidth(u1)*2.1)) ?  u : u1;
  return vec2(u, v);
}

vec3 background_lighting(PBRInfo pbrInputs, vec3 N, vec3 reflection)
{
    vec3 refl = normalize(reflection);
    vec2 index = vec2uv(refl);
    index = vec2(index.x+bg_rotate, index.y);

    vec2 brdf = texture2D(EnvBrdfMap, vec2(pbrInputs.NdotV, pbrInputs.perceptualRoughness)).rg;
    vec3 difflight = SRGBtoLINEAR(texture2D(EnvDiffMap, index)).rgb;
    vec3 diffuse  = difflight * pbrInputs.diffuseColor;
    float mipCount = 10.0; // resolution of 2048x1024
    float lod = (pbrInputs.perceptualRoughness * mipCount);
#ifdef GL_ARB_shader_texture_lod
    vec3 speclight = SRGBtoLINEAR(texture2DLod(EnvSpecMap, index, lod)).rgb;
#else  // NO GL_ARB_shader_texture_lod
    vec3 speclight = SRGBtoLINEAR(texture2D(EnvSpecMap, index, lod)).rgb;
#endif // GL_ARB_shader_texture_lod

    vec3 specular = speclight * (pbrInputs.specularColor * brdf.x + brdf.y);
    return diffuse + specular;
}