├── ProjectSettings ├── ProjectVersion.txt ├── TagManager.asset ├── TimeManager.asset ├── AudioManager.asset ├── EditorSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── DynamicsManager.asset ├── GraphicsSettings.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── ClusterInputManager.asset ├── EditorBuildSettings.asset ├── Physics2DSettings.asset └── UnityConnectSettings.asset ├── Assets ├── sdfshadowdemo │ ├── sdfdemo.unity │ ├── Unlit_SdfShadow.mat │ ├── Materials │ │ ├── defaultMat.mat │ │ └── defaultMat.mat.meta │ ├── sdfdemo.unity.meta │ ├── Materials.meta │ ├── SdfDepth.shader.meta │ ├── SdfShadow.shader.meta │ ├── Unlit_SdfShadow.mat.meta │ ├── calMinDistance.compute.meta │ ├── SdfBaker.cs.meta │ ├── calMinDistance.compute │ ├── SdfShadow.shader │ ├── sdfwall.obj.meta │ ├── SdfDepth.shader │ └── SdfBaker.cs └── sdfshadowdemo.meta ├── sdfshadow.sln └── Assembly-CSharp.csproj /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.6.5f1 2 | -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/sdfdemo.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/Assets/sdfshadowdemo/sdfdemo.unity -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/Unlit_SdfShadow.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/Assets/sdfshadowdemo/Unlit_SdfShadow.mat -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/Materials/defaultMat.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackie2009/sdfshadow/HEAD/Assets/sdfshadowdemo/Materials/defaultMat.mat -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/sdfdemo.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2a58c134ad6352543b4306b4d6af1db1 3 | timeCreated: 1622018584 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5228296373d82ce428937523ae42e185 3 | folderAsset: yes 4 | timeCreated: 1622547975 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f2c12cad3b3b9cb4397aa6441687201b 3 | folderAsset: yes 4 | timeCreated: 1622019072 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/SdfDepth.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ce19e1d3760539447b892d7bc9719af5 3 | timeCreated: 1622021731 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/SdfShadow.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 49db207582dac1447b1e631a7bccad7b 3 | timeCreated: 1622021731 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/Unlit_SdfShadow.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6b2e197a75e17f947940a84caab0d2f8 3 | timeCreated: 1622514635 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | mainObjectFileID: 2100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/calMinDistance.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 84575433510e49547b168a6216c557e9 3 | timeCreated: 1622182342 4 | licenseType: Pro 5 | ComputeShaderImporter: 6 | currentAPIMask: 4 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/Materials/defaultMat.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d3cc64ee83d97b4439e1ace429192fcd 3 | timeCreated: 1622019072 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | mainObjectFileID: 2100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/SdfBaker.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 018c5819b65faa2439e3fced1b2fdf0c 3 | timeCreated: 1622461834 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: 8 | - tempDepth: {instanceID: 0} 9 | - testTex: {instanceID: 0} 10 | - rt: {instanceID: 0} 11 | - renderShader: {fileID: 4800000, guid: ce19e1d3760539447b892d7bc9719af5, type: 3} 12 | - calMinDistance: {instanceID: 0} 13 | - sdfTex: {instanceID: 0} 14 | executionOrder: 0 15 | icon: {instanceID: 0} 16 | userData: 17 | assetBundleName: 18 | assetBundleVariant: 19 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/calMinDistance.compute: -------------------------------------------------------------------------------- 1 | // Each #kernel tells which function to compile; you can have many kernels 2 | #pragma kernel CSMain 3 | 4 | // Create a RenderTexture with enableRandomWrite flag and set it 5 | 6 | 7 | Texture2DArray depthTex; 8 | 9 | RWStructuredBuffer rstBuff; 10 | [numthreads(1,1,1)] 11 | void CSMain (uint3 id : SV_DispatchThreadID) 12 | { 13 | float minDis = 10000; 14 | bool inside = false; 15 | for (int i = 0; i < 32; i++) 16 | { 17 | for (int j = 0; j < 32; j++) 18 | { 19 | for (int k = 0; k < 6; k++) 20 | { 21 | float result = depthTex[uint3(i,j,k)].r; 22 | if (result != 0&&minDis> abs(result)) { 23 | minDis = abs(result); 24 | inside = result < 0; 25 | } 26 | } 27 | } 28 | } 29 | rstBuff[0] = minDis* (inside?-1:1); 30 | } 31 | -------------------------------------------------------------------------------- /sdfshadow.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sdfshadow", "Assembly-CSharp.csproj", "{60267BC2-96D2-EB75-93BF-5B0874C950B6}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {60267BC2-96D2-EB75-93BF-5B0874C950B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {60267BC2-96D2-EB75-93BF-5B0874C950B6}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {60267BC2-96D2-EB75-93BF-5B0874C950B6}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {60267BC2-96D2-EB75-93BF-5B0874C950B6}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | GlobalSection(MonoDevelopProperties) = preSolution 21 | StartupItem = Assembly-CSharp.csproj 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/SdfShadow.shader: -------------------------------------------------------------------------------- 1 | Shader "Unlit/SdfShadow" 2 | { 3 | Properties 4 | { 5 | 6 | } 7 | SubShader 8 | { 9 | Tags { "RenderType"="Opaque" } 10 | LOD 100 11 | 12 | Pass 13 | { 14 | cull off 15 | CGPROGRAM 16 | #pragma vertex vert 17 | #pragma fragment frag 18 | 19 | 20 | #include "UnityCG.cginc" 21 | 22 | struct appdata 23 | { 24 | float4 vertex : POSITION; 25 | 26 | }; 27 | 28 | struct v2f 29 | { 30 | float3 wpos : TEXCOORD0; 31 | 32 | float4 vertex : SV_POSITION; 33 | }; 34 | 35 | sampler3D _TestSdfTex; 36 | sampler3D _TestSdfTreeTex; 37 | 38 | 39 | v2f vert (appdata v) 40 | { 41 | v2f o; 42 | o.vertex = UnityObjectToClipPos(v.vertex); 43 | o.wpos = mul(unity_ObjectToWorld, v.vertex); 44 | 45 | 46 | return o; 47 | } 48 | half getSdf(float3 wpos) { 49 | half3 uv = wpos.xzy / half3(100, 100, 20); 50 | uv.xy += 0.5; 51 | if (max(max(uv.x, uv.y), uv.z) > 1) return 10; 52 | return tex3D(_TestSdfTex, saturate(uv)).r; 53 | } 54 | 55 | fixed4 frag(v2f i) : SV_Target 56 | { 57 | half sdf = 1000; 58 | half3 ray = normalize(_WorldSpaceLightPos0.xyz);// +half3(sin(_Time.x * 10000), cos(_Time.x * 13001), cos(_Time.x * 10101)) * 0.01); 59 | 60 | 61 | float3 nextWpos = i.wpos; 62 | float dis = 1; 63 | float csdf = 1.5;//表面1.5米开始避免自我遮挡 64 | for (int k = 0; k < 20; k++) 65 | { 66 | nextWpos += ray * abs(csdf); 67 | 68 | 69 | csdf = getSdf(nextWpos); 70 | sdf=min(sdf, csdf) ; 71 | 72 | } 73 | 74 | 75 | return (saturate(sdf)+0.5)/1.5; 76 | 77 | 78 | } 79 | ENDCG 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/sdfwall.obj.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5b0590d297a18d940bf06f295e387db7 3 | timeCreated: 1622019072 4 | licenseType: Pro 5 | ModelImporter: 6 | serializedVersion: 19 7 | fileIDToRecycleName: 8 | 100000: default 9 | 100002: //RootNode 10 | 400000: default 11 | 400002: //RootNode 12 | 2300000: default 13 | 3300000: default 14 | 4300000: default 15 | materials: 16 | importMaterials: 1 17 | materialName: 0 18 | materialSearch: 1 19 | animations: 20 | legacyGenerateAnimations: 4 21 | bakeSimulation: 0 22 | resampleCurves: 1 23 | optimizeGameObjects: 0 24 | motionNodeName: 25 | rigImportErrors: 26 | rigImportWarnings: 27 | animationImportErrors: 28 | animationImportWarnings: 29 | animationRetargetingWarnings: 30 | animationDoRetargetingWarnings: 0 31 | animationCompression: 1 32 | animationRotationError: 0.5 33 | animationPositionError: 0.5 34 | animationScaleError: 0.5 35 | animationWrapMode: 0 36 | extraExposedTransformPaths: [] 37 | clipAnimations: [] 38 | isReadable: 1 39 | avatarTransformableBones: [] 40 | meshes: 41 | lODScreenPercentages: [] 42 | globalScale: 1 43 | meshCompression: 0 44 | addColliders: 0 45 | importBlendShapes: 1 46 | swapUVChannels: 0 47 | generateSecondaryUV: 1 48 | useFileUnits: 1 49 | optimizeMeshForGPU: 1 50 | keepQuads: 0 51 | weldVertices: 1 52 | secondaryUVAngleDistortion: 8 53 | secondaryUVAreaDistortion: 15.000001 54 | secondaryUVHardAngle: 88 55 | secondaryUVPackMargin: 4 56 | useFileScale: 1 57 | tangentSpace: 58 | normalSmoothAngle: 60 59 | normalImportMode: 0 60 | tangentImportMode: 3 61 | importAnimation: 1 62 | copyAvatar: 0 63 | humanDescription: 64 | serializedVersion: 2 65 | human: [] 66 | skeleton: [] 67 | armTwist: 0.5 68 | foreArmTwist: 0.5 69 | upperLegTwist: 0.5 70 | legTwist: 0.5 71 | armStretch: 0.05 72 | legStretch: 0.05 73 | feetSpacing: 0 74 | rootMotionBoneName: 75 | rootMotionBoneRotation: {x: 0, y: 0, z: 0, w: 1} 76 | hasTranslationDoF: 0 77 | hasExtraRoot: 0 78 | skeletonHasParents: 1 79 | lastHumanDescriptionAvatarSource: {instanceID: 0} 80 | animationType: 0 81 | humanoidOversampling: 1 82 | additionalBone: 0 83 | userData: 84 | assetBundleName: 85 | assetBundleVariant: 86 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/SdfDepth.shader: -------------------------------------------------------------------------------- 1 | Shader "Hidden/SdfDepth" 2 | { 3 | Properties 4 | { 5 | [_NoScaleOffset] _MainTex("Base (RGB) Trans (A)", 2D) = "white" {} 6 | 7 | } 8 | SubShader 9 | { 10 | Tags { "RenderType"="Opaque" } 11 | LOD 100 12 | 13 | Pass 14 | { 15 | cull off 16 | CGPROGRAM 17 | #pragma vertex vert 18 | #pragma fragment frag 19 | #pragma multi_compile __ GEOM_TYPE_LEAF 20 | 21 | #include "UnityCG.cginc" 22 | 23 | struct appdata 24 | { 25 | float4 vertex : POSITION; 26 | float2 uv : TEXCOORD0; 27 | }; 28 | 29 | struct v2f 30 | { 31 | float2 uv : TEXCOORD0; 32 | float3 wpos : TEXCOORD1; 33 | 34 | float4 vertex : SV_POSITION; 35 | }; 36 | 37 | 38 | sampler2D _MainTex; 39 | float4 _MainTex_ST; 40 | 41 | v2f vert (appdata v) 42 | { 43 | v2f o; 44 | o.vertex = UnityObjectToClipPos(v.vertex); 45 | o.wpos = mul(unity_ObjectToWorld, v.vertex);// 46 | o.uv = TRANSFORM_TEX(v.uv, _MainTex); 47 | 48 | return o; 49 | } 50 | 51 | fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target 52 | { 53 | if (tex2D(_MainTex, i.uv).a < 0.5) { 54 | return 100; 55 | } 56 | float dis = length(i.wpos - _WorldSpaceCameraPos.xyz); 57 | #ifdef GEOM_TYPE_LEAF 58 | return dis; 59 | #else 60 | return (facing<0?1:-1)* dis;// 61 | #endif 62 | } 63 | ENDCG 64 | } 65 | //Pass 66 | //{ 67 | // cull front 68 | // CGPROGRAM 69 | // #pragma vertex vert 70 | // #pragma fragment frag 71 | 72 | 73 | // #include "UnityCG.cginc" 74 | 75 | // struct appdata 76 | // { 77 | // float4 vertex : POSITION; 78 | // float2 uv : TEXCOORD0; 79 | // }; 80 | 81 | // struct v2f 82 | // { 83 | // float2 uv : TEXCOORD0; 84 | // float3 wpos : TEXCOORD1; 85 | 86 | // float4 vertex : SV_POSITION; 87 | // }; 88 | 89 | // 90 | // sampler2D _MainTex; 91 | // float4 _MainTex_ST; 92 | 93 | // v2f vert(appdata v) 94 | // { 95 | // v2f o; 96 | // o.vertex = UnityObjectToClipPos(v.vertex); 97 | // o.wpos = mul(unity_ObjectToWorld, v.vertex);// 98 | // o.uv = TRANSFORM_TEX(v.uv, _MainTex); 99 | 100 | // return o; 101 | // } 102 | 103 | // fixed4 frag(v2f i) : SV_Target 104 | // { 105 | // clip(tex2D(_MainTex, i.uv).a - 0.5); 106 | 107 | // return -length(i.wpos - _WorldSpaceCameraPos.xyz);// _WorldSpaceCameraPos.xyz ); i.dis; 108 | // } 109 | // ENDCG 110 | //} 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /Assembly-CSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 10.0.20506 7 | 2.0 8 | 9 | {60267BC2-96D2-EB75-93BF-5B0874C950B6} 10 | Library 11 | Properties 12 | Assembly-CSharp 13 | v3.5 14 | 512 15 | Assets 16 | 17 | 18 | true 19 | full 20 | false 21 | Temp\bin\Debug\ 22 | DEBUG;TRACE;UNITY_5_3_OR_NEWER;UNITY_5_4_OR_NEWER;UNITY_5_5_OR_NEWER;UNITY_5_6_OR_NEWER;UNITY_5_6_5;UNITY_5_6;UNITY_5;ENABLE_AUDIO;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_DUCK_TYPING;ENABLE_GENERICS;ENABLE_PVR_GI;ENABLE_MICROPHONE;ENABLE_MULTIPLE_DISPLAYS;ENABLE_PHYSICS;ENABLE_RUNTIME_NAVMESH_BUILDING;ENABLE_SPRITERENDERER_FLIPPING;ENABLE_SPRITES;ENABLE_TERRAIN;ENABLE_RAKNET;ENABLE_UNET;ENABLE_LZMA;ENABLE_UNITYEVENTS;ENABLE_WEBCAM;ENABLE_WWW;ENABLE_CLOUD_SERVICES_COLLAB;ENABLE_CLOUD_SERVICES_ADS;ENABLE_CLOUD_HUB;ENABLE_CLOUD_PROJECT_ID;ENABLE_CLOUD_SERVICES_UNET;ENABLE_CLOUD_SERVICES_BUILD;ENABLE_CLOUD_LICENSE;ENABLE_EDITOR_METRICS;ENABLE_EDITOR_METRICS_CACHING;ENABLE_NATIVE_ARRAY;INCLUDE_DYNAMIC_GI;INCLUDE_GI;PLATFORM_SUPPORTS_MONO;RENDER_SOFTWARE_CURSOR;INCLUDE_PUBNUB;ENABLE_PLAYMODE_TESTS_RUNNER;ENABLE_SCRIPTING_NEW_CSHARP_COMPILER;ENABLE_VIDEO;UNITY_STANDALONE_WIN;UNITY_STANDALONE;ENABLE_SUBSTANCE;ENABLE_RUNTIME_GI;ENABLE_MOVIES;ENABLE_NETWORK;ENABLE_CRUNCH_TEXTURE_COMPRESSION;ENABLE_UNITYWEBREQUEST;ENABLE_CLOUD_SERVICES;ENABLE_CLOUD_SERVICES_ANALYTICS;ENABLE_CLOUD_SERVICES_PURCHASING;ENABLE_CLOUD_SERVICES_CRASH_REPORTING;ENABLE_EVENT_QUEUE;ENABLE_CLUSTERINPUT;ENABLE_VR;ENABLE_WEBSOCKET_HOST;ENABLE_MONO;NET_2_0_SUBSET;ENABLE_PROFILER;UNITY_ASSERTIONS;UNITY_EDITOR;UNITY_EDITOR_64;UNITY_EDITOR_WIN;ENABLE_NATIVE_ARRAY_CHECKS;UNITY_TEAM_LICENSE 23 | prompt 24 | 4 25 | 0169 26 | 27 | 28 | pdbonly 29 | true 30 | Temp\bin\Release\ 31 | prompt 32 | 4 33 | 0169 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | C:/Program Files/Unity/Editor/Data/Managed/UnityEngine.dll 43 | 44 | 45 | C:/Program Files/Unity/Editor/Data/Managed/UnityEditor.dll 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll 55 | 56 | 57 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/Networking/UnityEngine.Networking.dll 58 | 59 | 60 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/TestRunner/UnityEngine.TestRunner.dll 61 | 62 | 63 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/TestRunner/net35/unity-custom/nunit.framework.dll 64 | 65 | 66 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/UnityAnalytics/UnityEngine.Analytics.dll 67 | 68 | 69 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/UnityHoloLens/RuntimeEditor/UnityEngine.HoloLens.dll 70 | 71 | 72 | C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/UnityVR/RuntimeEditor/UnityEngine.VR.dll 73 | 74 | 75 | 76 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /Assets/sdfshadowdemo/SdfBaker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using UnityEngine; 6 | [ExecuteInEditMode] 7 | public class SdfBaker : MonoBehaviour { 8 | public enum BakeSdfMode { 9 | RAY_CAST,RENDER_DEPTH 10 | } 11 | [Header("烘焙9700 +2060s 需要3分钟左右")] 12 | public bool ckickToBake; 13 | public BakeSdfMode bakeSdfMode = BakeSdfMode.RENDER_DEPTH; 14 | RenderTexture tempDepth; 15 | RenderTexture rt; 16 | private Camera camera; 17 | public Shader renderShader; 18 | public ComputeShader calMinDistance; 19 | ComputeBuffer resBuff; 20 | 21 | 22 | Dictionary posCacheMap; 23 | 24 | public Texture3D sdfTex; 25 | 26 | //public bool sdfTree; 27 | [ContextMenu("bakesdf")] 28 | void bakesdf() 29 | { 30 | Stopwatch sw = new Stopwatch(); 31 | sw.Start(); 32 | 33 | posCacheMap = new Dictionary(); 34 | if (bakeSdfMode == BakeSdfMode.RENDER_DEPTH) 35 | { 36 | initCamera(); 37 | print(calMinDistance.FindKernel("CSMain")); 38 | resBuff = new ComputeBuffer(1, 4); 39 | calMinDistance.SetTexture(0, "depthTex", tempDepth); 40 | calMinDistance.SetBuffer(0, "rstBuff", resBuff); 41 | } 42 | 43 | int scale =1; 44 | int xCount = 100* scale; 45 | int zCount = 100* scale; 46 | int yCount = 20* scale; 47 | float step = 1.0f/ scale; 48 | sdfTex = new Texture3D(xCount, zCount, yCount, TextureFormat.RFloat, false); 49 | sdfTex.filterMode = FilterMode.Trilinear; 50 | sdfTex.wrapMode = TextureWrapMode.Clamp; 51 | 52 | var bakingColors= new Color[xCount * zCount * yCount]; 53 | for (int x = 0; x < xCount; x++) 54 | { 55 | for (int z = 0; z < zCount; z++) 56 | { 57 | for (int y = 0; y < yCount; y++) 58 | { 59 | if(bakeSdfMode== BakeSdfMode.RAY_CAST) 60 | bakingColors[y * xCount * zCount + z * xCount + x].r = getSdfFromRay(new Vector3(x, y, z)*step + Vector3.one * step/2 - new Vector3(xCount/scale/2, 0, zCount / scale / 2)); 61 | else 62 | bakingColors[y * xCount * zCount + z * xCount + x].r = getSdfFromCamera(new Vector3(x, y, z)*step + Vector3.one * step/2 - new Vector3(xCount/scale/2, 0, zCount / scale / 2)); 63 | 64 | } 65 | 66 | } 67 | 68 | } 69 | 70 | sdfTex.SetPixels(bakingColors); 71 | sdfTex.Apply(); 72 | 73 | Shader.SetGlobalTexture("_TestSdfTex", sdfTex); 74 | sw.Stop(); 75 | print("baking time:"+sw.ElapsedMilliseconds/1000+" s"); 76 | 77 | } 78 | 79 | 80 | private float getSdfFromCamera(Vector3 wpos) 81 | { 82 | int posIndex= ((int)wpos.z/3 + 500) * 1000 * 1000 + ((int)wpos.y/3 + 500) * 1000 + ((int)wpos.x/3 + 500); 83 | if (posCacheMap.ContainsKey(posIndex)) { 84 | return posCacheMap[posIndex]; 85 | } 86 | transform.position = wpos; 87 | camera.RenderToCubemap(tempDepth); 88 | if (rt == null) 89 | { 90 | rt = RenderTexture.GetTemporary(tempDepth.width, tempDepth.height, tempDepth.depth, tempDepth.format, RenderTextureReadWrite.Linear); 91 | rt.dimension = UnityEngine.Rendering.TextureDimension.Tex2DArray; 92 | rt.volumeDepth = 6; 93 | rt.Create(); 94 | 95 | 96 | } 97 | 98 | for (int i = 0; i < 6; i++) 99 | { 100 | Graphics.CopyTexture(tempDepth, i, rt, i); 101 | } 102 | calMinDistance.SetTexture(0, "depthTex", rt); 103 | 104 | 105 | calMinDistance.Dispatch(0, 1, 1, 1); 106 | 107 | float[] rstArray = new float[1]; 108 | resBuff.GetData(rstArray); 109 | 110 | // print(rstArray[0]); 111 | if(rstArray[0]>4) posCacheMap[posIndex]= rstArray[0]-3; 112 | return rstArray[0]; 113 | } 114 | 115 | private void initCamera() 116 | { 117 | camera = GetComponent(); 118 | camera.enabled = false; 119 | camera.SetReplacementShader(renderShader, ""); 120 | if (tempDepth == null) 121 | { 122 | tempDepth = new RenderTexture(32, 32, 16, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear);// .GetTemporary(32, 32, 16, RenderTextureFormat.ARGBHalf); 123 | 124 | tempDepth.dimension = UnityEngine.Rendering.TextureDimension.Cube; 125 | tempDepth.Create(); 126 | } 127 | 128 | } 129 | 130 | // Update is called once per frame 131 | void Update () { 132 | if (ckickToBake) { 133 | ckickToBake = false; 134 | bakesdf(); 135 | } 136 | if(sdfTex!=null) 137 | Shader.SetGlobalTexture("_TestSdfTex", sdfTex); 138 | } 139 | 140 | 141 | float getSdfFromRay(Vector3 wpos) { 142 | int posIndex = ((int)wpos.z / 3 + 500) * 1000 * 1000 + ((int)wpos.y / 3 + 500) * 1000 + ((int)wpos.x / 3 + 500); 143 | if (posCacheMap.ContainsKey(posIndex)) 144 | { 145 | return posCacheMap[posIndex]; 146 | } 147 | 148 | //正向求交 存最近表面距离 149 | float minDis = 100; 150 | int sign = 1; 151 | int rayRow = 32; 152 | for (int i = 0; i < rayRow; i++) 153 | { 154 | for (int j = 0; j <= rayRow; j++) 155 | { 156 | Vector3 dir = Vector3.zero; 157 | float yLen = Mathf.Sin(Mathf.Deg2Rad * (j - rayRow/2) * 180 / rayRow); 158 | float xzLen = Mathf.Sqrt(1 - yLen * yLen); 159 | dir.y = yLen; 160 | dir.x = Mathf.Cos(Mathf.Deg2Rad * i * 360 / rayRow) * xzLen; 161 | dir.z = Mathf.Sin(Mathf.Deg2Rad * i * 360 / rayRow) * xzLen; 162 | 163 | RaycastHit hitInfo; 164 | 165 | if (Physics.Raycast(wpos, dir, out hitInfo)) 166 | { 167 | if (minDis > hitInfo.distance) 168 | { 169 | 170 | minDis = hitInfo.distance; 171 | } 172 | } 173 | 174 | } 175 | 176 | } 177 | 178 | //反向求交 存最近表面距离 179 | for (int i = 0; i < rayRow; i++) 180 | { 181 | for (int j = 0; j <= rayRow; j++) 182 | { 183 | Vector3 dir = Vector3.zero; 184 | float yLen = Mathf.Sin(Mathf.Deg2Rad * (j - rayRow/2) * 180 / rayRow); 185 | float xzLen = Mathf.Sqrt(1 - yLen * yLen); 186 | dir.y = yLen; 187 | dir.x = Mathf.Cos(Mathf.Deg2Rad * i * 360 / rayRow) * xzLen; 188 | dir.z = Mathf.Sin(Mathf.Deg2Rad * i * 360 / rayRow) * xzLen; 189 | 190 | RaycastHit[] hitInfos = Physics.RaycastAll(wpos + dir * minDis, -dir, minDis); 191 | float maxRevertDis = 0; 192 | foreach (var hitInfo in hitInfos) 193 | { 194 | maxRevertDis = Mathf.Max(maxRevertDis, hitInfo.distance); 195 | sign = -1; 196 | } 197 | if (maxRevertDis != 0) { 198 | minDis = minDis - maxRevertDis; 199 | } 200 | 201 | } 202 | 203 | } 204 | 205 | minDis *= sign; 206 | 207 | if (minDis > 4) return posCacheMap[posIndex] = minDis - 3; 208 | return minDis; 209 | } 210 | 211 | } 212 | --------------------------------------------------------------------------------