fix: Fog masking

This commit is contained in:
Alexandre Coirier
2026-05-29 15:24:31 +02:00
parent 5611574882
commit aa82cacd5c
8 changed files with 1586 additions and 299 deletions
@@ -215,6 +215,9 @@
},
{
"m_Id": "aed770d7b8364459a7bfdfc34cbd2ab8"
},
{
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
}
],
"m_GroupDatas": [],
@@ -271,11 +274,39 @@
},
"m_InputSlot": {
"m_Node": {
"m_Id": "c5d342277052409f87283f2c60ccf3a1"
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
},
"m_SlotId": 13
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
},
"m_SlotId": 15
},
"m_InputSlot": {
"m_Node": {
"m_Id": "1035dda04278422b988336d6984e2045"
},
"m_SlotId": 0
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "3e46eb76f3e94a73912aecb313b485e5"
},
"m_SlotId": 0
},
"m_InputSlot": {
"m_Node": {
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
},
"m_SlotId": 18
}
},
{
"m_OutputSlot": {
"m_Node": {
@@ -383,7 +414,7 @@
},
"m_InputSlot": {
"m_Node": {
"m_Id": "c5d342277052409f87283f2c60ccf3a1"
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
},
"m_SlotId": 16
}
@@ -397,7 +428,7 @@
},
"m_InputSlot": {
"m_Node": {
"m_Id": "c5d342277052409f87283f2c60ccf3a1"
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
},
"m_SlotId": 17
}
@@ -481,7 +512,7 @@
},
"m_InputSlot": {
"m_Node": {
"m_Id": "c5d342277052409f87283f2c60ccf3a1"
"m_Id": "2f58e7c00b5c4814b4a3d8c13322694d"
},
"m_SlotId": 14
}
@@ -528,20 +559,6 @@
"m_SlotId": 0
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "c5d342277052409f87283f2c60ccf3a1"
},
"m_SlotId": 15
},
"m_InputSlot": {
"m_Node": {
"m_Id": "1035dda04278422b988336d6984e2045"
},
"m_SlotId": 0
}
},
{
"m_OutputSlot": {
"m_Node": {
@@ -836,6 +853,22 @@
"m_LiteralMode": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "04f9be2a155a4360a9351c1e284fa94b",
"m_Id": 15,
"m_DisplayName": "Alpha",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "Alpha",
"m_StageCapability": 3,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": [],
"m_LiteralMode": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
@@ -1191,9 +1224,9 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 596.0,
"y": 1187.0,
"width": 110.0,
"x": 845.0,
"y": 1202.0,
"width": 116.0,
"height": 34.0
}
},
@@ -1413,6 +1446,62 @@
}
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.CustomFunctionNode",
"m_ObjectId": "2f58e7c00b5c4814b4a3d8c13322694d",
"m_Group": {
"m_Id": ""
},
"m_Name": "Mask (Custom Function)",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 1002.0,
"y": 1119.0,
"width": 185.0,
"height": 190.0
}
},
"m_Slots": [
{
"m_Id": "c0c268cf854445c3a84028f37581f005"
},
{
"m_Id": "edda2fdb41e64d0d963b57e37eb02902"
},
{
"m_Id": "fafb345f71e44af2a725314204a0d6aa"
},
{
"m_Id": "945c2be648d84367930876227e5c006c"
},
{
"m_Id": "84e8c256f41d4dc5be903041907679c3"
},
{
"m_Id": "04f9be2a155a4360a9351c1e284fa94b"
}
],
"synonyms": [
"code",
"HLSL"
],
"m_Precision": 0,
"m_PreviewExpanded": false,
"m_DismissedVersion": 0,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_SourceType": 0,
"m_FunctionName": "Mask",
"m_FunctionSource": "2f64e213049952b44ac4bed6f62cafda",
"m_FunctionSourceUsePragmas": true,
"m_FunctionBody": "Enter function body here..."
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CategoryData",
@@ -1580,7 +1669,7 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 411.0,
"x": 410.0,
"y": 443.0,
"width": 206.0,
"height": 130.0
@@ -1803,22 +1892,6 @@
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "45ccaed6d4d547b090b274a4421169f6",
"m_Id": 15,
"m_DisplayName": "Alpha",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "Alpha",
"m_StageCapability": 3,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": [],
"m_LiteralMode": false
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Matrix4ShaderProperty",
@@ -2674,8 +2747,8 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 595.9999389648438,
"y": 1220.9998779296875,
"x": 845.0,
"y": 1236.0,
"width": 115.0,
"height": 34.0
}
@@ -2825,8 +2898,8 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 594.9999389648438,
"y": 1254.9998779296875,
"x": 844.0,
"y": 1270.0,
"width": 116.0,
"height": 34.0
}
@@ -2977,6 +3050,54 @@
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Matrix4MaterialSlot",
"m_ObjectId": "84e8c256f41d4dc5be903041907679c3",
"m_Id": 17,
"m_DisplayName": "Mask2",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Mask2",
"m_StageCapability": 3,
"m_Value": {
"e00": 1.0,
"e01": 0.0,
"e02": 0.0,
"e03": 0.0,
"e10": 0.0,
"e11": 1.0,
"e12": 0.0,
"e13": 0.0,
"e20": 0.0,
"e21": 0.0,
"e22": 1.0,
"e23": 0.0,
"e30": 0.0,
"e31": 0.0,
"e32": 0.0,
"e33": 1.0
},
"m_DefaultValue": {
"e00": 1.0,
"e01": 0.0,
"e02": 0.0,
"e03": 0.0,
"e10": 0.0,
"e11": 1.0,
"e12": 0.0,
"e13": 0.0,
"e20": 0.0,
"e21": 0.0,
"e22": 1.0,
"e23": 0.0,
"e30": 0.0,
"e31": 0.0,
"e32": 0.0,
"e33": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.MultiplyNode",
@@ -3034,22 +3155,6 @@
"m_DefaultValue": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "8892c2b112c14c028eddce5e899710b2",
"m_Id": 14,
"m_DisplayName": "MaskID",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "MaskID",
"m_StageCapability": 3,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": [],
"m_LiteralMode": false
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
@@ -3312,7 +3417,7 @@
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Matrix4MaterialSlot",
"m_ObjectId": "92ea1472d59d4857abd59195ace147a4",
"m_ObjectId": "945c2be648d84367930876227e5c006c",
"m_Id": 16,
"m_DisplayName": "Mask1",
"m_SlotType": 0,
@@ -3616,54 +3721,6 @@
"m_LiteralMode": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Matrix4MaterialSlot",
"m_ObjectId": "a363c8f5686c459792f7e2c53fb8dc8a",
"m_Id": 17,
"m_DisplayName": "Mask2",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Mask2",
"m_StageCapability": 3,
"m_Value": {
"e00": 1.0,
"e01": 0.0,
"e02": 0.0,
"e03": 0.0,
"e10": 0.0,
"e11": 1.0,
"e12": 0.0,
"e13": 0.0,
"e20": 0.0,
"e21": 0.0,
"e22": 1.0,
"e23": 0.0,
"e30": 0.0,
"e31": 0.0,
"e32": 0.0,
"e33": 1.0
},
"m_DefaultValue": {
"e00": 1.0,
"e01": 0.0,
"e02": 0.0,
"e03": 0.0,
"e10": 0.0,
"e11": 1.0,
"e12": 0.0,
"e13": 0.0,
"e20": 0.0,
"e21": 0.0,
"e22": 1.0,
"e23": 0.0,
"e30": 0.0,
"e31": 0.0,
"e32": 0.0,
"e33": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.PropertyNode",
@@ -3762,8 +3819,8 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 592.0,
"y": 1153.0,
"x": 841.0,
"y": 1168.0,
"width": 114.0,
"height": 34.0
}
@@ -3945,6 +4002,29 @@
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector3MaterialSlot",
"m_ObjectId": "c0c268cf854445c3a84028f37581f005",
"m_Id": 18,
"m_DisplayName": "New",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "New",
"m_StageCapability": 3,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"m_Labels": []
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Matrix4ShaderProperty",
@@ -4106,23 +4186,8 @@
{
"m_Id": "8c26652d47d64417a9ce7a93bb811466"
},
{
"m_Id": "8892c2b112c14c028eddce5e899710b2"
},
{
"m_Id": "fdc7026b3c2f43f9af9342c37132e6cc"
},
{
"m_Id": "92ea1472d59d4857abd59195ace147a4"
},
{
"m_Id": "a363c8f5686c459792f7e2c53fb8dc8a"
},
{
"m_Id": "8ead54ba7a8d487ea3f55e930101215f"
},
{
"m_Id": "45ccaed6d4d547b090b274a4421169f6"
}
],
"synonyms": [
@@ -4837,6 +4902,22 @@
"m_Space": 2
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "edda2fdb41e64d0d963b57e37eb02902",
"m_Id": 14,
"m_DisplayName": "MaskID",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "MaskID",
"m_StageCapability": 3,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": [],
"m_LiteralMode": false
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Matrix4ShaderProperty",
@@ -5072,34 +5153,10 @@
"m_ColorMode": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.PositionMaterialSlot",
"m_ObjectId": "fb84ed33198947c792183c161d398299",
"m_Id": 0,
"m_DisplayName": "Position",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Position",
"m_StageCapability": 1,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"m_Labels": [],
"m_Space": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Matrix4MaterialSlot",
"m_ObjectId": "fdc7026b3c2f43f9af9342c37132e6cc",
"m_ObjectId": "fafb345f71e44af2a725314204a0d6aa",
"m_Id": 13,
"m_DisplayName": "Mask0",
"m_SlotType": 0,
@@ -5144,6 +5201,30 @@
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.PositionMaterialSlot",
"m_ObjectId": "fb84ed33198947c792183c161d398299",
"m_Id": 0,
"m_DisplayName": "Position",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Position",
"m_StageCapability": 1,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"m_Labels": [],
"m_Space": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector3MaterialSlot",
@@ -220,80 +220,6 @@ float WorldDistort(float3 PositionWS, float3 NormalWS, float Angle, float Tripla
return stripes;
}
float ProjectionMask(
float3 PositionWS,
float3 CamPositionWS,
float3 RectPositionWS,
float3 RectNormalWS,
float3 RectUpWS,
float2 RectSize,
float Oval)
{
// Build the rectangle local frame
float3 N = normalize(RectNormalWS);
float3 U = normalize(RectUpWS);
float3 R = normalize(cross(N, U)); // Right axis
U = normalize(cross(R, N)); // Recompute Up orthogonally in case RectUpWS is not perfectly perpendicular to RectNormalWS
// Ray from camera toward the fragment
float3 rayDir = normalize(PositionWS - CamPositionWS);
// Ray-plane intersection
// Plane equation: dot(P - RectPositionWS, N) = 0
float denom = dot(rayDir, N);
// Ray is parallel to the plane or facing away — no hit
if (abs(denom) < 1e-6)
return 0;
float t = dot(RectPositionWS - CamPositionWS, N) / denom;
// Intersection must be in front of the camera (t > 0)
// and not beyond the fragment (t <= distance from camera to fragment)
float tFrag = dot(PositionWS - CamPositionWS, rayDir);
if (t < 0.0 || t > tFrag + 1e-4)
return 0;
// Compute the hit point on the plane
float3 hitPoint = CamPositionWS + rayDir * t;
// Project hit point into the rectangle local space
float3 localVec = hitPoint - RectPositionWS;
float localU = dot(localVec, R); // horizontal axis
float localV = dot(localVec, U); // vertical axis
float halfW = RectSize.x * 0.5;
float halfH = RectSize.y * 0.5;
// Corner radius: 0 = rectangle, min(halfW, halfH) = oval
float radius = saturate(Oval) * min(halfW, halfH);
// Rounded rectangle SDF
float2 uv = float2(localU, localV);
float2 ext = float2(halfW, halfH);
float2 q = abs(uv) - ext + radius;
float sdf = length(max(q, 0.0)) - radius;
return sdf <= 0.0 ? 1.0 : 0.0;
}
float SampleProjectionMask(float3 PositionWS, float4x4 M)
{
if (M[0].w == 0)
return 0;
return ProjectionMask(
PositionWS,
M[0].xyz, // CamPositionWS
M[1].xyz, // RectPositionWS
M[3].xyz, // RectNormalWS
M[2].xyz, // RectUpWS
float2(M[2].w, M[3].w), // RectSize
M[1].w // Shape
);
}
void EnkiBillal_float
(
float4 Color,
@@ -308,12 +234,7 @@ void EnkiBillal_float
float StripesWidth,
float StripesSmooth,
int Step,
float MaskID,
float4x4 Mask0,
float4x4 Mask1,
float4x4 Mask2,
out float3 Output,
out float Alpha
out float3 Output
)
{
float3 scaledPositionOS = PositionOS * ObjectScale;
@@ -384,38 +305,6 @@ void EnkiBillal_float
//Output = PerlinColor3D(PositionWS, 5);
//Output = smoothstep(.97, .93, ao);
// Mask
if (Mask2[0].w == 1)
{
float mask = SampleProjectionMask(PositionWS, Mask2);
Alpha = (MaskID <= 1) ? 1 - mask : mask;
}
else if (MaskID == 0)
{
Alpha = 1;
}
else
{
float Active = Mask0[0].w + Mask1[0].w; // + Mask2[0].w;
if (Active > 0)
{
float mask = saturate(
SampleProjectionMask(PositionWS, Mask0) +
SampleProjectionMask(PositionWS, Mask1) // +
//SampleProjectionMask(PositionWS, Mask2)
);
Alpha = (MaskID == 1) ? 1 - mask : mask;
}
else
{
Alpha = (MaskID == 1) ? 0 : 1;
}
}
}
#endif
@@ -0,0 +1,120 @@
#ifndef MASK_INCLUDED
#define MASK_INCLUDED
float ProjectionMask(
float3 PositionWS,
float3 CamPositionWS,
float3 RectPositionWS,
float3 RectNormalWS,
float3 RectUpWS,
float2 RectSize,
float Oval)
{
// Build the rectangle local frame
float3 N = normalize(RectNormalWS);
float3 U = normalize(RectUpWS);
float3 R = normalize(cross(N, U)); // Right axis
U = normalize(cross(R, N)); // Recompute Up orthogonally in case RectUpWS is not perfectly perpendicular to RectNormalWS
// Ray from camera toward the fragment
float3 rayDir = normalize(PositionWS - CamPositionWS);
// Ray-plane intersection
// Plane equation: dot(P - RectPositionWS, N) = 0
float denom = dot(rayDir, N);
// Ray is parallel to the plane or facing away — no hit
if (abs(denom) < 1e-6)
return 0;
float t = dot(RectPositionWS - CamPositionWS, N) / denom;
// Intersection must be in front of the camera (t > 0)
// and not beyond the fragment (t <= distance from camera to fragment)
float tFrag = dot(PositionWS - CamPositionWS, rayDir);
if (t < 0.0 || t > tFrag + 1e-4)
return 0;
// Compute the hit point on the plane
float3 hitPoint = CamPositionWS + rayDir * t;
// Project hit point into the rectangle local space
float3 localVec = hitPoint - RectPositionWS;
float localU = dot(localVec, R); // horizontal axis
float localV = dot(localVec, U); // vertical axis
float halfW = RectSize.x * 0.5;
float halfH = RectSize.y * 0.5;
// Corner radius: 0 = rectangle, min(halfW, halfH) = oval
float radius = saturate(Oval) * min(halfW, halfH);
// Rounded rectangle SDF
float2 uv = float2(localU, localV);
float2 ext = float2(halfW, halfH);
float2 q = abs(uv) - ext + radius;
float sdf = length(max(q, 0.0)) - radius;
return sdf <= 0.0 ? 1.0 : 0.0;
}
float SampleProjectionMask(float3 PositionWS, float4x4 M)
{
if (M[0].w == 0)
return 0;
return ProjectionMask(
PositionWS,
M[0].xyz, // CamPositionWS
M[1].xyz, // RectPositionWS
M[3].xyz, // RectNormalWS
M[2].xyz, // RectUpWS
float2(M[2].w, M[3].w), // RectSize
M[1].w // Shape
);
}
void Mask_float
(
float3 PositionWS,
float MaskID,
float4x4 Mask0,
float4x4 Mask1,
float4x4 Mask2,
out float Alpha
)
{
Alpha = 1;
if (Mask2[0].w == 1)
{
float mask = SampleProjectionMask(PositionWS, Mask2);
Alpha = (MaskID <= 1) ? 1 - mask : mask;
}
else if (MaskID == 0)
{
Alpha = 1;
}
else
{
float Active = Mask0[0].w + Mask1[0].w; // + Mask2[0].w;
if (Active > 0)
{
float mask = saturate(
SampleProjectionMask(PositionWS, Mask0) +
SampleProjectionMask(PositionWS, Mask1) // +
//SampleProjectionMask(PositionWS, Mask2)
);
Alpha = (MaskID == 1) ? 1 - mask : mask;
}
else
{
Alpha = (MaskID == 1) ? 0 : 1;
}
}
}
#endif
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 2f64e213049952b44ac4bed6f62cafda
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: