https://gamedev.stackexchange.com/questions/96051/unity-5-how-to-get-a-shadowmap

  1. UNITY_DECLARE_SHADOWMAP(tex) - declares a shadowmap texture variable with name tex”.
  2. 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.
  3. 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.
  4. https://docs.unity3d.com/Manual/SL-BuiltinMacros.html
  5. https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetShadowSamplingMode.html
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3.  
  4. [RequireComponent(typeof(Camera))]
  5. public class RawShadowmapDepth : MonoBehaviour
  6. {
  7. public Light m_Light;
  8. RenderTexture m_ShadowmapCopy;
  9.  
  10. void Start()
  11. {
  12. RenderTargetIdentifier shadowmap = BuiltinRenderTextureType.CurrentActive;
  13. m_ShadowmapCopy = new RenderTexture(1024, 1024, 0);
  14. CommandBuffer cb = new CommandBuffer();
  15.  
  16. // Change shadow sampling mode for m_Light's shadowmap.
  17. cb.SetShadowSamplingMode(shadowmap, ShadowSamplingMode.RawDepth);
  18.  
  19. // The shadowmap values can now be sampled normally - copy it to a different render texture.
  20. cb.Blit(shadowmap, new RenderTargetIdentifier(m_ShadowmapCopy));
  21.  
  22. // Execute after the shadowmap has been filled.
  23. m_Light.AddCommandBuffer(LightEvent.AfterShadowMap, cb);
  24.  
  25. // Sampling mode is restored automatically after this command buffer completes, so shadows will render normally.
  26. }
  27.  
  28. void OnRenderImage(RenderTexture src, RenderTexture dest)
  29. {
  30. // Display the shadowmap in the corner.
  31. Camera.main.rect = new Rect(0, 0, 0.5f, 0.5f);
  32. Graphics.Blit(m_ShadowmapCopy, dest);
  33. Camera.main.rect = new Rect(0, 0, 1, 1);
  34. }
  35. }
  36.  
  37. Texture2D _ShadowMapTexture;
    声明下就能用了 不行你再blit一份出来用
    注意一个事情是 他本身那个world to shadowmartrix screenspace 和主camera有关 所以是不能用的(他做screenspace shadow)可以用
    所以你要自己拿 world to shadowspacematric
    就是cameralight pos的那个space
    ====================================
    经测试是拿到的
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. [RequireComponent(typeof(Camera))]
  4. public class rawdepth : MonoBehaviour {
  5.  
  6. public Light m_Light;
  7. RenderTexture m_ShadowmapCopy;
  8. // Use this for initialization
  9. void Start () {
  10. RenderTargetIdentifier shadowmap = BuiltinRenderTextureType.CurrentActive;
  11. m_ShadowmapCopy = new RenderTexture(, , );
  12. CommandBuffer cb = new CommandBuffer();
  13.  
  14. // Change shadow sampling mode for m_Light's shadowmap.
  15. cb.SetShadowSamplingMode(shadowmap, ShadowSamplingMode.RawDepth);
  16.  
  17. // The shadowmap values can now be sampled normally - copy it to a different render texture.
  18. cb.Blit(shadowmap, new RenderTargetIdentifier(m_ShadowmapCopy));
  19.  
  20. // Execute after the shadowmap has been filled.
  21. m_Light.AddCommandBuffer(LightEvent.AfterShadowMap, cb);
  22.  
  23. // Sampling mode is restored automatically after this command buffer completes, so shadows will render normally.
  24.  
  25. }
  26.  
  27. //Update is called once per frame
  28. void OnRenderImage(RenderTexture src, RenderTexture dest)
  29. {
  30. //Display the shadowmap in the corner.
  31. Camera.main.rect = new Rect(, , 0.5f, 0.5f);
  32. Graphics.Blit(m_ShadowmapCopy, dest);
  33. Camera.main.rect = new Rect(, , , );
    Shader.SetGlobalTexture("_ShadowMap", shadowMap);//buildin
  34. }
  35. }
  1.  
  2. 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);//这个方法也对

  1. =================
    unity_WorldToShadow[0]这个matrix就是world camera lightV Proj到二维 再加 -1101
    是可用的
    =================
    _CameraDepthTexture
    https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html
    声明下
  1. _CameraDepthTexture就可以了
  2.  
  3. 看了下forward render path
    Camera Detphshadow caster pass会画这样一张rt 是在camera space的用两个matrix就可以转到lightspaceshadow
    它的位置也是个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.
  1.  ===================
    Done

unity 拿shadowmap/ sample shadow map/拿_ShadowMapTexture的更多相关文章

  1. Unity基础6 Shadow Map 阴影实现

    这篇实现来的有点墨迹,前前后后折腾零碎的时间折腾了半个月才才实现一个基本的shadow map流程,只能说是对原理理解更深刻一些,但离实际应用估计还需要做很多优化.这篇文章大致分析下shadow ma ...

  2. Unity基础(5) Shadow Map 概述

    这篇是自己看shadow map是的一些笔记,内容稍稍凌乱,如有错误请帮忙纠正 1.常见阴影处理方式 Shadow Map : using Z-Buffer Shadow Mapping 的原理与实践 ...

  3. [ZZ] Shadow Map

    Shadow Map 如何能够高效的产生更接近真实的阴影一直是视频游戏的一个很有挑战的工作,本文介绍目前所为人熟知的两种阴影技术之一的ShadowMap(阴影图)技术.     ShadowMap技术 ...

  4. [工作积累] shadow map问题汇总

    1.基本问题和相关 Common Techniques to Improve Shadow Depth Maps: https://msdn.microsoft.com/en-us/library/w ...

  5. Shadow Map 原理和改进 【转】

    http://blog.csdn.net/ronintao/article/details/51649664 参考 1.Common Techniques to Improve Shadow Dept ...

  6. (转)Shadow Map & Shadow Volume

    转自:http://blog.csdn.net/hippig/article/details/7858574 shadow volume 这个术语几乎是随着 DOOM3 的发布而成为FPS 玩家和图形 ...

  7. Shadow Map 实现极其细节

    这里不介绍算法原理,只说说在实现过程中遇到的问题,以及背后的原因.开发环境:opengl 2.0  glsl 1.0. 第一个问题:产生深度纹理. 在opengl中每一次离屏渲染需要向opengl提供 ...

  8. Shadow Map阴影贴图技术之探 【转】

    这两天勉勉强强把一个shadowmap的demo做出来了.参考资料多,苦头可不少.Shadow Map技术是目前与Shadow Volume技术并行的传统阴影渲染技术,而且在游戏领域可谓占很大优势.本 ...

  9. GraphicsLab Project之再谈Shadow Map

    作者:i_dovelemon 日期:2019-06-07 主题:Shadow Map(SM), Percentage Closer Filtering(PCF), Variance Shadow Ma ...

随机推荐

  1. Oracle11g常用的命令

    cmd H: cd H:\oracle\product\\Db_1\BIN exp jz/jz file=C:/QS-BF20131017.dmp (备份) imp jz/jz file=C:/BF2 ...

  2. 六:ZooKeeper的java客户端api的使用

    一:客户端链接测试 package com.yeepay.sxf.createConnection; import java.io.IOException; import org.apache.zoo ...

  3. Longest Palindromic Substring (最长回文字符串)——两种方法还没看,仍需认真看看

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. Linux 基础——开山篇

    为什么要开始学习Linux命令? 首先当然是因为工作需要了,现在的工作是负责银行调度的系统的源系统接入的工作,经常要到生产部署版本.所以……买了一本<Linux命令行与shell脚本编程大全&g ...

  5. numpy及scipy的使用

    numpy的使用 把list A转换为numpy 矩阵 np.array(A) np.array(A, 'int32') numpy加载txt文件里面的矩阵 matrix = np.loadtxt(t ...

  6. Zabbix历史数据库迁移 及分区

    https://blog.csdn.net/hkyw000/article/details/78971201?utm_source=blogxgwz6

  7. 根据C# 事件思想来实现 php 事件

    事件定义 当我们使用委托场景时,我们很希望有这样两个角色出现:广播者和订阅者.我们需要这两个角色来实现订阅和广播这种很常见的场景. 广播者这个角色应该有这样的功能:包括一个委托字段,通过调用委托来发出 ...

  8. windows命令启动mysql

    找到mysql的安装位置,进入bin目录 dos输入  mysql -h localhost -uroot -p   ,在输入密码

  9. 转:nginx+CGI/FASTCGI

    简介版: 1.fastcgi与cgi区别:fastcgi通过线程来响应请求,而cgi对每个请求生成一个进程. 2.典型nginx数据传输过程:user->nginx->本地socket(请 ...

  10. 洛谷——P2381 圆圆舞蹈

    P2381 圆圆舞蹈 题目描述 熊大妈的乃修在时针的带领下,围成了一个圆圈舞蹈,由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针 ...