mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-12-11 00:31:28 -05:00
122 lines
4.2 KiB
Text
122 lines
4.2 KiB
Text
|
// stripped down version of Nicholas' Static Sky
|
||
|
// hybrid lighting shader that exposed vector_insert_todo
|
||
|
// bug
|
||
|
|
||
|
vec4 xll_tex2Dlod(sampler2D s, vec4 coord) {
|
||
|
return texture2DLod( s, coord.xy, coord.w);
|
||
|
}
|
||
|
mat3 xll_constructMat3_mf4x4( mat4 m) {
|
||
|
return mat3( vec3( m[0]), vec3( m[1]), vec3( m[2]));
|
||
|
}
|
||
|
struct Lamp {
|
||
|
vec4 posRange;
|
||
|
vec4 colorImp;
|
||
|
};
|
||
|
struct v2f {
|
||
|
vec4 pos;
|
||
|
vec3 diffuse;
|
||
|
};
|
||
|
struct HybridAppData {
|
||
|
vec4 vertex;
|
||
|
vec4 tangent;
|
||
|
vec3 normal;
|
||
|
vec4 texcoord;
|
||
|
vec4 bakedCol;
|
||
|
vec2 bakedDir;
|
||
|
};
|
||
|
uniform vec3 _WorldSpaceCameraPos;
|
||
|
uniform vec4 _ProjectionParams;
|
||
|
uniform vec4 _ScreenParams;
|
||
|
uniform vec4 _ZBufferParams;
|
||
|
uniform vec4 _WorldSpaceLightPos0;
|
||
|
|
||
|
uniform mat4 _Object2World;
|
||
|
uniform mat4 _World2Object;
|
||
|
|
||
|
|
||
|
|
||
|
uniform float _RenderExposure;
|
||
|
uniform vec2 _DynLampInfo_bufferSize;
|
||
|
uniform sampler2D _DynLampInfo;
|
||
|
uniform float LightVertOffset;
|
||
|
uniform sampler2D LightVertTexture;
|
||
|
uniform vec4 LightVertTextureSize;
|
||
|
void CalcDynamicLight( in vec3 worldVert, in vec3 worldNorm, in Lamp lamp[4], inout vec3 hybridDir, inout vec3 hybridCol ) {
|
||
|
vec4 atten;
|
||
|
int i = 0;
|
||
|
for ( ; (i < 4); (i++)) {
|
||
|
|
||
|
vec3 lightToVert = (lamp[i].posRange.xyz - worldVert);
|
||
|
float lengthSq = dot( lightToVert, lightToVert);
|
||
|
vec3 lightToVertNorm = (lightToVert * inversesqrt(lengthSq));
|
||
|
|
||
|
atten[i] = (lengthSq * lamp[i].posRange.w);
|
||
|
float nDotL = dot( lightToVertNorm, worldNorm);
|
||
|
float weight = (atten[i] * nDotL);
|
||
|
}
|
||
|
|
||
|
int j = 0;
|
||
|
for ( ; (j < 4); (j++)) {
|
||
|
hybridCol += (lamp[j].colorImp.xyz * atten[j]);
|
||
|
}
|
||
|
}
|
||
|
void LoadBakedLight( out vec3 hybridCol, out vec3 hybridDir ) {
|
||
|
}
|
||
|
vec4 DoSampleGPUBuffer( in sampler2D buffer, in vec2 coord, in vec2 dimensions ) {
|
||
|
return xll_tex2Dlod( buffer, vec4( (coord / dimensions), 0.0, 0.0));
|
||
|
}
|
||
|
void ReadLightArray3( in ivec4 lightIdx, out Lamp l0, out Lamp l1, out Lamp l2, out Lamp l3 ) {
|
||
|
|
||
|
l0.posRange = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.x), 1.0), _DynLampInfo_bufferSize);
|
||
|
l0.colorImp = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.x), 2.0), _DynLampInfo_bufferSize);
|
||
|
l1.posRange = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.y), 1.0), _DynLampInfo_bufferSize);
|
||
|
|
||
|
l1.colorImp = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.y), 2.0), _DynLampInfo_bufferSize);
|
||
|
l2.posRange = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.z), 1.0), _DynLampInfo_bufferSize);
|
||
|
l2.colorImp = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.z), 2.0), _DynLampInfo_bufferSize);
|
||
|
|
||
|
l3.posRange = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.w), 1.0), _DynLampInfo_bufferSize);
|
||
|
l3.colorImp = DoSampleGPUBuffer( _DynLampInfo, vec2( float(lightIdx.w), 2.0), _DynLampInfo_bufferSize);
|
||
|
}
|
||
|
void DoCalcHybridLight2( in vec3 worldVert, in vec3 worldNorm, out vec3 hybridCol, in vec4 bakedColor, in vec2 bakedDir ) {
|
||
|
vec3 hybridDir;
|
||
|
LoadBakedLight( hybridCol, hybridDir);
|
||
|
|
||
|
ivec4 lightIdx = ivec4( int(worldVert.x), int(worldVert.y), int(worldVert.z), int((-worldVert.x)));
|
||
|
Lamp l[4];
|
||
|
ReadLightArray3( lightIdx, l[0], l[1], l[2], l[3]);
|
||
|
|
||
|
CalcDynamicLight( worldVert, worldNorm, l, hybridDir, hybridCol);
|
||
|
}
|
||
|
vec3 CalcDiffuseHybridLight( in vec3 worldVert, in vec3 worldNorm, in vec4 bakedColor, in vec2 bakedDir ) {
|
||
|
vec3 hybridCol;
|
||
|
DoCalcHybridLight2( worldVert, worldNorm, hybridCol, bakedColor, bakedDir);
|
||
|
|
||
|
return hybridCol;
|
||
|
}
|
||
|
v2f vert( in HybridAppData v ) {
|
||
|
v2f o;
|
||
|
|
||
|
vec3 worldVert = (_Object2World * v.vertex).xyz;
|
||
|
vec3 worldNorm = normalize((xll_constructMat3_mf4x4( _Object2World) * v.normal.xyz));
|
||
|
o.diffuse = CalcDiffuseHybridLight( worldVert, worldNorm, v.bakedCol, v.bakedDir);
|
||
|
|
||
|
o.pos = (gl_ModelViewProjectionMatrix * v.vertex);
|
||
|
return o;
|
||
|
}
|
||
|
attribute vec4 TANGENT;
|
||
|
varying vec3 xlv_TEXCOORD2;
|
||
|
void main() {
|
||
|
v2f xl_retval;
|
||
|
HybridAppData xlt_v;
|
||
|
xlt_v.vertex = vec4(gl_Vertex);
|
||
|
xlt_v.tangent = vec4(TANGENT);
|
||
|
xlt_v.normal = vec3(gl_Normal);
|
||
|
xlt_v.texcoord = vec4(gl_MultiTexCoord0);
|
||
|
xlt_v.bakedCol = vec4(gl_Color);
|
||
|
xlt_v.bakedDir = vec2(gl_MultiTexCoord1);
|
||
|
xl_retval = vert( xlt_v);
|
||
|
gl_Position = vec4(xl_retval.pos);
|
||
|
xlv_TEXCOORD2 = vec3(xl_retval.diffuse);
|
||
|
}
|