Skip to content

Commit

Permalink
Merge fee11c0 into 85b971d
Browse files Browse the repository at this point in the history
  • Loading branch information
Silverlan authored Oct 2, 2024
2 parents 85b971d + fee11c0 commit 2c25cea
Show file tree
Hide file tree
Showing 36 changed files with 846 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--[[
Copyright (C) 2024 Silverlan
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
]]

include("/shaders/toon/shader.lua")
70 changes: 70 additions & 0 deletions assets/addons/toon_shader/lua/shaders/toon/shader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
file, You can obtain one at http://mozilla.org/MPL/2.0/.
]]

local cvRampTexture = console.register_variable(
"toon_shader_ramp_texture",
udm.TYPE_STRING,
"toon/ramp_2band",
bit.bor(console.FLAG_BIT_ARCHIVE),
"The ramp texture to use for the toon shader."
)

console.add_change_callback("toon_shader_ramp_texture", function(old, new)
local sh = shader.get("toon")
if util.is_valid(sh) == false then
return
end
sh:GetWrapper():InitializeRampTexture()
end)

local Shader = util.register_class("shader.ToonShader", shader.BaseTexturedLit3D)

Shader.FragmentShader = "programs/scene/toon"
Expand All @@ -14,4 +30,58 @@ Shader.ShaderMaterial = "toon"
function Shader:InitializePipeline(pipelineInfo, pipelineIdx)
shader.BaseTexturedLit3D.InitializePipeline(self, pipelineInfo, pipelineIdx)
end
function Shader:InitializeRampTexture()
if self.m_rampDescriptorSet == nil then
return
end
prosper.wait_idle(true)

local textures = {
cvRampTexture:GetString(),
"toon/ramp_texture",
"white",
}
for i, texName in ipairs(textures) do
local tex = asset.load(texName, asset.TYPE_TEXTURE)
if tex ~= nil then
self.m_rampTexture = tex
self.m_rampDescriptorSet:SetBindingTexture(0, tex:GetVkTexture())
break
else
if i == #textures then
log.error("Failed to load ramp texture '" .. texName .. "' for toon shader!")
else
log.error(
"Failed to load ramp texture '"
.. texName
.. "' for toon shader! Falling back to '"
.. textures[i + 1]
.. "'..."
)
end
end
end
end
function Shader:InitializeShaderResources()
shader.BaseTexturedLit3D.InitializeShaderResources(self)
local rampDsInfo = shader.DescriptorSetInfo("RAMP", {
shader.DescriptorSetBinding(
"TEXTURE",
prosper.DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
prosper.SHADER_STAGE_FRAGMENT_BIT
),
})
self:AttachDescriptorSetInfo(rampDsInfo)
end
function Shader:OnInitializationComplete()
local setIdx = self:GetShader():FindDescriptorSetIndex("RAMP")
if setIdx == nil then
return
end
self.m_rampDescriptorSet = self:GetShader():CreateDescriptorSet(setIdx)
self:InitializeRampTexture()

local pcb = self:GetBindPcb()
pcb:RecordBindDescriptorSet(self.m_rampDescriptorSet, setIdx)
end
shader.register("toon", Shader)
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
},
{
$string name "normal_map"
},
{
$string name "ramp_texture"
$string default "error"
}
]
}
121 changes: 89 additions & 32 deletions assets/addons/toon_shader/shaders/programs/scene/toon.frag
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
#include "/common/light_source.glsl"
#include "/common/vertex_outputs/vertex_data.glsl"
#include "/common/inputs/textures/normal_map.glsl"
#include "/common/inputs/fs_lightmap.glsl"
#include "/common/inputs/fs_renderer.glsl"
#include "/common/pbr/lighting/light_spot.glsl"
#include "/common/pbr/lighting/light_point.glsl"
#include "/common/pbr/lighting/light_directional.glsl"
#include "/common/pixel_outputs/fs_bloom_color.glsl"
#include "/programs/scene/pbr/fs_config.glsl"
#include "/programs/scene/scene_push_constants.glsl"
Expand Down Expand Up @@ -45,15 +48,17 @@ struct LightInfo {
float NdotL; // The dot product of the normal and the light direction
};

layout(LAYOUT_ID(RAMP, TEXTURE)) uniform sampler2D u_rampTexture;
vec3 calc_specular_component(const ShadingInfo shadingInfo, const SpecularInfo specInfo, const LightInfo lightInfo)
{
vec3 halfVector = normalize(lightInfo.lightDirection + shadingInfo.viewDirection);
float NdotH = max(dot(shadingInfo.surfaceNormal, halfVector), 0.0);
float specularIntensity = pow(NdotH * lightInfo.lightIntensity, specInfo.glossiness * specInfo.glossiness);
float specularIntensity = pow(NdotH * lightInfo.lightIntensity, specInfo.glossiness *specInfo.glossiness);
float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
return specularIntensitySmooth * specInfo.specularColor;
}


vec3 calc_rim_lighting(const ShadingInfo shadingInfo, const LightInfo lightInfo, const RimLightingInfo rimInfo)
{
float rimDot = 1 - dot(shadingInfo.viewDirection, shadingInfo.surfaceNormal);
Expand All @@ -62,21 +67,17 @@ vec3 calc_rim_lighting(const ShadingInfo shadingInfo, const LightInfo lightInfo,
return rimIntensity * rimInfo.color;
}

void calc_toon_blinn_phong_lighting(const ShadingInfo shadingInfo, const SpecularInfo specInfo, LightSourceData lightData, const LightInfo lightInfo, const RimLightingInfo rimInfo, inout vec3 lightColor, inout vec3 specularColor, inout vec3 rimColor, uint lightIndex, bool enableShadows)
vec3 calc_light_color(float NdotL, vec3 lightColor, float lightIntensity, float shadowFactor)
{
float shadowFactor = 1.0;
if(CSPEC_ENABLE_DYNAMIC_SHADOWS == 1)
shadowFactor = enableShadows ? get_spot_light_shadow_factor(lightIndex, true) : 1.0;

vec2 uv = vec2(1 - (lightInfo.NdotL * 0.5 + 0.5), 0.5);
//vec4 tex = fetch_ramp_texture(vec2(0,0))
float lightIntensity = (lightInfo.NdotL * shadowFactor) > 0 ? 1 : 0;
//float lightIntensity = smoothstep(0, 0.01, lightInfo.NdotL * shadowFactor); // Two bands
vec3 light = lightIntensity * lightData.color.rgb * lightInfo.lightIntensity;
vec2 uv = vec2(1 - (NdotL * 0.5 + 0.5), 0.5);
vec4 rampCol = texture(u_rampTexture, uv);
return lightColor * lightIntensity *rampCol.rgb *shadowFactor;
}

// Specular
void calc_toon_blinn_phong_lighting(const ShadingInfo shadingInfo, const SpecularInfo specInfo, LightSourceData lightData, const LightInfo lightInfo, float shadowFactor, const RimLightingInfo rimInfo, inout vec3 lightColor, inout vec3 specularColor, inout vec3 rimColor, uint lightIndex, bool enableShadows)
{
vec3 light = calc_light_color(lightInfo.NdotL, lightData.color.rgb, lightInfo.lightIntensity, shadowFactor);
vec3 specular = calc_specular_component(shadingInfo, specInfo, lightInfo);

vec3 rim = calc_rim_lighting(shadingInfo, lightInfo, rimInfo);

lightColor += light.rgb;
Expand All @@ -93,6 +94,25 @@ void calc_toon_blinn_phong_lighting(const ShadingInfo shadingInfo, const Specula

const uint TOON_DEBUG_MODE = TOON_DEBUG_MODE_NONE;

LightInfo calc_light_info(float lightIntensity, vec3 normal, vec3 lightDir)
{
LightInfo lightInfo;
// Default light intensities are designed for PBR, we have to
// (subjectively) adjust using some factor.
lightInfo.lightIntensity = lightIntensity;

float NdotL = max(dot(normal, lightDir), 0.0);
lightInfo.NdotL = NdotL;
lightInfo.lightDirection = lightDir;
return lightInfo;
}

void calc_toon_blinn_phong_lighting(ShadingInfo shadingInfo, SpecularInfo specInfo, RimLightingInfo rimInfo, LightSourceData light, vec3 lightDir, float attenuation, float shadowFactor, vec3 normal, inout vec3 lightColor, inout vec3 specularColor, inout vec3 rimColor, uint lightIndex, bool enableShadows)
{
LightInfo lightInfo = calc_light_info((light.color.a / 5.0) *attenuation, normal, lightDir);
calc_toon_blinn_phong_lighting(shadingInfo, specInfo, light, lightInfo, shadowFactor, rimInfo, lightColor, specularColor, rimColor, lightIndex, enableShadows);
}

void main()
{
SpecularInfo specInfo;
Expand All @@ -105,7 +125,8 @@ void main()
rimInfo.color = get_mat_rim_color();

vec2 texCoords = fs_in.vert_uv;
vec4 color = fetch_albedo_map(texCoords, get_instance_color());
vec4 baseColor = fetch_albedo_map(texCoords, get_instance_color());
vec4 color = baseColor;

mat3 normalMatrix = transpose(inverse(mat3(fs_in.M)));
vec3 normal = get_normal_from_map(texCoords, get_mat_flags());
Expand All @@ -131,39 +152,47 @@ void main()
uint lightIndex = visibleLightTileIndicesBuffer.data[tileStartOffset + i].index;
LightSourceData light = get_light_source(lightIndex);
float attenuation = calc_spot_light_attenuation(light, fs_in.vert_pos_ws.xyz);

LightInfo lightInfo;
// Default light intensities are designed for PBR, we have to
// (subjectively) adjust using some factor.
lightInfo.lightIntensity = light.color.a / 5.0;

lightInfo.lightIntensity *= attenuation;
vec3 lightDir = normalize(light.position.xyz - fs_in.vert_pos_ws.xyz);
float NdotL = max(dot(normal, lightDir), 0.0);
lightInfo.NdotL = NdotL;
lightInfo.lightDirection = lightDir;

calc_toon_blinn_phong_lighting(shadingInfo, specInfo, light, lightInfo, rimInfo, totalLightColor, totalSpecularColor, totalRimColor, lightIndex, enableShadows);
float shadowFactor = 1.0;
if(CSPEC_ENABLE_DYNAMIC_SHADOWS == 1)
shadowFactor = enableShadows ? get_spot_light_shadow_factor(lightIndex, true) : 1.0;
calc_toon_blinn_phong_lighting(shadingInfo, specInfo, rimInfo, light, lightDir, attenuation, shadowFactor, normal, totalLightColor, totalSpecularColor, totalRimColor, lightIndex, enableShadows);
}
}
if(CSPEC_ENABLE_LIGHT_SOURCES_POINT == 1) {
for(uint i = SCENE_POINT_LIGHT_BUFFER_START; i < SCENE_POINT_LIGHT_BUFFER_END && visibleLightTileIndicesBuffer.data[tileStartOffset + i].index != -1; i++) {
//uint lightIndex = visibleLightTileIndicesBuffer.data[tileStartOffset +i].index;
//LightSourceData light = get_light_source(lightIndex);
//color += apply_point_light(light,lightIndex,materialInfo,normal,view,vertPos,enableShadows);
uint lightIndex = visibleLightTileIndicesBuffer.data[tileStartOffset + i].index;
LightSourceData light = get_light_source(lightIndex);
float attenuation = calc_point_light_attenuation(light, fs_in.vert_pos_ws.xyz);
vec3 lightDir = normalize(light.position.xyz - fs_in.vert_pos_ws.xyz);
float shadowFactor = 1.0;
if(CSPEC_ENABLE_DYNAMIC_SHADOWS == 1)
shadowFactor = enableShadows ? get_point_light_shadow_factor(lightIndex, true, fs_in.vert_pos_ws.xyz) : 1.0;
calc_toon_blinn_phong_lighting(shadingInfo, specInfo, rimInfo, light, lightDir, attenuation, shadowFactor, normal, totalLightColor, totalSpecularColor, totalRimColor, lightIndex, enableShadows);
}
}
if(CSPEC_ENABLE_LIGHT_SOURCES_DIRECTIONAL == 1) {
for(uint i = SCENE_DIRECTIONAL_LIGHT_BUFFER_START; i < SCENE_DIRECTIONAL_LIGHT_BUFFER_END && visibleLightTileIndicesBuffer.data[tileStartOffset + i].index != -1; i++) {
//uint lightIndex = visibleLightTileIndicesBuffer.data[tileStartOffset +i].index;
//LightSourceData light = get_light_source(lightIndex);
//color += apply_directional_light(light,lightIndex,materialInfo,normal,view,enableShadows);
uint lightIndex = visibleLightTileIndicesBuffer.data[tileStartOffset + i].index;
LightSourceData light = get_light_source(lightIndex);
float attenuation = 1.0;
vec3 lightDir = -light.direction.xyz;
float shadowFactor = 1.0;
if(CSPEC_ENABLE_DYNAMIC_SHADOWS == 1)
shadowFactor = enableShadows ? get_directional_light_shadow_factor(lightIndex) : 1.0;
calc_toon_blinn_phong_lighting(shadingInfo, specInfo, rimInfo, light, lightDir, attenuation, shadowFactor, normal, totalLightColor, totalSpecularColor, totalRimColor, lightIndex, enableShadows);
}
}

// TODO
vec3 ambientLightColor = vec3(0.4, 0.4, 0.4);

bool useLightmaps = false;
if(CSPEC_ENABLE_LIGHT_MAPS == 1) {
useLightmaps = is_light_map_enabled();
//if(useLightmaps)
// color = baseColor.rgb;
}
if(CSPEC_ENABLE_IBL == 1) {
//if(!useLightmaps)
/*MaterialInfo matInfo;
Expand All @@ -173,6 +202,34 @@ void main()
ambientLightColor = texture(u_brdfLUT, texCoords).rgb;//get_ibl_contribution(matInfo, normal, viewDir, get_reflection_probe_intensity()) *100;
*/
}
if(CSPEC_ENABLE_LIGHT_MAPS == 1) {
// TODO: Lightmap mode should be determined by specialization constant to avoid if-condition overhead
if(useLightmaps) {
vec4 colDirect = texture(u_lightMap, fs_in.vert_uv_lightmap.xy);
float exposure = get_lightmap_exposure_pow();
if(is_indirect_light_map_enabled()) {
vec3 colIndirect = texture(u_lightMapIndirect, fs_in.vert_uv_lightmap.xy).rgb;
if(is_directional_light_map_enabled()) {
vec3 dominantDir = texture(u_lightMapDominant, fs_in.vert_uv_lightmap.xy).rgb;
dominantDir = dominantDir * 2.0 - 1.0;
dominantDir = normalize(dominantDir);

LightInfo lightInfo = calc_light_info(1.0, normal, dominantDir);
vec3 light = calc_light_color(lightInfo.NdotL, colDirect.rgb, lightInfo.lightIntensity, 1.0);
vec3 specular = calc_specular_component(shadingInfo, specInfo, lightInfo);
vec3 rim = calc_rim_lighting(shadingInfo, lightInfo, rimInfo);

totalLightColor += light.rgb;
totalSpecularColor += specular;
totalRimColor += rim;
}
else
color.rgb = baseColor.rgb * ((colDirect.rgb + colIndirect.rgb) * exposure);
}
else
color.rgb += baseColor.rgb * (exposure * colDirect.rgb);
}
}

if(TOON_DEBUG_MODE == TOON_DEBUG_MODE_NONE)
color.rgb *= (ambientLightColor + totalLightColor + totalSpecularColor + totalRimColor);
Expand Down
1 change: 1 addition & 0 deletions assets/scripts/localization/en/texts/components.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ c_category_physics_fluid = "Fluid"
c_category_rendering = "Rendering"
c_category_rendering_bvh = "BVH"
c_category_rendering_camera = "Camera"
c_category_rendering_effects = "Effects"
c_category_rendering_lighting = "Lighting"
c_category_rendering_model = "Model"
c_category_rendering_post_processing = "Post-Processing"
Expand Down
15 changes: 15 additions & 0 deletions assets/shaders/common/pbr/lighting/light_point.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
#include "light_shared.glsl"
#include "/lighting/fs_lighting_point.glsl"

float calc_point_light_attenuation(vec3 pointToLight, LightSourceData light)
{
float distance = length(pointToLight);
pointToLight /= distance;
float rangeAttenuation = calc_light_falloff(distance, light.position.w);
rangeAttenuation = pow(rangeAttenuation, light.falloffExponent);
return rangeAttenuation;
}

float calc_point_light_attenuation(LightSourceData light, vec3 vertPos)
{
vec3 pointToLight = light.position.xyz - vertPos;
return calc_point_light_attenuation(pointToLight, light);
}

vec3 apply_point_light(LightSourceData light, uint lightIndex, MaterialInfo materialInfo, vec3 normal, vec3 view, vec3 vertPos, bool enableShadows)
{
vec3 pointToLight = light.position.xyz - vertPos;
Expand Down
Loading

0 comments on commit 2c25cea

Please sign in to comment.