初始化后处理阶段屏幕处理器
This commit is contained in:
@@ -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:
|
||||
Reference in New Issue
Block a user