This repository has been archived on 2025-06-21. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
raylib-python-cffi/examples/models/resources/shaders/irradiance.fs
Richard Smith a703659c9d initial
2019-05-21 10:56:31 +01:00

58 lines
1.6 KiB
GLSL

/*******************************************************************************************
*
* rPBR [shader] - Irradiance cubemap fragment shader
*
* Copyright (c) 2017 Victor Fisac
*
**********************************************************************************************/
#version 330
# Input vertex attributes (from vertex shader)
in vec3 fragPos
# Input uniform values
uniform samplerCube environmentMap
# Constant values
const float PI = 3.14159265359f
# Output fragment color
out vec4 finalColor
void main()
[
# The sample direction equals the hemisphere's orientation
vec3 normal = normalize(fragPos)
vec3 irradiance = vec3(0.0)
vec3 up = vec3(0.0, 1.0, 0.0)
vec3 right = cross(up, normal)
up = cross(normal, right)
float sampleDelta = 0.025f
float nrSamples = 0.0
for (float phi = 0.0 phi < 2.0*PI phi += sampleDelta)
[
for (float theta = 0.0 theta < 0.5*PI theta += sampleDelta)
[
# Spherical to cartesian (in tangent space)
vec3 tangentSample = vec3(sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta))
# tangent space to world
vec3 sampleVec = tangentSample.x*right + tangentSample.y*up + tangentSample.z*normal
# Fetch color from environment cubemap
irradiance += texture(environmentMap, sampleVec).rgb*cos(theta)*sin(theta)
nrSamples++
]
]
# Calculate irradiance average value from samples
irradiance = PI*irradiance*(1.0/float(nrSamples))
# Calculate final fragment color
finalColor = vec4(irradiance, 1.0)
]