添加依赖,转utf8
This commit is contained in:
@@ -6,7 +6,8 @@
|
||||
"Unity.RenderPipelines.Universal.Runtime",
|
||||
"Unity.RenderPipelines.Universal.Editor",
|
||||
"Unity.RenderPipelines.Core.Runtime",
|
||||
"Unity.RenderPipelines.Core.Editor"
|
||||
"Unity.RenderPipelines.Core.Editor",
|
||||
"Lrss3.Deconstruction"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"Unity.RenderPipelines.Core.Runtime",
|
||||
"Unity.RenderPipelines.Universal.Runtime"
|
||||
"Unity.RenderPipelines.Universal.Runtime",
|
||||
"Lrss3.Deconstruction"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
||||
@@ -2,7 +2,7 @@ Shader "XericLibrary/ScreenDectection/MeshPicking"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
// 传入材质ID
|
||||
// 传入材质ID
|
||||
_MatID ("Material ID", Int) = 0
|
||||
}
|
||||
SubShader
|
||||
@@ -12,7 +12,7 @@ Shader "XericLibrary/ScreenDectection/MeshPicking"
|
||||
|
||||
Pass
|
||||
{
|
||||
// 关闭光照和雾效,纯数据输出
|
||||
// 关闭光照和雾效,纯数据输出
|
||||
Lighting Off
|
||||
Fog { Mode Off }
|
||||
|
||||
@@ -29,31 +29,31 @@ Shader "XericLibrary/ScreenDectection/MeshPicking"
|
||||
struct v2f
|
||||
{
|
||||
float4 vertex : SV_POSITION;
|
||||
float3 worldPos : TEXCOORD0; // 传递世界坐标给片段着色器
|
||||
float3 worldPos : TEXCOORD0; // 传递世界坐标给片段着色器
|
||||
};
|
||||
|
||||
int _MatID; // 接收材质ID
|
||||
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
|
||||
// 1. 将世界坐标编码到 RGB 通道
|
||||
// 假设你的地质模型世界坐标范围在 -1000 到 1000 之间
|
||||
// 加上 1000 变成 0~2000,再除以 2000 变成 0~1
|
||||
float3 encodedPos = (i.worldPos + 1000.0) / 2000.0;
|
||||
|
||||
// 2. 将材质ID编码到 Alpha 通道 (假设ID不超过255)
|
||||
// 2. 将材质ID编码到 Alpha 通道 (假设ID不超过255)
|
||||
float encodedID = _MatID / 255.0;
|
||||
|
||||
// 输出 RGBA = (PosX, PosY, PosZ, MatID)
|
||||
// 输出 RGBA = (PosX, PosY, PosZ, MatID)
|
||||
return fixed4(encodedPos.x, encodedPos.y, encodedPos.z, encodedID);
|
||||
}
|
||||
ENDCG
|
||||
|
||||
@@ -3,10 +3,10 @@ using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
#if UNITY_2022_1_OR_NEWER // urp 13
|
||||
// #if UNITY_2022_1_OR_NEWER // urp 13
|
||||
namespace XericLibrary.PostProcess
|
||||
{
|
||||
// 定义数据结构,用来存储展平后的 SubMesh 信息
|
||||
// 定义数据结构,用来存储展平后的 SubMesh 信息
|
||||
[System.Serializable]
|
||||
public struct SubMeshDrawData
|
||||
{
|
||||
@@ -19,13 +19,13 @@ namespace XericLibrary.PostProcess
|
||||
public class GeoLayerPickerRenderPassFeature : ScriptableRendererFeature
|
||||
{
|
||||
|
||||
// 在 Feature 外部类中,添加 Inspector 面板需要的配置参数
|
||||
[Header("拾取设置")]
|
||||
// 在 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>();
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace XericLibrary.PostProcess
|
||||
|
||||
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
|
||||
{
|
||||
// 在 URP 中,必须在这里告诉管线我们要渲染到哪张 RT 上
|
||||
// 在 URP 中,必须在这里告诉管线我们要渲染到哪张 RT 上
|
||||
if (passRT != null)
|
||||
{
|
||||
ConfigureTarget(new RenderTargetIdentifier(passRT));
|
||||
@@ -49,27 +49,27 @@ namespace XericLibrary.PostProcess
|
||||
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
// 安全检查
|
||||
// 安全检查
|
||||
if (passMaterial == null || passRT == null || passDrawList == null || passDrawList.Count == 0)
|
||||
return;
|
||||
|
||||
// 从对象池获取 CommandBuffer (性能更好)
|
||||
// 从对象池获取 CommandBuffer (性能更好)
|
||||
CommandBuffer cmd = CommandBufferPool.Get("GeoLayerPickBuffer");
|
||||
|
||||
// 在 URP 13 中,绝对不能在 Execute 里用 cmd.SetRenderTarget()。必须重写 OnCameraSetup,使用 ConfigureTarget() 告诉 URP 这个 Pass 的输出目标是你创建的 m_PickRT。
|
||||
// 设置视口为 RT 的尺寸 (64x64)
|
||||
// 在 URP 13 中,绝对不能在 Execute 里用 cmd.SetRenderTarget()。必须重写 OnCameraSetup,使用 ConfigureTarget() 告诉 URP 这个 Pass 的输出目标是你创建的 m_PickRT。
|
||||
// 设置视口为 RT 的尺寸 (64x64)
|
||||
cmd.SetViewport(new Rect(0, 0, passRT.width, passRT.height));
|
||||
|
||||
// 【核心】遍历绘制每一个 SubMesh
|
||||
// 【核心】遍历绘制每一个 SubMesh
|
||||
for (int i = 0; i < passDrawList.Count; i++)
|
||||
{
|
||||
var data = passDrawList[i];
|
||||
cmd.SetGlobalInt(m_MatIDProperty, data.uniqueMatID);
|
||||
// 注意第四个参数 submeshIndex,这是多材质插槽的关键
|
||||
// 注意第四个参数 submeshIndex,这是多材质插槽的关键
|
||||
cmd.DrawMesh(data.mesh, data.matrix, passMaterial, data.subMeshIndex);
|
||||
}
|
||||
|
||||
// 提交命令给 GPU 执行
|
||||
// 提交命令给 GPU 执行
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
@@ -87,7 +87,7 @@ namespace XericLibrary.PostProcess
|
||||
|
||||
m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
|
||||
|
||||
// 【步骤 4】在 Create 中初始化 RT,并展平 SubMesh 数据
|
||||
// 【步骤 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();
|
||||
@@ -96,19 +96,19 @@ namespace XericLibrary.PostProcess
|
||||
}
|
||||
|
||||
|
||||
// 提供一个公开方法,方便运行时动态刷新网格列表
|
||||
// 提供一个公开方法,方便运行时动态刷新网格列表
|
||||
public void RebuildDrawList()
|
||||
{
|
||||
m_DrawList.Clear();
|
||||
if (geoMeshes == null)
|
||||
return;
|
||||
|
||||
int globalIDCounter = 1; // ID 从 1 开始,0 留给背景
|
||||
int globalIDCounter = 1; // ID 从 1 开始,0 留给背景
|
||||
foreach (var mf in geoMeshes)
|
||||
{
|
||||
if (mf == null || mf.sharedMesh == null) continue;
|
||||
|
||||
// 遍历网格的每一个材质插槽 (SubMesh)
|
||||
// 遍历网格的每一个材质插槽 (SubMesh)
|
||||
for (int sub = 0; sub < mf.sharedMesh.subMeshCount; sub++)
|
||||
{
|
||||
m_DrawList.Add(new SubMeshDrawData
|
||||
@@ -124,7 +124,7 @@ namespace XericLibrary.PostProcess
|
||||
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
// 将外部数据传递给内部的 Pass,并注入管线
|
||||
// 将外部数据传递给内部的 Pass,并注入管线
|
||||
if (m_ScriptablePass != null)
|
||||
{
|
||||
m_ScriptablePass.passMaterial = pickMaterial;
|
||||
@@ -136,8 +136,8 @@ namespace XericLibrary.PostProcess
|
||||
}
|
||||
|
||||
|
||||
// 暴露 RT 给外部读取像素用
|
||||
// 暴露 RT 给外部读取像素用
|
||||
public RenderTexture GetPickRT() => m_PickRT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// #endif
|
||||
Reference in New Issue
Block a user