浏览代码

updated hlsl files with new code and comments

/ShaderShadow
Dan 4 年前
当前提交
69e04488
共有 6 个文件被更改,包括 160 次插入287 次删除
  1. 168
      Assets/Shaders/Shadows/BlurredShadowPlane.shadergraph
  2. 52
      Assets/Shaders/Shadows/BlurredShadowPlaneLighting.hlsl
  3. 1
      Assets/Shaders/Shadows/Materials/ShadowMat.mat
  4. 34
      Assets/Shaders/Shadows/PlaneContactShadows.hlsl
  5. 190
      Assets/Shaders/Shadows/Prefabs/ShadowObject.prefab
  6. 2
      ProjectSettings/ProjectSettings.asset

168
Assets/Shaders/Shadows/BlurredShadowPlane.shadergraph
文件差异内容过多而无法显示
查看文件

52
Assets/Shaders/Shadows/BlurredShadowPlaneLighting.hlsl


#ifndef SHADERGRAPH_PREVIEW
real SampleShadowmapDirect(TEXTURE2D_SHADOW_PARAM(ShadowMap, sampler_ShadowMap), SamplerState PointClamp, float4 shadowCoord, ShadowSamplingData samplingData, half4 shadowParams, bool isPerspectiveProjection = true)
real SampleShadowmapDirect(TEXTURE2D_SHADOW_PARAM(ShadowMap, sampler_ShadowMap), SamplerState PointClamp, float4 shadowCoord, ShadowSamplingData samplingData, bool isPerspectiveProjection = true)
{
// Compiler will optimize this branch away as long as isPerspectiveProjection is known at compile time

real distance;
real shadowStrength = shadowParams.x;
// Instead of getting a value of 0 or 1 depending on a shadowmap comparison, we want to actually sample the shadowmap's values
// Shadow coords that fall out of the light frustum volume must always return attenuation 1.0
// TODO: We could use branch here to save some perf on some platforms.
return BEYOND_SHADOW_FAR(shadowCoord) ? 1.0 : distance;
return distance;
void MainLightBlurShadow_half(half RandSeed, half FadeTightness, SamplerState PointClamp, half BlurRadius, half3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
void MainLightBlurShadow_half(half rand, half fadeTightness, SamplerState pointClamp, half blurRadius, half3 worldPos, out half shadowAtten)
Direction = half3(0.5, 0.5, 0);
Color = 1;
DistanceAtten = 1;
ShadowAtten = 1;
shadowAtten = 1;
const int NUM_STEPS = 40;
float oneDivNumSteps = 1.0 / NUM_STEPS;
Direction = mainLight.direction;
Color = mainLight.color;
DistanceAtten = mainLight.distanceAttenuation;
ShadowAtten = 1;
shadowAtten = 1;
half4 shadowParams = GetMainLightShadowParams();
for (int i = 0; i < 40; i++)
for (int i = 0; i < NUM_STEPS; i++)
half3 newWorldPos = WorldPos + half3(sin(RandSeed + i), 0, cos(RandSeed + i)) * i * 0.025 * BlurRadius;
//Instead of sampling the position of the current pixel, sample a point that's a i steps away, in a semi-randomized direction
//the starting direction is randomized and then the direction is rotated by "i" radians every iteration
half3 newWorldPos = worldPos + half3(sin(rand + i), 0, cos(rand + i)) * i * oneDivNumSteps * blurRadius;
#if SHADOWS_SCREEN
half4 clipPos = TransformWorldToHClip(newWorldPos);
half4 shadowCoord = ComputeScreenPos(clipPos);

#else
half cascadeIndex = 0;
#endif
float shadowScale = pow(_MainLightWorldToShadow[cascadeIndex], 0.6) * 20; // Some "Magic Math" to try to make transitions between cascades smoother
//mainLight = GetMainLight(shadowCoord);
float shadowMapSample = SampleShadowmapDirect(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), PointClamp, shadowCoord, shadowSamplingData, shadowParams, false);
float shadowScale = pow(_MainLightWorldToShadow[cascadeIndex], 0.7) * 20; // Some "Magic Math" to try to make transitions between cascades smoother
float shadowMapSample = SampleShadowmapDirect(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), pointClamp, shadowCoord, shadowSamplingData, false);
float shadowCoordPos = shadowCoord.z / shadowScale;
float shadowCoordPos = shadowCoord.z / shadowScale; //Divide by shadowScale to get transitions more close-ish when using shadow cascades
float fade = max(0.01, ((shadowMapPos - shadowCoordPos) * FadeTightness));
float onedivfade = 1 / saturate(fade);
ShadowAtten -= step(shadowCoord.z, shadowMapSample) * 0.025 * clamp((41 - i * onedivfade), 0, onedivfade*2) * max(step(fade * 8, i+1), 0.3);
ShadowAtten = BEYOND_SHADOW_FAR(shadowCoord) ? 1.0 : saturate(ShadowAtten);
//blurAmount is a value between 0 and 1 expressing how much to blur
float blurAmount = max(0.01, ((shadowMapPos - shadowCoordPos) * fadeTightness));
float oneDivBlurAmount = 1 / saturate(blurAmount);
//0 or 1 depending on whether a shadow is being cast
float isInShadow = step(shadowCoord.z, shadowMapSample);
// reduce the influence of higher iterations (further out samples) if the sampled shadowmap value is close to our position
float tightenCloseShadows = clamp((NUM_STEPS - i * oneDivBlurAmount), 0, oneDivBlurAmount * 2);
// reduce the influence of lower iterations (closer in samples) if the sampled shadowmap value is far way from our position
float reduceInnerIterations = max(step(saturate(blurAmount) * NUM_STEPS / 4, i + 1), 0.3);
shadowAtten -= isInShadow * oneDivNumSteps * tightenCloseShadows * reduceInnerIterations;
// If the position is out of shadow distance, just return 1
shadowAtten = BEYOND_SHADOW_FAR(shadowCoord) ? 1.0 : saturate(shadowAtten);
}
#endif
}

1
Assets/Shaders/Shadows/Materials/ShadowMat.mat


m_Floats:
- BlurRadius: 0.4
- FadeTightness: 20
- MultiplyStrength: 0.4
- ShadowStrength: 0.5
- _AlphaClip: 0
- _Blend: 0

34
Assets/Shaders/Shadows/PlaneContactShadows.hlsl


void PlaneContactShadow_float(float rand, float3 Position, float dist, float depth, out float shadowAmount)
void PlaneContactShadow_float(float rand, float3 position, float dist, float depth, out float shadowAmount)
const int TOTAL_STEPS = 10;
float3 Direction = mainLight.direction;
float3 dirX = normalize(cross(Direction, float3(0, 1, 0)));
float3 dirY = cross(Direction, dirX);
for (int i = 0; i < 10; i++)
float3 direction = mainLight.direction;
float3 dirX = cross(direction, float3(0, 1, 0)); // Unit vector at right angle to light direction
dirX = length(dirX) == 0 ? float3(1, 0, 0) : normalize(dirX); //edge case incase the light direction is completely vertical
float3 dirY = cross(direction, dirX); // Unit vector at right angle to the light direction and dirX
for (int i = 0; i < TOTAL_STEPS; i++)
//float3 Normal = float3(cos(i * STAR_ANGLE + rand), 0, sin(i * STAR_ANGLE + rand));
float3 Normal = -Direction + (cos(i * STAR_ANGLE + rand) * dirX + sin(i * STAR_ANGLE + rand) * dirY) * 0.3;
float3 posStep = Position - Normal * (i) * 0.1 * dist;
//"normal" is a vector that rotates STAR_ANGLE radians around the light direction every iteration; it faces opposite the light direction and
//is at a sharp angle with the light direction. This is to create the blurring effect. The starting orientation of the vector
//is randomized using the "rand" input.
float3 normal = -direction + (cos(i * STAR_ANGLE + rand) * dirX + sin(i * STAR_ANGLE + rand) * dirY) * 0.3;
//by multiplying the step distance by the number of iterations (i) pixels that are further from the object will also recieve shadow, but less.
float3 posStep = position - normal * i * 0.1 * dist;
// Get the XY position of the projected position to be checked against the depth texture
newPos += float4(0.5, 0.5, 0, 0);
newPos += float4(0.5, 0.5, 0, 0);
// Prevent the depth texture values from bleeding from one side to the other
newPos = clamp(newPos, 0, 0.999);
//Get the view space depth value of our projected point
newPos = clamp(newPos, 0, 0.999);
shadowAmount += 0.02 / dist * (10 - i) * saturate(dist - abs(-newPosView.z - LinearEyeDepth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(newPos.xy), _ZBufferParams)));
// Compare the view space depth of our projected point to the depth texture value at the same XY location
float depthCompare = saturate(dist - abs(-newPosView.z - LinearEyeDepth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(newPos.xy), _ZBufferParams)));
//scale by 2 divided by the total number of steps squared, the total number of steps minus i, and scale inversely by dist
shadowAmount += 2.0 / (TOTAL_STEPS * TOTAL_STEPS) * (TOTAL_STEPS - i) * depthCompare / dist;
}
#endif
}

190
Assets/Shaders/Shadows/Prefabs/ShadowObject.prefab


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &111030322
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 111030326}
- component: {fileID: 111030325}
- component: {fileID: 111030324}
- component: {fileID: 111030323}
m_Layer: 0
m_Name: Cylinder (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &111030326
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 111030322}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0.6, y: 0.249, z: -0.024}
m_LocalScale: {x: 0.09539202, y: 0.09539202, z: 0.09539202}
m_Children: []
m_Father: {fileID: 8658607402519927358}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &111030325
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 111030322}
m_Mesh: {fileID: 10206, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &111030324
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 111030322}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!136 &111030323
CapsuleCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 111030322}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
m_Radius: 0.5000001
m_Height: 2
m_Direction: 1
m_Center: {x: 0.000000059604645, y: 0, z: -0.00000008940697}
--- !u!1 &1812338804
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1812338808}
- component: {fileID: 1812338807}
- component: {fileID: 1812338806}
- component: {fileID: 1812338805}
m_Layer: 0
m_Name: Cylinder
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1812338808
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1812338804}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0.4, y: 0.291, z: 0}
m_LocalScale: {x: 0.29247, y: 0.29247, z: 0.29247}
m_Children: []
m_Father: {fileID: 8658607402519927358}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &1812338807
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1812338804}
m_Mesh: {fileID: 10206, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &1812338806
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1812338804}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!136 &1812338805
CapsuleCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1812338804}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
m_Radius: 0.5000001
m_Height: 2
m_Direction: 1
m_Center: {x: 0.000000059604645, y: 0, z: -0.00000008940697}
--- !u!1 &4687476865244239166
GameObject:
m_ObjectHideFlags: 0

m_Children:
- {fileID: 1748037316647009009}
- {fileID: 4687476865244239298}
- {fileID: 1812338808}
- {fileID: 111030326}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

2
ProjectSettings/ProjectSettings.asset


bundleVersion: 0.1
preloadedAssets:
- {fileID: 11400000, guid: 6f7fbab671a5848ddae17e25cb330888, type: 2}
- {fileID: 4800000, guid: e7c77f6eaab324a819efdc13b8125a39, type: 3}
- {fileID: -7698313333363481667, guid: 5c86decb01f9d6b46a50ce486fba0042, type: 2}
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1

正在加载...
取消
保存