batched-unlit.effect 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - name: opaque
  5. passes:
  6. - vert: unlit-vs:vert
  7. frag: unlit-fs:frag
  8. properties: &props
  9. __metadata__: { editor: { visible: false } } # all the custom data will be overriden anyways, so hide all from inspector
  10. colorArr: { value: [1, 1, 1, 1], editor: { type: color } } # note the 'Arr' suffix
  11. tilingOffsetArr: { value: [1, 1, 0, 0] } # default value here is applied to every array elements
  12. mainTexture: { value: grey }
  13. - name: transparent
  14. passes:
  15. - vert: unlit-vs:vert
  16. frag: unlit-fs:frag
  17. depthStencilState:
  18. depthTest: true
  19. depthWrite: false
  20. blendState:
  21. targets:
  22. - blend: true
  23. blendSrc: src_alpha
  24. blendDst: one_minus_src_alpha
  25. blendDstAlpha: one_minus_src_alpha
  26. properties: *props
  27. }%
  28. // This is the batched reference for 'builtin-unlit.effect',
  29. // can be used in BatchedSkinningModel as the master material.
  30. CCProgram unlit-vs %{
  31. precision highp float;
  32. #include <legacy/input>
  33. #include <builtin/uniforms/cc-global>
  34. #include <legacy/decode-base>
  35. #include <legacy/local-batch>
  36. // array lengths need to be determined at resource-import-time
  37. // so we can't define this as a runtime macro
  38. #pragma define BATCHED_UNITS 6
  39. #if USE_VERTEX_COLOR
  40. in lowp vec4 a_color;
  41. out lowp vec4 v_color;
  42. #endif
  43. #if USE_TEXTURE
  44. out vec2 v_uv;
  45. uniform TexCoords {
  46. vec4 tilingOffsetArr[BATCHED_UNITS];
  47. };
  48. #endif
  49. ////////////////
  50. in float a_batch_id;
  51. out float v_batch_id;
  52. in vec2 a_batch_uv;
  53. out vec2 v_batch_uv;
  54. highp vec4 vert () {
  55. int id = int(a_batch_id);
  56. #if USE_TEXTURE
  57. vec4 tilingOffset = tilingOffsetArr[id];
  58. v_batch_uv = a_batch_uv;
  59. #endif
  60. v_batch_id = a_batch_id;
  61. ////////////////
  62. highp vec4 position;
  63. CCVertInput(position);
  64. highp mat4 matWorld;
  65. CCGetWorldMatrix(matWorld);
  66. #if USE_TEXTURE
  67. v_uv = a_texCoord;
  68. #if FLIP_UV
  69. v_uv.y = 1.0 - v_uv.y;
  70. #endif
  71. v_uv = v_uv * tilingOffset.xy + tilingOffset.zw;
  72. #endif
  73. #if USE_VERTEX_COLOR
  74. v_color = a_color;
  75. #endif
  76. return cc_matProj * (cc_matView * matWorld) * position;
  77. }
  78. }%
  79. CCProgram unlit-fs %{
  80. precision highp float;
  81. #include <legacy/output>
  82. #pragma define BATCHED_UNITS 6
  83. #if USE_TEXTURE
  84. in vec2 v_uv;
  85. uniform sampler2D mainTexture;
  86. #endif
  87. #if USE_COLOR
  88. uniform Constant {
  89. vec4 colorArr[BATCHED_UNITS];
  90. };
  91. #endif
  92. #if USE_VERTEX_COLOR
  93. in lowp vec4 v_color;
  94. #endif
  95. ////////////////
  96. in float v_batch_id;
  97. in vec2 v_batch_uv;
  98. vec4 getVec4Uniform (vec4 uniformArr[BATCHED_UNITS]) {
  99. if (v_batch_id < 1.0) { return uniformArr[0]; }
  100. else if (v_batch_id < 2.0) { return uniformArr[1]; }
  101. else if (v_batch_id < 3.0) { return uniformArr[2]; }
  102. else if (v_batch_id < 4.0) { return uniformArr[3]; }
  103. else if (v_batch_id < 5.0) { return uniformArr[4]; }
  104. else { return uniformArr[5]; }
  105. }
  106. vec4 frag () {
  107. #if USE_COLOR
  108. vec4 color = getVec4Uniform(colorArr);
  109. #endif
  110. ////////////////
  111. vec4 o = vec4(1, 1, 1, 1);
  112. #if USE_TEXTURE
  113. #if BATCH_TEXTURE // need to use batched UV if texture is specified as batchable
  114. o *= texture(mainTexture, v_batch_uv);
  115. #else
  116. o *= texture(mainTexture, v_uv);
  117. #endif
  118. #endif
  119. #if USE_COLOR
  120. o *= color;
  121. #endif
  122. #if USE_VERTEX_COLOR
  123. o *= v_color;
  124. #endif
  125. return CCFragOutput(o);
  126. }
  127. }%