ParticlesInstancing.hlsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef UNIVERSAL_PARTICLESINSTANCING_INCLUDED
  2. #define UNIVERSAL_PARTICLESINSTANCING_INCLUDED
  3. #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) && !defined(SHADER_TARGET_SURFACE_ANALYSIS)
  4. #define UNITY_PARTICLE_INSTANCING_ENABLED
  5. #endif
  6. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  7. #ifndef UNITY_PARTICLE_INSTANCE_DATA
  8. #define UNITY_PARTICLE_INSTANCE_DATA DefaultParticleInstanceData
  9. #endif
  10. struct DefaultParticleInstanceData
  11. {
  12. float3x4 transform;
  13. uint color;
  14. float animFrame;
  15. };
  16. StructuredBuffer<UNITY_PARTICLE_INSTANCE_DATA> unity_ParticleInstanceData;
  17. float4 unity_ParticleUVShiftData;
  18. half unity_ParticleUseMeshColors;
  19. void ParticleInstancingMatrices(out float4x4 objectToWorld, out float4x4 worldToObject)
  20. {
  21. UNITY_PARTICLE_INSTANCE_DATA data = unity_ParticleInstanceData[unity_InstanceID];
  22. // transform matrix
  23. objectToWorld._11_21_31_41 = float4(data.transform._11_21_31, 0.0f);
  24. objectToWorld._12_22_32_42 = float4(data.transform._12_22_32, 0.0f);
  25. objectToWorld._13_23_33_43 = float4(data.transform._13_23_33, 0.0f);
  26. objectToWorld._14_24_34_44 = float4(data.transform._14_24_34, 1.0f);
  27. // inverse transform matrix (TODO: replace with a library implementation if/when available)
  28. float3x3 worldToObject3x3;
  29. worldToObject3x3[0] = objectToWorld[1].yzx * objectToWorld[2].zxy - objectToWorld[1].zxy * objectToWorld[2].yzx;
  30. worldToObject3x3[1] = objectToWorld[0].zxy * objectToWorld[2].yzx - objectToWorld[0].yzx * objectToWorld[2].zxy;
  31. worldToObject3x3[2] = objectToWorld[0].yzx * objectToWorld[1].zxy - objectToWorld[0].zxy * objectToWorld[1].yzx;
  32. float det = dot(objectToWorld[0].xyz, worldToObject3x3[0]);
  33. worldToObject3x3 = transpose(worldToObject3x3);
  34. worldToObject3x3 *= rcp(det);
  35. float3 worldToObjectPosition = mul(worldToObject3x3, -objectToWorld._14_24_34);
  36. worldToObject._11_21_31_41 = float4(worldToObject3x3._11_21_31, 0.0f);
  37. worldToObject._12_22_32_42 = float4(worldToObject3x3._12_22_32, 0.0f);
  38. worldToObject._13_23_33_43 = float4(worldToObject3x3._13_23_33, 0.0f);
  39. worldToObject._14_24_34_44 = float4(worldToObjectPosition, 1.0f);
  40. }
  41. void ParticleInstancingSetup()
  42. {
  43. ParticleInstancingMatrices(unity_ObjectToWorld, unity_WorldToObject);
  44. }
  45. #else
  46. void ParticleInstancingSetup() {}
  47. #endif
  48. #endif // UNIVERSAL_PARTICLESINSTANCING_INCLUDED