https://gamedev.stackexchange.com/questions/96051/unity-5-how-to-get-a-shadowmap
UNITY_DECLARE_SHADOWMAP(tex) - declares a shadowmap texture variable with name “tex”. UNITY_SAMPLE_SHADOW(tex,uv) - samples shadowmap texture “tex” at given “uv” coordinate (XY components are texture location, Z component is depth to compare with). Returns single float value with the shadow term in 0..1 range. UNITY_SAMPLE_SHADOW_PROJ(tex,uv) - similar to above, but does a projective shadowmap read. “uv” is a float4, all other components are divided by .w for doing the lookup.
https://docs.unity3d.com/Manual/SL-BuiltinMacros.html
https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetShadowSamplingMode.html
using UnityEngine; using UnityEngine.Rendering;
[RequireComponent(typeof(Camera))] public class RawShadowmapDepth : MonoBehaviour { public Light m_Light; RenderTexture m_ShadowmapCopy;
void Start() { RenderTargetIdentifier shadowmap = BuiltinRenderTextureType.CurrentActive; m_ShadowmapCopy = new RenderTexture(1024, 1024, 0); CommandBuffer cb = new CommandBuffer();
// Change shadow sampling mode for m_Light's shadowmap. cb.SetShadowSamplingMode(shadowmap, ShadowSamplingMode.RawDepth);
// The shadowmap values can now be sampled normally - copy it to a different render texture. cb.Blit(shadowmap, new RenderTargetIdentifier(m_ShadowmapCopy));
// Execute after the shadowmap has been filled. m_Light.AddCommandBuffer(LightEvent.AfterShadowMap, cb);
// Sampling mode is restored automatically after this command buffer completes, so shadows will render normally. }
void OnRenderImage(RenderTexture src, RenderTexture dest) { // Display the shadowmap in the corner. Camera.main.rect = new Rect(0, 0, 0.5f, 0.5f); Graphics.Blit(m_ShadowmapCopy, dest); Camera.main.rect = new Rect(0, 0, 1, 1); } }
Texture2D _ShadowMapTexture;
聲明下就能用了 不行你再blit一份出來用
注意一個事情是 他本身那個world to shadow的martrix是 screenspace的 和主camera有關 所以是不能用的(他做screenspace shadow)可以用
所以你要自己拿 world to shadowspace的matric
就是camera在light pos的那個space
====================================
經測試是拿到的
using UnityEngine; using UnityEngine.Rendering; [RequireComponent(typeof(Camera))] public class rawdepth : MonoBehaviour { public Light m_Light; RenderTexture m_ShadowmapCopy; // Use this for initialization void Start () { RenderTargetIdentifier shadowmap = BuiltinRenderTextureType.CurrentActive; m_ShadowmapCopy = new RenderTexture(4096, 4096, 0); CommandBuffer cb = new CommandBuffer(); // Change shadow sampling mode for m_Light's shadowmap. cb.SetShadowSamplingMode(shadowmap, ShadowSamplingMode.RawDepth); // The shadowmap values can now be sampled normally - copy it to a different render texture. cb.Blit(shadowmap, new RenderTargetIdentifier(m_ShadowmapCopy)); // Execute after the shadowmap has been filled. m_Light.AddCommandBuffer(LightEvent.AfterShadowMap, cb); // Sampling mode is restored automatically after this command buffer completes, so shadows will render normally. } //Update is called once per frame void OnRenderImage(RenderTexture src, RenderTexture dest) { //Display the shadowmap in the corner. Camera.main.rect = new Rect(0, 0, 0.5f, 0.5f); Graphics.Blit(m_ShadowmapCopy, dest); Camera.main.rect = new Rect(0, 0, 1, 1);
Shader.SetGlobalTexture("_ShadowMap", shadowMap);//buildin } }
ref
https://www.cnblogs.com/wangze/archive/2010/04/07/1706640.html
https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetShadowSamplingMode.html
采樣的時候有proj的問題 要注意
fragmentshader:
float4 wpos;
wpos = i.worldPos;
//w在這里面了 proj的信息
float4 shadowCoord = mul(unity_WorldToShadow[0], wpos);
float4 shadow = tex2Dproj(_ShadowMap, shadowCoord);
//float4 shadow =tex2D(_ShadowMap, shadowCoord.xy/shadowCoord.w);//這個方法也對
=================
unity_WorldToShadow[0]這個matrix就是world 到camera 在light的V Proj到二維 再加 -1到1到0到1
是可用的
=================
拿 _CameraDepthTexture
https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html
聲明下
_CameraDepthTexture就可以了
看了下forward render path
Camera Detph在shadow caster pass會畫這樣一張rt 是在camera space的用兩個matrix就可以轉到lightspace做shadow用
它的位置也是個zprepass的位置 screen normal在接下來的pass里
看起來unity是在復用 shadow caster做張depth用
Depth texture is rendered using the same shader
passes as used for shadow caster rendering
(ShadowCaster pass type). So by extension, if a shader does not support shadow casting (i.e. there’s no shadow caster pass in the shader or any of the fallbacks), then objects using that shader will not show up in the depth texture.
- Make your shader fallback to some other shader that has a shadow casting pass, or
- If you’re using surface shaders
, adding anaddshadowdirective will make them generate a shadow pass too.
===================
Done
