| 1 |
{ -*- buffer-read-only: t -*- } |
| 2 |
{ DON'T EDIT -- this file was automatically generated from "source/lighting_model_physical/add_light.glsl" } |
| 3 |
'/*' + LineEnding + |
| 4 |
' Copyright 2020-2022 Michalis Kamburelis and glTF-Sample-Viewer authors.' + LineEnding + |
| 5 |
'' + LineEnding + |
| 6 |
' This file is part of "Castle Game Engine".' + LineEnding + |
| 7 |
'' + LineEnding + |
| 8 |
' "Castle Game Engine" is free software; see the file COPYING.txt,' + LineEnding + |
| 9 |
' included in this distribution, for details about the copyright.' + LineEnding + |
| 10 |
'' + LineEnding + |
| 11 |
' "Castle Game Engine" is distributed in the hope that it will be useful,' + LineEnding + |
| 12 |
' but WITHOUT ANY WARRANTY; without even the implied warranty of' + LineEnding + |
| 13 |
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.' + LineEnding + |
| 14 |
'' + LineEnding + |
| 15 |
' ----------------------------------------------------------------------------' + LineEnding + |
| 16 |
'' + LineEnding + |
| 17 |
' Shader code used for adding light source contribution for PhysicalMaterial' + LineEnding + |
| 18 |
' (PBR lighting model).' + LineEnding + |
| 19 |
' This is used by both desktop OpenGL and OpenGLES.' + LineEnding + |
| 20 |
'' + LineEnding + |
| 21 |
' In the future this may be used in both Gouraud and Phong shading,' + LineEnding + |
| 22 |
' so it may go either in vertex or fragment shader.' + LineEnding + |
| 23 |
' For now: since PhysicalMaterial now forces Phong shading, this is only used in Phong shading.' + LineEnding + |
| 24 |
'*/' + LineEnding + |
| 25 |
'' + LineEnding + |
| 26 |
'/* Light source position (or direction, if not LIGHT<Light>_TYPE_POSITIONAL)' + LineEnding + |
| 27 |
' in eye coordinates. */' + LineEnding + |
| 28 |
'uniform vec3 castle_LightSource<Light>Position;' + LineEnding + |
| 29 |
'' + LineEnding + |
| 30 |
'/* SpotLight specific parameters */' + LineEnding + |
| 31 |
'#ifdef LIGHT<Light>_TYPE_SPOT' + LineEnding + |
| 32 |
'uniform vec3 castle_LightSource<Light>SpotDirection;' + LineEnding + |
| 33 |
'uniform float castle_LightSource<Light>SpotCosCutoff;' + LineEnding + |
| 34 |
'#ifdef LIGHT<Light>_HAS_BEAM_WIDTH' + LineEnding + |
| 35 |
'/* In radians. Note that this differs from gl_LightSource[<Light>].spotCutoff' + LineEnding + |
| 36 |
' that is in degrees. */' + LineEnding + |
| 37 |
'uniform float castle_LightSource<Light>SpotCutoff;' + LineEnding + |
| 38 |
'uniform float castle_LightSource<Light>BeamWidth;' + LineEnding + |
| 39 |
'#endif' + LineEnding + |
| 40 |
'#ifdef LIGHT<Light>_HAS_SPOT_EXPONENT' + LineEnding + |
| 41 |
'uniform float castle_LightSource<Light>SpotExponent;' + LineEnding + |
| 42 |
'#endif' + LineEnding + |
| 43 |
'#endif' + LineEnding + |
| 44 |
'' + LineEnding + |
| 45 |
'uniform vec3 castle_LightSource<Light>Color;' + LineEnding + |
| 46 |
'' + LineEnding + |
| 47 |
'#ifdef LIGHT<Light>_HAS_ATTENUATION' + LineEnding + |
| 48 |
'/* Attenuation: constant, linear, quadratic. */' + LineEnding + |
| 49 |
'uniform vec3 castle_LightSource<Light>Attenuation;' + LineEnding + |
| 50 |
'#endif' + LineEnding + |
| 51 |
'' + LineEnding + |
| 52 |
'#ifdef LIGHT<Light>_HAS_RADIUS' + LineEnding + |
| 53 |
'uniform float castle_LightSource<Light>Radius;' + LineEnding + |
| 54 |
'#endif' + LineEnding + |
| 55 |
'' + LineEnding + |
| 56 |
'/* EnvironmentLight parameters */' + LineEnding + |
| 57 |
'#ifdef LIGHT<Light>_TYPE_ENVIRONMENT' + LineEnding + |
| 58 |
'uniform samplerCube diffuseTexture;' + LineEnding + |
| 59 |
'uniform samplerCube specularTexture;' + LineEnding + |
| 60 |
'#endif' + LineEnding + |
| 61 |
'' + LineEnding + |
| 62 |
'// In case of OpenGLES, all shader code is glued, so this is already declared' + LineEnding + |
| 63 |
'#ifndef GL_ES' + LineEnding + |
| 64 |
'AngularInfo getAngularInfo(const in vec3 pointToLight, const in vec3 normal, const in vec3 view);' + LineEnding + |
| 65 |
'vec3 specularReflection(const in MaterialInfo materialInfo, const in AngularInfo angularInfo);' + LineEnding + |
| 66 |
'float visibilityOcclusion(const in MaterialInfo materialInfo, const in AngularInfo angularInfo);' + LineEnding + |
| 67 |
'float microfacetDistribution(const in MaterialInfo materialInfo, const in AngularInfo angularInfo);' + LineEnding + |
| 68 |
'vec3 diffuse(const in MaterialInfo materialInfo);' + LineEnding + |
| 69 |
'#endif' + LineEnding + |
| 70 |
'' + LineEnding + |
| 71 |
'/* Add light contribution.' + LineEnding + |
| 72 |
' Note: this never changes color.a.' + LineEnding + |
| 73 |
'*/' + LineEnding + |
| 74 |
'void PLUG_add_light(inout vec4 color,' + LineEnding + |
| 75 |
' const in vec4 vertex_eye,' + LineEnding + |
| 76 |
' const in vec3 normal_eye,' + LineEnding + |
| 77 |
' const in MaterialInfo material_info,' + LineEnding + |
| 78 |
' const in vec3 view)' + LineEnding + |
| 79 |
'{' + LineEnding + |
| 80 |
' vec3 light_dir;' + LineEnding + |
| 81 |
'' + LineEnding + |
| 82 |
'/* Calculate light_dir */' + LineEnding + |
| 83 |
'#ifdef LIGHT<Light>_TYPE_PUNCTUAL' + LineEnding + |
| 84 |
' #ifdef LIGHT<Light>_TYPE_POSITIONAL' + LineEnding + |
| 85 |
' light_dir = castle_LightSource<Light>Position - vec3(vertex_eye);' + LineEnding + |
| 86 |
' float distance_to_light = length(light_dir);' + LineEnding + |
| 87 |
' light_dir /= distance_to_light;' + LineEnding + |
| 88 |
' #else' + LineEnding + |
| 89 |
' light_dir = normalize(castle_LightSource<Light>Position);' + LineEnding + |
| 90 |
' #endif' + LineEnding + |
| 91 |
'#else' + LineEnding + |
| 92 |
' light_dir = normal_eye;' + LineEnding + |
| 93 |
'#endif' + LineEnding + |
| 94 |
'' + LineEnding + |
| 95 |
'#ifdef LIGHT<Light>_TYPE_SPOT' + LineEnding + |
| 96 |
' /* Check SpotCosCutoff first, as we want to add nothing' + LineEnding + |
| 97 |
' (not even ambient term) when were outside of spot light cone. */' + LineEnding + |
| 98 |
'' + LineEnding + |
| 99 |
' float spot_cos = dot(normalize(castle_LightSource<Light>SpotDirection), -light_dir);' + LineEnding + |
| 100 |
' if (spot_cos < castle_LightSource<Light>SpotCosCutoff)' + LineEnding + |
| 101 |
' return;' + LineEnding + |
| 102 |
'#endif' + LineEnding + |
| 103 |
'' + LineEnding + |
| 104 |
' float scale = 1.0;' + LineEnding + |
| 105 |
' /* PLUG: light_scale (scale, normal_eye, light_dir) */' + LineEnding + |
| 106 |
'' + LineEnding + |
| 107 |
'#ifdef LIGHT<Light>_TYPE_SPOT' + LineEnding + |
| 108 |
'#ifdef LIGHT<Light>_HAS_BEAM_WIDTH' + LineEnding + |
| 109 |
' /* calculate spot following VRML 2.0/X3D idea of beamWidth */' + LineEnding + |
| 110 |
' float cutOffAngle = castle_LightSource<Light>SpotCutoff;' + LineEnding + |
| 111 |
' if (castle_LightSource<Light>BeamWidth < cutOffAngle) {' + LineEnding + |
| 112 |
' scale *= clamp(' + LineEnding + |
| 113 |
' ( acos(spot_cos) - cutOffAngle) /' + LineEnding + |
| 114 |
' (castle_LightSource<Light>BeamWidth - cutOffAngle),' + LineEnding + |
| 115 |
' 0.0, 1.0);' + LineEnding + |
| 116 |
' }' + LineEnding + |
| 117 |
'#endif' + LineEnding + |
| 118 |
'' + LineEnding + |
| 119 |
'#ifdef LIGHT<Light>_HAS_SPOT_EXPONENT' + LineEnding + |
| 120 |
' /* calculate spot like fixed-function pipeline, using exponent */' + LineEnding + |
| 121 |
' scale *= pow(spot_cos, castle_LightSource<Light>SpotExponent);' + LineEnding + |
| 122 |
'#endif' + LineEnding + |
| 123 |
'#endif' + LineEnding + |
| 124 |
'' + LineEnding + |
| 125 |
'#ifdef LIGHT<Light>_HAS_ATTENUATION' + LineEnding + |
| 126 |
' scale /= max(1.0,' + LineEnding + |
| 127 |
' castle_LightSource<Light>Attenuation.x +' + LineEnding + |
| 128 |
' castle_LightSource<Light>Attenuation.y * distance_to_light +' + LineEnding + |
| 129 |
' castle_LightSource<Light>Attenuation.z * distance_to_light * distance_to_light);' + LineEnding + |
| 130 |
'#endif' + LineEnding + |
| 131 |
'' + LineEnding + |
| 132 |
'#ifdef LIGHT<Light>_HAS_RADIUS' + LineEnding + |
| 133 |
' if (distance_to_light >= castle_LightSource<Light>Radius)' + LineEnding + |
| 134 |
' scale = 0.0;' + LineEnding + |
| 135 |
'#endif' + LineEnding + |
| 136 |
'' + LineEnding + |
| 137 |
' /* Main code that calculates per-light-source contribution to the final color.' + LineEnding + |
| 138 |
' Was called "getPointShade" in glTF-Sample-Viewer.' + LineEnding + |
| 139 |
'' + LineEnding + |
| 140 |
' light_dir now must be the direction from current vertex to light source.' + LineEnding + |
| 141 |
' It is assumed to be already normalized.' + LineEnding + |
| 142 |
'' + LineEnding + |
| 143 |
' view is direction from current vertex to camera, in eye-space,' + LineEnding + |
| 144 |
' already normalized.' + LineEnding + |
| 145 |
' IOW, "normalize(- vertex_eye)" because camera position is zero in eye-space.' + LineEnding + |
| 146 |
' */' + LineEnding + |
| 147 |
' AngularInfo angularInfo = getAngularInfo(light_dir, normal_eye, view);' + LineEnding + |
| 148 |
'' + LineEnding + |
| 149 |
' if (angularInfo.NdotL > 0.0 || angularInfo.NdotV > 0.0)' + LineEnding + |
| 150 |
' {' + LineEnding + |
| 151 |
' // Calculate the shading terms for the microfacet specular shading model' + LineEnding + |
| 152 |
' vec3 F = specularReflection(material_info, angularInfo);' + LineEnding + |
| 153 |
' float Vis = visibilityOcclusion(material_info, angularInfo);' + LineEnding + |
| 154 |
' float D = microfacetDistribution(material_info, angularInfo);' + LineEnding + |
| 155 |
'' + LineEnding + |
| 156 |
' // Calculation of analytical lighting contribution' + LineEnding + |
| 157 |
' vec3 diffuseContrib = (1.0 - F) * diffuse(material_info);' + LineEnding + |
| 158 |
' vec3 specContrib = F * Vis * D;' + LineEnding + |
| 159 |
'' + LineEnding + |
| 160 |
'#ifdef LIGHT<Light>_TYPE_ENVIRONMENT' + LineEnding + |
| 161 |
' diffuseContrib *= textureCube(diffuseTexture, light_dir).rgb;' + LineEnding + |
| 162 |
' specContrib *= textureCube(specularTexture, light_dir).rgb;' + LineEnding + |
| 163 |
'#endif' + LineEnding + |
| 164 |
'' + LineEnding + |
| 165 |
' // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)' + LineEnding + |
| 166 |
' vec3 light_contribution = angularInfo.NdotL * (diffuseContrib + specContrib);' + LineEnding + |
| 167 |
'' + LineEnding + |
| 168 |
' color.rgb += castle_LightSource<Light>Color *' + LineEnding + |
| 169 |
' light_contribution *' + LineEnding + |
| 170 |
' scale;' + LineEnding + |
| 171 |
' }' + LineEnding + |
| 172 |
'}' + LineEnding + |
| 173 |
'' |