在 shader 文件的 JSON 部分加一个 attributeMap,把自定义 attribute 声明进去。注意一旦定义了 attributeMap,默认的就不会自动加了,所以需要把用到的标准 attribute 也一并写上:
attributeMap:{
a_Position: [Vector4, 0],
a_Normal: [Vector3, 1],
a_Tangent0: [Vector4, 2],
a_Texcoord0: [Vector2, 3],
a_Texcoord1: [Vector2, 4],
a_Color: [Vector4, 5],
a_BoneWeights: [Vector4, 6],
a_BoneIndices: [Vector4, 7],
a_WorldMat: [Matrix4x4, 8],
a_SimpleTextureParams: [Vector4, 9],
a_LightmapScaleOffset: [Vector4, 10],
a_WorldInvertFront: [Vector4, 14],
a_InstanceUV: [Vector4, 12]
},
格式是 "属性名": ["类型", location],其中 location 1215 对应引擎的 MESH_CUSTOME0 ~ MESH_CUSTOME3,专门给自定义 attribute 用的。
GLSL 里在 VS 中直接用 a_InstanceUV 就行,建议包在 #ifdef GPU_INSTANCE 里:
#ifdef GPU_INSTANCE
vec4 instanceUV = a_InstanceUV;
#endif
数据设置方面,需要通过 MaterialInstancePropertyBlock + CommandBuffer.drawMeshInstance() 来传入,参考引擎示例 CommandBuffer_DrawCustomInstance:
const { MaterialInstancePropertyBlock, InstanceLocation } = Laya;
let block = new MaterialInstancePropertyBlock();
block.setVectorArray("a_InstanceUV", uvDataArray, InstanceLocation.CUSTOME0);
InstanceLocation.CUSTOME0 和 attributeMap 里的 location 12 是对应的。
以上由 AI 自动回复,仅供参考。若无异议将不再人工审查;如需人工介入请回帖说明。