unity 拿shadowmap/ sample shadow map/拿_ShadowMapTexture
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 anaddshadowdirective will make them generate a shadow pass too.
===================
Done
unity 拿shadowmap/ sample shadow map/拿_ShadowMapTexture的更多相关文章
- Unity基础6 Shadow Map 阴影实现
这篇实现来的有点墨迹,前前后后折腾零碎的时间折腾了半个月才才实现一个基本的shadow map流程,只能说是对原理理解更深刻一些,但离实际应用估计还需要做很多优化.这篇文章大致分析下shadow ma ...
- Unity基础(5) Shadow Map 概述
这篇是自己看shadow map是的一些笔记,内容稍稍凌乱,如有错误请帮忙纠正 1.常见阴影处理方式 Shadow Map : using Z-Buffer Shadow Mapping 的原理与实践 ...
- [ZZ] Shadow Map
Shadow Map 如何能够高效的产生更接近真实的阴影一直是视频游戏的一个很有挑战的工作,本文介绍目前所为人熟知的两种阴影技术之一的ShadowMap(阴影图)技术. ShadowMap技术 ...
- [工作积累] shadow map问题汇总
1.基本问题和相关 Common Techniques to Improve Shadow Depth Maps: https://msdn.microsoft.com/en-us/library/w ...
- Shadow Map 原理和改进 【转】
http://blog.csdn.net/ronintao/article/details/51649664 参考 1.Common Techniques to Improve Shadow Dept ...
- (转)Shadow Map & Shadow Volume
转自:http://blog.csdn.net/hippig/article/details/7858574 shadow volume 这个术语几乎是随着 DOOM3 的发布而成为FPS 玩家和图形 ...
- Shadow Map 实现极其细节
这里不介绍算法原理,只说说在实现过程中遇到的问题,以及背后的原因.开发环境:opengl 2.0 glsl 1.0. 第一个问题:产生深度纹理. 在opengl中每一次离屏渲染需要向opengl提供 ...
- Shadow Map阴影贴图技术之探 【转】
这两天勉勉强强把一个shadowmap的demo做出来了.参考资料多,苦头可不少.Shadow Map技术是目前与Shadow Volume技术并行的传统阴影渲染技术,而且在游戏领域可谓占很大优势.本 ...
- GraphicsLab Project之再谈Shadow Map
作者:i_dovelemon 日期:2019-06-07 主题:Shadow Map(SM), Percentage Closer Filtering(PCF), Variance Shadow Ma ...
随机推荐
- 【转载】Python: Enum枚举的实现
转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html 从C系语言过来用Python,好不容易适应了写代码不打 ...
- [Android] 按钮单击事件的五种写法
在平时学习安卓的过程中,不论是看视频还是看博客,我发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.所以我想把这些写法总结一下,比较下各种写法的优劣,希望可以让自己可以灵 ...
- 打印之Lodop
前序 前面遇到一个问题:在线打印合同.通过各方查找资料和请教他人,终于完美的解决了这个问题.其中的解决方案,可以查看:http://www.cnblogs.com/zcy-xy/p/4290436.h ...
- kindeditor异步加载 无法初始化
使用集成的php输出kindeditor无法初始化 function showEditor($id, $value='', $width='700px', $height='300px', $styl ...
- Druid 架构
本篇译自 Druid 项目白皮书部分内容( https://github.com/apache/incubator-druid/tree/master/publications/whitepaper/ ...
- 洛谷P1074 靶形数独 [搜索]
题目传送门 题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了 ...
- Maven实用总结
使用Maven还是推荐IDEA,以前用eclipse总是喜欢出现乱七八糟的问题,具体错误和解决方案也记不清楚了. 下面总结下IDEA中遇到的问题和解决方法: 与IDEA搭配的相关问题 如何根据模板快速 ...
- UTF-8字符「EF BF BD」-备胎
在众多的utf-8码点值中,除了ascii,你还应该记住「EF BF BD」,因为它是很多编程语言以及库中的备胎,即无效的码点值在编码的时候会默认用这个码点值进行替换,即utf-8中的超级「备胎」(R ...
- 3000大洋,自己配置i5主机
自用的HP笔记本已经“七年之痒”了,所以在2016年新年之前,换了台新电脑.主要用来娱乐(电影),但是也要能够满足工作学习(本人从事IT行业)的需要. 遵循本人一贯的高冷装X的习惯,决定自己买配件攒机 ...
- Kali Linux WPScan更新到2.9.3
Kali Linux WPScan更新到2.9.3 WPScan是Kali Linux内置的一款Web漏洞扫描工具,专门扫描WordPress模版构建的网站.该工具最近更新到2.9.3.在新版本中 ...