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(, , );
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.5f, 0.5f);
Graphics.Blit(m_ShadowmapCopy, dest);
Camera.main.rect = new Rect(, , , );
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 anaddshadow
directive will make them generate a shadow pass too.
===================
Done