当前位置:   article > 正文

[Unity] GPU动画实现(五)——渲染GPU动画_unity_vertex_input_instance_id

unity_vertex_input_instance_id

GPU动画实现的最后一步是在shader中重新进行顶点采样。

将动画数据存在一张图片上,每帧动画数据就是一行,因此每个顶点的位置由2个属性确定

1.帧数

2.顶点ID

帧数 = 当前时间 / 动画时间

顶点ID则通过UNITY_VERTEX_INPUT_INSTANCE_ID,Unity会自动将该顶点ID传入

因此,在shade中,进行如下定义,这里直接用最简单的unlit shader示例

  1. struct appdata
  2. {
  3. float4 vertex : POSITION;
  4. float2 uv : TEXCOORD0;
  5. uint vid : SV_VERTEXID;
  6. UNITY_VERTEX_INPUT_INSTANCE_ID
  7. };
  8. struct v2f
  9. {
  10. float2 uv : TEXCOORD0;
  11. UNITY_FOG_COORDS(1)
  12. float4 vertex : SV_POSITION;
  13. UNITY_VERTEX_INPUT_INSTANCE_ID
  14. };

在顶点着色器中,直接进行顶点位置修改,通过顶点ID和动画时间计算取得顶点的位置。

  1. v2f vert (appdata v)
  2. {
  3. v2f o;
  4. UNITY_SETUP_INSTANCE_ID(v);
  5. UNITY_TRANSFER_INSTANCE_ID(v, o);
  6. float4 uv_anim = float4((v.vid + 0.5) * UNITY_ACCESS_INSTANCED_PROP(Props, _AnimTex_TexelSize).x, _Time.y / UNITY_ACCESS_INSTANCED_PROP(Props, _AnimTime),0.0,0.0);
  7. v.vertex = float4(tex2Dlod(_AnimTex, uv_anim).xyz,1.0);
  8. //v.vertex.y = v.vertex.y - 0.5;
  9. o.vertex = UnityObjectToClipPos(v.vertex);
  10. o.uv = v.uv * UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex_ST).xy + UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex_ST).zw;
  11. return o;
  12. }

片元着色器不用特别处理,直接采样输出即可


着色器源码如下

  1. Properties
  2. {
  3. _MainTex ("Texture", 2D) = "white" {}
  4. _AnimTex ("Texture", 2D) = "white" {}
  5. _AnimTime ("AnimTime", float) = 1
  6. }
  7. SubShader
  8. {
  9. Tags { "RenderType"="Opaque" }
  10. LOD 100
  11. Pass
  12. {
  13. CGPROGRAM
  14. #pragma target 3.5
  15. #pragma vertex vert
  16. #pragma fragment frag
  17. // make fog work
  18. #pragma multi_compile_instancing
  19. #include "UnityCG.cginc"
  20. struct appdata
  21. {
  22. float4 vertex : POSITION;
  23. float2 uv : TEXCOORD0;
  24. uint vid : SV_VERTEXID;
  25. UNITY_VERTEX_INPUT_INSTANCE_ID
  26. };
  27. struct v2f
  28. {
  29. float2 uv : TEXCOORD0;
  30. UNITY_FOG_COORDS(1)
  31. float4 vertex : SV_POSITION;
  32. UNITY_VERTEX_INPUT_INSTANCE_ID
  33. };
  34. CBUFFER_START(UnityPerMaterial)
  35. sampler2D _MainTex;
  36. sampler2D _AnimTex;
  37. //float4 _MainTex_ST;
  38. UNITY_INSTANCING_BUFFER_START(Props)
  39. UNITY_DEFINE_INSTANCED_PROP(float4, _AnimTex_TexelSize)
  40. UNITY_DEFINE_INSTANCED_PROP(float4, _MainTex_ST)
  41. UNITY_DEFINE_INSTANCED_PROP(float, _AnimTime)
  42. UNITY_INSTANCING_BUFFER_END(Props)
  43. CBUFFER_END
  44. v2f vert (appdata v)
  45. {
  46. v2f o;
  47. UNITY_SETUP_INSTANCE_ID(v);
  48. UNITY_TRANSFER_INSTANCE_ID(v, o);
  49. float4 uv_anim = float4((v.vid + 0.5) * UNITY_ACCESS_INSTANCED_PROP(Props, _AnimTex_TexelSize).x, _Time.y / UNITY_ACCESS_INSTANCED_PROP(Props, _AnimTime),0.0,0.0);
  50. v.vertex = float4(tex2Dlod(_AnimTex, uv_anim).xyz,1.0);
  51. //v.vertex.y = v.vertex.y - 0.5;
  52. o.vertex = UnityObjectToClipPos(v.vertex);
  53. o.uv = v.uv * UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex_ST).xy + UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex_ST).zw;
  54. return o;
  55. }
  56. fixed4 frag (v2f i) : SV_Target
  57. {
  58. UNITY_SETUP_INSTANCE_ID(i);
  59. fixed4 col = tex2D(_MainTex, i.uv);
  60. //return float4(1.0,1.0,1.0,1.0) * _Time.y;
  61. return col;
  62. }
  63. ENDCG
  64. }
  65. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/94492
推荐阅读
相关标签
  

闽ICP备14008679号