Skip to content

Commit

Permalink
refactor: adapter to webgl1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxudong committed Jul 17, 2024
1 parent 67fdc58 commit c8ac015
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
56 changes: 31 additions & 25 deletions packages/shaderlab/src/shaders/Light.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){
vec3 scene_DirectLightColor[SCENE_DIRECT_LIGHT_COUNT];
vec3 scene_DirectLightDirection[SCENE_DIRECT_LIGHT_COUNT];

DirectLight getDirectLight(int index){
DirectLight light;
light.color = scene_DirectLightColor[index];
light.direction = scene_DirectLightDirection[index];

return light;
}
#ifdef GRAPHICS_API_WEBGL2
DirectLight getDirectLight(int index){
DirectLight light;
light.color = scene_DirectLightColor[index];
light.direction = scene_DirectLightDirection[index];

return light;
}
#endif

#endif

Expand All @@ -59,14 +61,16 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){
vec3 scene_PointLightPosition[ SCENE_POINT_LIGHT_COUNT ];
float scene_PointLightDistance[ SCENE_POINT_LIGHT_COUNT ];

PointLight getPointLight(int index){
PointLight light;
light.color = scene_PointLightColor[index];
light.position = scene_PointLightPosition[index];
light.distance = scene_PointLightDistance[index];
#ifdef GRAPHICS_API_WEBGL2
PointLight getPointLight(int index){
PointLight light;
light.color = scene_PointLightColor[index];
light.position = scene_PointLightPosition[index];
light.distance = scene_PointLightDistance[index];

return light;
}
return light;
}
#endif

#endif

Expand All @@ -91,17 +95,19 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){
float scene_SpotLightAngleCos[ SCENE_SPOT_LIGHT_COUNT ];
float scene_SpotLightPenumbraCos[ SCENE_SPOT_LIGHT_COUNT ];

SpotLight getSpotLight(int index){
SpotLight light;
light.color = scene_SpotLightColor[index];
light.position = scene_SpotLightPosition[index];
light.direction = scene_SpotLightDirection[index];
light.distance = scene_SpotLightDistance[index];
light.angleCos = scene_SpotLightAngleCos[index];
light.penumbraCos = scene_SpotLightPenumbraCos[index];

return light;
}
#ifdef GRAPHICS_API_WEBGL2
SpotLight getSpotLight(int index){
SpotLight light;
light.color = scene_SpotLightColor[index];
light.position = scene_SpotLightPosition[index];
light.direction = scene_SpotLightDirection[index];
light.distance = scene_SpotLightDistance[index];
light.angleCos = scene_SpotLightAngleCos[index];
light.penumbraCos = scene_SpotLightPenumbraCos[index];

return light;
}
#endif


#endif
Expand Down
2 changes: 1 addition & 1 deletion packages/shaderlab/src/shaders/Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ VertexInputs getVertexInputs(Attributes attributes){
#endif


// BlendShape
// BlendShape
#ifdef RENDERER_HAS_BLENDSHAPE
calculateBlendShape(attributes, position
#ifdef RENDERER_HAS_NORMAL
Expand Down
30 changes: 27 additions & 3 deletions packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){
for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) {
// warning: use `continue` syntax may trigger flickering bug in safri 16.1.
if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){
DirectLight directionalLight = getDirectLight(i);
#ifdef GRAPHICS_API_WEBGL2
DirectLight directionalLight = getDirectLight(i);
#else
DirectLight directionalLight;
directionalLight.color = scene_DirectLightColor[i];
directionalLight.direction = scene_DirectLightDirection[i];
#endif

#ifdef SCENE_IS_CALCULATE_SHADOWS
if (i == 0) { // Sun light index is always 0
directionalLight.color *= shadowAttenuation;
Expand All @@ -141,7 +148,14 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){

for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) {
if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){
PointLight pointLight = getPointLight(i);
#ifdef GRAPHICS_API_WEBGL2
PointLight pointLight = getPointLight(i);
#else
PointLight pointLight;
pointLight.color = scene_PointLightColor[i];
pointLight.position = scene_PointLightPosition[i];
pointLight.distance = scene_PointLightDistance[i];
#endif
addPointDirectLightRadiance( pointLight, brdfData, color );
}
}
Expand All @@ -152,7 +166,17 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){

for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) {
if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){
SpotLight spotLight = getSpotLight(i);
#ifdef GRAPHICS_API_WEBGL2
SpotLight spotLight = getSpotLight(i);
#else
SpotLight spotLight;
spotLight.color = scene_SpotLightColor[i];
spotLight.position = scene_SpotLightPosition[i];
spotLight.direction = scene_SpotLightDirection[i];
spotLight.distance = scene_SpotLightDistance[i];
spotLight.angleCos = scene_SpotLightAngleCos[i];
spotLight.penumbraCos = scene_SpotLightPenumbraCos[i];
#endif
addSpotDirectLightRadiance( spotLight, brdfData, color );
}
}
Expand Down

0 comments on commit c8ac015

Please sign in to comment.