当前位置: 首页 > news >正文

unity深度图

https://gameinstitute.qq.com/community/detail/124214
https://catlikecoding.com/unity/tutorials/scriptable-render-pipeline/spotlight-shadows/
https://docs.unity3d.com/ScriptReference/RenderTextureFormat.Shadowmap.html

渲染到深度rt:

using UnityEngine;

public class DrawDepthTexture : MonoBehaviour
{
    public RenderTexture colorRT;
    public RenderTexture depthRT;
    public Material depthMat;
    void Start()
    {
        bool flag = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Shadowmap);
        Debug.LogError(flag);

        colorRT = new RenderTexture(Camera.main.pixelWidth, Camera.main.pixelHeight, 0);
        depthRT = new RenderTexture(Camera.main.pixelWidth, Camera.main.pixelHeight, 16, RenderTextureFormat.Depth);
        Camera.main.SetTargetBuffers(depthRT.colorBuffer, depthRT.depthBuffer);
        depthMat.SetTexture("_MainTex", depthRT);
        Matrix4x4 proj = Camera.main.projectionMatrix;
        proj = GL.GetGPUProjectionMatrix(proj, true);
        Matrix4x4 view = Camera.main.worldToCameraMatrix;
        Matrix4x4 vp = proj * view;
        Debug.LogError(vp);

        Debug.LogError(vp.MultiplyPoint3x4(new Vector3(0, 0, 2)));
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(source, destination, depthMat);
    }
}

渲染物体的shader,用任何的都可以,下面是采样深度图,blit到屏幕:

Shader "Unlit/DrawDepthTexture"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata 
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
                return o;
            }  

			sampler2D _MainTex;
            fixed4 frag (v2f i) : SV_Target
            {
				float depth = tex2D(_MainTex, i.uv).r;
				return fixed4(depth, 0, 0, 1);
            }
            ENDCG
        }
    }
}

直接使用tex2D,采样即可。
在这里插入图片描述

如果是使用原生的ShadowMap格式:

depthRT = new RenderTexture(Camera.main.pixelWidth, Camera.main.pixelHeight, 16, RenderTextureFormat.Shadowmap);

则采样原生的Shadowmap图的时候,使用:

Shader "Unlit/DrawDepthTexture"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata 
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
                return o;
            }  

			uniform Texture2D _MainTex;
			uniform SamplerState sampler_MainTex;
			Texture2D fakePoint;
			SamplerState samplerfakePoint;

            fixed4 frag (v2f i) : SV_Target
            {
				float depth = fakePoint.Sample(samplerfakePoint,float2(0,0)).a; //建立采样器/  
				for (int j = 0; j < 5; j++)
				{
					depth += _MainTex.Sample(samplerfakePoint, i.uv).r;			//对该点进行反复采样/  
				}
				depth -= fakePoint.Sample(samplerfakePoint, float2(0, 0)).a;    //去除多余量/  
				depth = depth / 5.f;                                            //对采样总和取平均值/  
				fixed4 col = fixed4(depth, 0, 0, 1.f);
				return col;
            }
            ENDCG
        }
    }
}

不知道作者是从哪里找到这样的方法,有知道的告诉我unity原生的shadowmap是如何编码的。

相关文章:

  • SRP——聚光灯的阴影
  • CSS——背景图像区域
  • Tex2DArray的使用举例
  • Linux中查看系统资源占用情况的命令
  • unity中草实现举例
  • MySQL 并发事务问题以及事务的隔离级别
  • 四元数转矩阵
  • Activator.CreateInstance with parameters
  • 再谈DrawMeshInstancedIndirect的参数问题
  • Sitecore 8.2 防火墙规则的权威指南
  • unity烘焙参数的程序化配置
  • 苗条的生成树 Slim Span--洛谷
  • 关于ADB 执行报错问题-db server version (31) doesn't match this client (40); killing...
  • 如何查看srp中的shader文件
  • 项目Beta冲刺(6/7)(追光的人)(2019.5.28)
  • [数据结构]链表的实现在PHP中
  • 【Leetcode】104. 二叉树的最大深度
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • php ci框架整合银盛支付
  • Python_OOP
  • Python打包系统简单入门
  • Quartz初级教程
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • vue总结
  • 对象引论
  • 番外篇1:在Windows环境下安装JDK
  • 关于 Cirru Editor 存储格式
  • 欢迎参加第二届中国游戏开发者大会
  • 面试遇到的一些题
  • 微信小程序开发问题汇总
  • 我感觉这是史上最牛的防sql注入方法类
  • 我建了一个叫Hello World的项目
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • !!java web学习笔记(一到五)
  • #if #elif #endif
  • (03)光刻——半导体电路的绘制
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (Note)C++中的继承方式
  • (多级缓存)缓存同步
  • (附源码)ssm码农论坛 毕业设计 231126
  • (六)激光线扫描-三维重建
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (十六)串口UART
  • (四)Linux Shell编程——输入输出重定向
  • (一)Thymeleaf用法——Thymeleaf简介
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .gitignore文件_Git:.gitignore
  • .NET BackgroundWorker