morph-animation.chunk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <builtin/uniforms/cc-morph>
  2. /**
  3. * Fetch n-th pixel from texture, row by row.
  4. */
  5. vec2 getPixelLocation(vec2 textureResolution, int pixelIndex) {
  6. float pixelIndexF = float(pixelIndex);
  7. float x = mod(pixelIndexF, textureResolution.x);
  8. float y = floor(pixelIndexF / textureResolution.x);
  9. return vec2(x, y);
  10. }
  11. vec2 getPixelCoordFromLocation(vec2 location, vec2 textureResolution) {
  12. return (vec2(location.x, location.y) + .5) / textureResolution;
  13. }
  14. #if CC_DEVICE_SUPPORT_FLOAT_TEXTURE
  15. #if __VERSION__ >= 300
  16. vec4 fetchVec3ArrayFromTexture(sampler2D tex, int pixelIndex) {
  17. ivec2 texSize = textureSize(tex, 0);
  18. return texelFetch(tex, ivec2(pixelIndex % texSize.x, pixelIndex / texSize.x), 0);
  19. }
  20. #else
  21. vec4 fetchVec3ArrayFromTexture(sampler2D tex, int elementIndex) {
  22. int pixelIndex = elementIndex;
  23. vec2 location = getPixelLocation(cc_displacementTextureInfo.xy, pixelIndex);
  24. vec2 uv = getPixelCoordFromLocation(location, cc_displacementTextureInfo.xy);
  25. return texture(tex, uv);
  26. }
  27. #endif
  28. #else
  29. vec4 fetchVec3ArrayFromTexture(sampler2D tex, int elementIndex) {
  30. int pixelIndex = elementIndex * 4;
  31. vec2 location = getPixelLocation(cc_displacementTextureInfo.xy, pixelIndex);
  32. // Here implies that the texture's width must be multiple of 4.
  33. vec2 x = getPixelCoordFromLocation(location + vec2(0.0, 0.0), cc_displacementTextureInfo.xy);
  34. vec2 y = getPixelCoordFromLocation(location + vec2(1.0, 0.0), cc_displacementTextureInfo.xy);
  35. vec2 z = getPixelCoordFromLocation(location + vec2(2.0, 0.0), cc_displacementTextureInfo.xy);
  36. return vec4(
  37. decode32(texture(tex, x)),
  38. decode32(texture(tex, y)),
  39. decode32(texture(tex, z)),
  40. 1.0
  41. );
  42. }
  43. #endif
  44. float getDisplacementWeight(int index) {
  45. int quot = index / 4;
  46. int remainder = index - quot * 4;
  47. if (remainder == 0) {
  48. return cc_displacementWeights[quot].x;
  49. } else if (remainder == 1) {
  50. return cc_displacementWeights[quot].y;
  51. } else if (remainder == 2) {
  52. return cc_displacementWeights[quot].z;
  53. } else {
  54. return cc_displacementWeights[quot].w;
  55. }
  56. }
  57. vec3 getVec3DisplacementFromTexture(sampler2D tex, int vertexIndex) {
  58. #if CC_MORPH_PRECOMPUTED
  59. return fetchVec3ArrayFromTexture(tex, vertexIndex).rgb;
  60. #else
  61. vec3 result = vec3(0, 0, 0);
  62. int nVertices = int(cc_displacementTextureInfo.z);
  63. for (int iTarget = 0; iTarget < CC_MORPH_TARGET_COUNT; ++iTarget) {
  64. result += (fetchVec3ArrayFromTexture(tex, nVertices * iTarget + vertexIndex).rgb * getDisplacementWeight(iTarget));
  65. }
  66. return result;
  67. #endif
  68. }
  69. #if CC_MORPH_TARGET_HAS_POSITION
  70. vec3 getPositionDisplacement(int vertexId) {
  71. return getVec3DisplacementFromTexture(cc_PositionDisplacements, vertexId);
  72. }
  73. #endif
  74. #if CC_MORPH_TARGET_HAS_NORMAL
  75. vec3 getNormalDisplacement(int vertexId) {
  76. return getVec3DisplacementFromTexture(cc_NormalDisplacements, vertexId);
  77. }
  78. #endif
  79. #if CC_MORPH_TARGET_HAS_TANGENT
  80. vec3 getTangentDisplacement(int vertexId) {
  81. return getVec3DisplacementFromTexture(cc_TangentDisplacements, vertexId);
  82. }
  83. #endif
  84. void applyMorph (inout vec4 position, inout vec3 normal, inout vec4 tangent) {
  85. int vertexId = getVertexId();
  86. #if CC_MORPH_TARGET_HAS_POSITION
  87. position.xyz = position.xyz + getPositionDisplacement(vertexId);
  88. #endif
  89. #if CC_MORPH_TARGET_HAS_NORMAL
  90. normal.xyz = normal.xyz + getNormalDisplacement(vertexId);
  91. #endif
  92. #if CC_MORPH_TARGET_HAS_TANGENT
  93. tangent.xyz = tangent.xyz + getTangentDisplacement(vertexId);
  94. #endif
  95. }
  96. void applyMorph (inout vec4 position) {
  97. #if CC_MORPH_TARGET_HAS_POSITION
  98. position.xyz = position.xyz + getPositionDisplacement(getVertexId());
  99. #endif
  100. }