最近项目需要用到DOTS的技术,发现除了程序部分需要做代码调整外,Shader的编写也需要进行一些调整,不然运行的时候会出些各种奇怪的现象,比如不显示模型,模型位置错乱等等。
由于也在慢慢摸索,可能有部分写的不对的地方,请各位大佬指教。。。
支持DOTS的Shader分为带骨骼的SkinnedMeshedRender和普通的MeshedRender,普通的MeshedRender实现的方式和普通的GPUInstancing类似,注意是必须增加Instance。
增加变体:
#pragma multi_compile _ DOTS_INSTANCING_ON
#pragma target 4.5
顶点着色器输入结构体增加:
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : INSTANCEID_SEMANTIC;
#endif
顶点着色器输出结构体增加:
#if UNITY_ANY_INSTANCING_ENABLED
uint instanceID : CUSTOM_INSTANCE_ID;
#endif
顶点着色器:
#if UNITY_ANY_INSTANCING_ENABLED
o.instanceID = v.instanceID;
#endif
对于蒙皮对象的Shader,需要另外处理:
//Properties加入相关
[HideInInspector]_ComputeMeshIndex("Compute Mesh Buffer Index Offset", Float) = 0
//加入变体
//DOTS
#pragma multi_compile _ DOTS_INSTANCING_ON
#pragma target 4.5
//加入方法和结构//Dots
#if defined(UNITY_DOTS_INSTANCING_ENABLED)
// DOTS instancing definitions
UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
UNITY_DOTS_INSTANCED_PROP(float, _ComputeMeshIndex)
UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)
// DOTS instancing usage macros
// This_macro_cannot_be_called_without_UNITY_DOTS_INSTANCING_ENABLED
#define _ComputeMeshIndex UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float, Metadata__ComputeMeshIndex)
#endif
// Object and Global properties
// Graph Functions
struct DeformedVertexData
{
float3 Position;
float3 Normal;
float3 Tangent;
};
uniform StructuredBuffer<DeformedVertexData> _DeformedMeshData : register(t1);
void Unity_ComputeDeformedVertex_float(uint vertexID, out float3 positionOut, out float3 normalOut, out float3 tangentOut)
{
const DeformedVertexData vertexData = _DeformedMeshData[asuint(_ComputeMeshIndex) + vertexID];
positionOut = vertexData.Position;
normalOut = vertexData.Normal;
tangentOut = vertexData.Tangent;
}
//Attributes中加入
//DOTS
uint vertexID : SV_VertexID;
///
//Varyings中加入
//dots
#if defined(UNITY_DOTS_INSTANCING_ENABLED)
float3 SkinnedPosition = 0;
float3 SkinnedNormal = 0;
float3 SkinnedTangent = 0;
Unity_ComputeDeformedVertex_float(v.vertexID, SkinnedPosition, SkinnedNormal, SkinnedTangent);
#else
float3 SkinnedPosition = v.positionOS.xyz;
float3 SkinnedNormal = v.normalOS;
float3 SkinnedTangent = v.tangentOS.xyz;
#endif
v.positionOS = float4(SkinnedPosition,v.positionOS.z);
v.normalOS = SkinnedNormal;
v.tangentOS = float4(SkinnedTangent , v.tangentOS.z);
///