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 an addshadow directive will make them generate a shadow pass too.
 ===================
Done

最新文章

  1. 学习CSS3动画(animation)
  2. 隐语义模型LFM(latent factor model)
  3. numpy之sum
  4. 【转】java静态代码块和构造方法执行顺序
  5. 《工作型PPT设计之道》培训心得
  6. CCEditBox用法
  7. POJ 1011 sticks 搜索
  8. 在imge控件中直接显示图片(图片是byte[]格式)
  9. gitHub远程分支创建
  10. Android 使用 RemoteViews 发送自定义通知 ,遇到 Couldn't expand RemoteViews问题总结
  11. Oracle游标-循环查询表中数据(表名),并执行
  12. Java垃圾回收机制以及内存泄露
  13. Tyvj-TOM的无穷序列
  14. .NET MVC与三层架构
  15. 从Thread.start()方法看Thread源码,多次start一个线程会怎么样
  16. c运行时函数参考学习地址
  17. 【SSH系列】Hibernate映射 -- 一对多关联映射
  18. SSM-SpringMVC-22:SpringMVC中转发(forward)和重定向(redirect)
  19. [Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II
  20. cmake教程

热门文章

  1. ES6的新增数据类型:Symbol
  2. C#发送Post请求,带参数,不带参数,指定参数
  3. javaScript存储
  4. 微信小程序 - 时间进度条功能
  5. Java学习笔记(十)——xml
  6. selenium c# 的注意事项
  7. 延长SSH的连接时间并重启ssh服务
  8. 四十八 常用内建模块 HTMLParser
  9. Python函数式编程——map()、reduce()
  10. RabbitMQ (十三) 集群+单机搭建(window)