初始化后处理阶段屏幕处理器
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
# Changelog
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
* built-in 阶段的简易处理器
|
||||
* URP 阶段的处理器
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c252a65d20865247a6d3cc2ba1f0075
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d673101928967242bf6cdfa9cb0ccc2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6760f91188a394147b91452bfe315216
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "Lrss3.ScreendetectiongrolayerpickerUrp.Editor",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"Lrss3.ScreendetectiongrolayerpickerUrp",
|
||||
"Unity.RenderPipelines.Universal.Runtime",
|
||||
"Unity.RenderPipelines.Universal.Editor",
|
||||
"Unity.RenderPipelines.Core.Runtime",
|
||||
"Unity.RenderPipelines.Core.Editor"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 655ba29a1fbde0447bf386c055e931eb
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: abe80e4311f5be343adfcf420b846c0a
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a825232565e66c0469f297525ad1e8cb
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "Lrss3.ScreendetectiongrolayerpickerUrp",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"Unity.RenderPipelines.Core.Runtime",
|
||||
"Unity.RenderPipelines.Universal.Runtime"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bbdbb3efc0f815a488c85c3dcb7a6151
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1ea6370245890746869db987244b160
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using XericLibrary.Runtime.Type;
|
||||
|
||||
namespace Deconstruction.ScreenDetection.MeshDetection
|
||||
{
|
||||
/// <summary>
|
||||
/// 在屏幕上截取模型的表面属性
|
||||
///
|
||||
/// </summary>
|
||||
public class GeoLayerPicker : MonoBehaviour
|
||||
{
|
||||
public Camera targetCamera;
|
||||
public Material pickMaterial;
|
||||
public MeshFilter[] geoMeshes; // 外部只需拖入地质层网格
|
||||
|
||||
public Vector2Int pickRTSize = new Vector2Int(64, 64);
|
||||
public Bool2 pickRTAdapt = new Bool2(false, true);
|
||||
|
||||
private RenderTexture pickRT;
|
||||
private Texture2D readTex;
|
||||
private CommandBuffer cmd;
|
||||
private float nextCheckTime;
|
||||
|
||||
private static readonly int MatID = Shader.PropertyToID("_MatID");
|
||||
|
||||
// 【核心】缓存展平后的绘制数据
|
||||
private struct SubMeshDrawData
|
||||
{
|
||||
public Mesh mesh;
|
||||
public Matrix4x4 matrix;
|
||||
public int subMeshIndex;
|
||||
public int uniqueMatID; // 分配给这个插槽的唯一ID
|
||||
}
|
||||
|
||||
private List<SubMeshDrawData> drawList = new List<SubMeshDrawData>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
// 1. 展平所有网格的 SubMesh
|
||||
if (geoMeshes != null)
|
||||
{
|
||||
int globalIDCounter = 1; // ID从1开始,0留给背景
|
||||
foreach (var mf in geoMeshes)
|
||||
{
|
||||
if (mf == null || mf.sharedMesh == null) continue;
|
||||
|
||||
Mesh mesh = mf.sharedMesh;
|
||||
Matrix4x4 matrix = mf.transform.localToWorldMatrix;
|
||||
|
||||
// 遍历这个网格上的每一个材质插槽
|
||||
for (int sub = 0; sub < mesh.subMeshCount; sub++)
|
||||
{
|
||||
drawList.Add(new SubMeshDrawData
|
||||
{
|
||||
mesh = mesh,
|
||||
matrix = matrix,
|
||||
subMeshIndex = sub,
|
||||
uniqueMatID = globalIDCounter++
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 初始化 RT 和 CommandBuffer
|
||||
pickRT = new RenderTexture(pickRTSize.x, pickRTSize.y, 0, RenderTextureFormat.ARGB32);
|
||||
pickRT.Create();
|
||||
readTex = new Texture2D(pickRTSize.x, pickRTSize.y, TextureFormat.RGBA32, false);
|
||||
|
||||
cmd = new CommandBuffer();
|
||||
cmd.name = "GeoLayerPickBuffer";
|
||||
targetCamera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, cmd);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// if (Time.time < nextCheckTime) return;
|
||||
// nextCheckTime = Time.time + 0.2f;
|
||||
|
||||
cmd.Clear();
|
||||
|
||||
// 视口设置
|
||||
var screenSize = new Vector2(Screen.width, Screen.height);
|
||||
var screenStart = (screenSize - pickRTSize) / 2;
|
||||
Rect pickRect = new Rect(pickRTAdapt.X * screenStart.x, pickRTAdapt.Y * screenStart.y, pickRTAdapt.x ? Screen.width : pickRTSize.x,
|
||||
pickRTAdapt.x ? Screen.height : pickRTSize.y);
|
||||
cmd.SetViewport(pickRect);
|
||||
cmd.SetRenderTarget(pickRT);
|
||||
cmd.ClearRenderTarget(true, true, Color.clear);
|
||||
|
||||
// 【核心】遍历展平后的 SubMesh 列表进行绘制
|
||||
for (int i = 0; i < drawList.Count; i++)
|
||||
{
|
||||
var data = drawList[i];
|
||||
pickMaterial.SetInt(MatID, data.uniqueMatID);
|
||||
|
||||
// 使用带 submeshIndex 参数的 DrawMesh 重载
|
||||
cmd.DrawMesh(data.mesh, data.matrix, pickMaterial, data.subMeshIndex);
|
||||
}
|
||||
|
||||
// 读取像素
|
||||
RenderTexture.active = pickRT;
|
||||
readTex.ReadPixels(new Rect(0, 0, pickRTSize.x, pickRTSize.y), 0, 0);
|
||||
readTex.Apply();
|
||||
RenderTexture.active = null;
|
||||
|
||||
ProcessPickedData();
|
||||
}
|
||||
|
||||
void ProcessPickedData()
|
||||
{
|
||||
Color32[] pixels = readTex.GetPixels32();
|
||||
// 解码 pixels 中的 uniqueMatID 和 WorldPos
|
||||
// 根据 uniqueMatID 反查地质层信息并更新 UI...
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04e13623766d409e9c979b15693bb2dd
|
||||
timeCreated: 1780626411
|
||||
@@ -0,0 +1,62 @@
|
||||
Shader "XericLibrary/ScreenDectection/MeshPicking"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
// 传入材质ID
|
||||
_MatID ("Material ID", Int) = 0
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Opaque" }
|
||||
LOD 100
|
||||
|
||||
Pass
|
||||
{
|
||||
// 关闭光照和雾效,纯数据输出
|
||||
Lighting Off
|
||||
Fog { Mode Off }
|
||||
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 vertex : SV_POSITION;
|
||||
float3 worldPos : TEXCOORD0; // 传递世界坐标给片段着色器
|
||||
};
|
||||
|
||||
int _MatID; // 接收材质ID
|
||||
|
||||
v2f vert (appdata v)
|
||||
{
|
||||
v2f o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
// 计算世界坐标并传递
|
||||
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
|
||||
return o;
|
||||
}
|
||||
|
||||
fixed4 frag (v2f i) : SV_Target
|
||||
{
|
||||
// 1. 将世界坐标编码到 RGB 通道
|
||||
// 假设你的地质模型世界坐标范围在 -1000 到 1000 之间
|
||||
// 加上 1000 变成 0~2000,再除以 2000 变成 0~1
|
||||
float3 encodedPos = (i.worldPos + 1000.0) / 2000.0;
|
||||
|
||||
// 2. 将材质ID编码到 Alpha 通道 (假设ID不超过255)
|
||||
float encodedID = _MatID / 255.0;
|
||||
|
||||
// 输出 RGBA = (PosX, PosY, PosZ, MatID)
|
||||
return fixed4(encodedPos.x, encodedPos.y, encodedPos.z, encodedID);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 624bb1fb3e89c5342ba64aec70bfd6af
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!21 &2100000
|
||||
Material:
|
||||
serializedVersion: 8
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: XMeshPicking
|
||||
m_Shader: {fileID: 4800000, guid: 624bb1fb3e89c5342ba64aec70bfd6af, type: 3}
|
||||
m_Parent: {fileID: 0}
|
||||
m_ModifiedSerializedProperties: 0
|
||||
m_ValidKeywords: []
|
||||
m_InvalidKeywords: []
|
||||
m_LightmapFlags: 4
|
||||
m_EnableInstancingVariants: 0
|
||||
m_DoubleSidedGI: 0
|
||||
m_CustomRenderQueue: -1
|
||||
stringTagMap: {}
|
||||
disabledShaderPasses: []
|
||||
m_LockedProperties:
|
||||
m_SavedProperties:
|
||||
serializedVersion: 3
|
||||
m_TexEnvs: []
|
||||
m_Ints: []
|
||||
m_Floats:
|
||||
- _MatID: 73
|
||||
m_Colors: []
|
||||
m_BuildTextureStacks: []
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed648077cd0dc06418a72fd06e15af7b
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ca912e2c263eab47b131fc429db0a39
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,143 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
#if UNITY_2022_1_OR_NEWER // urp 13
|
||||
namespace XericLibrary.PostProcess
|
||||
{
|
||||
// 定义数据结构,用来存储展平后的 SubMesh 信息
|
||||
[System.Serializable]
|
||||
public struct SubMeshDrawData
|
||||
{
|
||||
public Mesh mesh;
|
||||
public Matrix4x4 matrix;
|
||||
public int subMeshIndex;
|
||||
public int uniqueMatID;
|
||||
}
|
||||
|
||||
public class GeoLayerPickerRenderPassFeature : ScriptableRendererFeature
|
||||
{
|
||||
|
||||
// 在 Feature 外部类中,添加 Inspector 面板需要的配置参数
|
||||
[Header("拾取设置")]
|
||||
public Material pickMaterial;
|
||||
public MeshFilter[] geoMeshes;
|
||||
public Vector2Int pickRTSize = new Vector2Int(64, 64);
|
||||
|
||||
// 内部持有的资源
|
||||
private RenderTexture m_PickRT;
|
||||
private List<SubMeshDrawData> m_DrawList = new List<SubMeshDrawData>();
|
||||
|
||||
class GeoLayerPickerRenderPass : ScriptableRenderPass
|
||||
{
|
||||
public Material passMaterial;
|
||||
public RenderTexture passRT;
|
||||
public List<SubMeshDrawData> passDrawList;
|
||||
|
||||
private int m_MatIDProperty = Shader.PropertyToID("_MatID");
|
||||
|
||||
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
|
||||
{
|
||||
// 在 URP 中,必须在这里告诉管线我们要渲染到哪张 RT 上
|
||||
if (passRT != null)
|
||||
{
|
||||
ConfigureTarget(new RenderTargetIdentifier(passRT));
|
||||
ConfigureClear(ClearFlag.All, Color.clear);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
// 安全检查
|
||||
if (passMaterial == null || passRT == null || passDrawList == null || passDrawList.Count == 0)
|
||||
return;
|
||||
|
||||
// 从对象池获取 CommandBuffer (性能更好)
|
||||
CommandBuffer cmd = CommandBufferPool.Get("GeoLayerPickBuffer");
|
||||
|
||||
// 在 URP 13 中,绝对不能在 Execute 里用 cmd.SetRenderTarget()。必须重写 OnCameraSetup,使用 ConfigureTarget() 告诉 URP 这个 Pass 的输出目标是你创建的 m_PickRT。
|
||||
// 设置视口为 RT 的尺寸 (64x64)
|
||||
cmd.SetViewport(new Rect(0, 0, passRT.width, passRT.height));
|
||||
|
||||
// 【核心】遍历绘制每一个 SubMesh
|
||||
for (int i = 0; i < passDrawList.Count; i++)
|
||||
{
|
||||
var data = passDrawList[i];
|
||||
cmd.SetGlobalInt(m_MatIDProperty, data.uniqueMatID);
|
||||
// 注意第四个参数 submeshIndex,这是多材质插槽的关键
|
||||
cmd.DrawMesh(data.mesh, data.matrix, passMaterial, data.subMeshIndex);
|
||||
}
|
||||
|
||||
// 提交命令给 GPU 执行
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
|
||||
public override void OnCameraCleanup(CommandBuffer cmd)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
GeoLayerPickerRenderPass m_ScriptablePass;
|
||||
|
||||
public override void Create()
|
||||
{
|
||||
m_ScriptablePass = new GeoLayerPickerRenderPass();
|
||||
|
||||
m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
|
||||
|
||||
// 【步骤 4】在 Create 中初始化 RT,并展平 SubMesh 数据
|
||||
if (m_PickRT != null) m_PickRT.Release();
|
||||
m_PickRT = new RenderTexture(pickRTSize.x, pickRTSize.y, 0, RenderTextureFormat.ARGB32);
|
||||
m_PickRT.Create();
|
||||
|
||||
RebuildDrawList();
|
||||
}
|
||||
|
||||
|
||||
// 提供一个公开方法,方便运行时动态刷新网格列表
|
||||
public void RebuildDrawList()
|
||||
{
|
||||
m_DrawList.Clear();
|
||||
if (geoMeshes == null)
|
||||
return;
|
||||
|
||||
int globalIDCounter = 1; // ID 从 1 开始,0 留给背景
|
||||
foreach (var mf in geoMeshes)
|
||||
{
|
||||
if (mf == null || mf.sharedMesh == null) continue;
|
||||
|
||||
// 遍历网格的每一个材质插槽 (SubMesh)
|
||||
for (int sub = 0; sub < mf.sharedMesh.subMeshCount; sub++)
|
||||
{
|
||||
m_DrawList.Add(new SubMeshDrawData
|
||||
{
|
||||
mesh = mf.sharedMesh,
|
||||
matrix = mf.transform.localToWorldMatrix,
|
||||
subMeshIndex = sub,
|
||||
uniqueMatID = globalIDCounter++
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
// 将外部数据传递给内部的 Pass,并注入管线
|
||||
if (m_ScriptablePass != null)
|
||||
{
|
||||
m_ScriptablePass.passMaterial = pickMaterial;
|
||||
m_ScriptablePass.passRT = m_PickRT;
|
||||
m_ScriptablePass.passDrawList = m_DrawList;
|
||||
|
||||
renderer.EnqueuePass(m_ScriptablePass);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 暴露 RT 给外部读取像素用
|
||||
public RenderTexture GetPickRT() => m_PickRT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 743401b70b599714a91857602199159a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "com.lrss3.screendetectiongrolayerpicker-urp",
|
||||
"displayName": "ScreenDetectionGroLayerPicker-URP",
|
||||
"version": "0.1.0",
|
||||
"unity": "2022.1",
|
||||
"description": "\u6293\u53d6\u5c4f\u5e55\u4e2d\u770b\u5230\u7684\u6a21\u578b\u4fe1\u606f"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4f64761718f61141ba89ea791fe26e0
|
||||
PackageManifestImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user