VertexAttributeSemantic.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. /**
  5. * An enum describing the built-in vertex attribute semantics.
  6. *
  7. * @enum {string}
  8. *
  9. * @private
  10. */
  11. const VertexAttributeSemantic = {
  12. /**
  13. * Per-vertex position.
  14. *
  15. * @type {string}
  16. * @constant
  17. */
  18. POSITION: "POSITION",
  19. /**
  20. * Per-vertex normal.
  21. *
  22. * @type {string}
  23. * @constant
  24. */
  25. NORMAL: "NORMAL",
  26. /**
  27. * Per-vertex tangent.
  28. *
  29. * @type {string}
  30. * @constant
  31. */
  32. TANGENT: "TANGENT",
  33. /**
  34. * Per-vertex texture coordinates.
  35. *
  36. * @type {string}
  37. * @constant
  38. */
  39. TEXCOORD: "TEXCOORD",
  40. /**
  41. * Per-vertex color.
  42. *
  43. * @type {string}
  44. * @constant
  45. */
  46. COLOR: "COLOR",
  47. /**
  48. * Per-vertex joint IDs for skinning.
  49. *
  50. * @type {string}
  51. * @constant
  52. */
  53. JOINTS: "JOINTS",
  54. /**
  55. * Per-vertex joint weights for skinning.
  56. *
  57. * @type {string}
  58. * @constant
  59. */
  60. WEIGHTS: "WEIGHTS",
  61. /**
  62. * Per-vertex feature ID.
  63. *
  64. * @type {string}
  65. * @constant
  66. */
  67. FEATURE_ID: "_FEATURE_ID",
  68. };
  69. function semanticToVariableName(semantic) {
  70. switch (semantic) {
  71. case VertexAttributeSemantic.POSITION:
  72. return "positionMC";
  73. case VertexAttributeSemantic.NORMAL:
  74. return "normalMC";
  75. case VertexAttributeSemantic.TANGENT:
  76. return "tangentMC";
  77. case VertexAttributeSemantic.TEXCOORD:
  78. return "texCoord";
  79. case VertexAttributeSemantic.COLOR:
  80. return "color";
  81. case VertexAttributeSemantic.JOINTS:
  82. return "joints";
  83. case VertexAttributeSemantic.WEIGHTS:
  84. return "weights";
  85. case VertexAttributeSemantic.FEATURE_ID:
  86. return "featureId";
  87. //>>includeStart('debug', pragmas.debug);
  88. default:
  89. throw new DeveloperError("semantic is not a valid value.");
  90. //>>includeEnd('debug');
  91. }
  92. }
  93. /**
  94. * Returns whether the vertex attribute semantic can have a set index.
  95. *
  96. * @param {VertexAttributeSemantic} semantic The semantic.
  97. *
  98. * @returns {boolean} Whether the semantic can have a set index.
  99. *
  100. * @private
  101. */
  102. VertexAttributeSemantic.hasSetIndex = function (semantic) {
  103. //>>includeStart('debug', pragmas.debug);
  104. Check.typeOf.string("semantic", semantic);
  105. //>>includeEnd('debug');
  106. switch (semantic) {
  107. case VertexAttributeSemantic.POSITION:
  108. case VertexAttributeSemantic.NORMAL:
  109. case VertexAttributeSemantic.TANGENT:
  110. return false;
  111. case VertexAttributeSemantic.TEXCOORD:
  112. case VertexAttributeSemantic.COLOR:
  113. case VertexAttributeSemantic.JOINTS:
  114. case VertexAttributeSemantic.WEIGHTS:
  115. case VertexAttributeSemantic.FEATURE_ID:
  116. return true;
  117. //>>includeStart('debug', pragmas.debug);
  118. default:
  119. throw new DeveloperError("semantic is not a valid value.");
  120. //>>includeEnd('debug');
  121. }
  122. };
  123. /**
  124. * Gets the vertex attribute semantic matching the glTF semantic.
  125. *
  126. * @param {string} gltfSemantic The glTF semantic.
  127. *
  128. * @returns {VertexAttributeSemantic|undefined} The vertex attribute semantic, or undefined if there is no match.
  129. *
  130. * @private
  131. */
  132. VertexAttributeSemantic.fromGltfSemantic = function (gltfSemantic) {
  133. //>>includeStart('debug', pragmas.debug);
  134. Check.typeOf.string("gltfSemantic", gltfSemantic);
  135. //>>includeEnd('debug');
  136. let semantic = gltfSemantic;
  137. // Strip the set index from the semantic
  138. const setIndexRegex = /^(\w+)_\d+$/;
  139. const setIndexMatch = setIndexRegex.exec(gltfSemantic);
  140. if (setIndexMatch !== null) {
  141. semantic = setIndexMatch[1];
  142. }
  143. switch (semantic) {
  144. case "POSITION":
  145. return VertexAttributeSemantic.POSITION;
  146. case "NORMAL":
  147. return VertexAttributeSemantic.NORMAL;
  148. case "TANGENT":
  149. return VertexAttributeSemantic.TANGENT;
  150. case "TEXCOORD":
  151. return VertexAttributeSemantic.TEXCOORD;
  152. case "COLOR":
  153. return VertexAttributeSemantic.COLOR;
  154. case "JOINTS":
  155. return VertexAttributeSemantic.JOINTS;
  156. case "WEIGHTS":
  157. return VertexAttributeSemantic.WEIGHTS;
  158. case "_FEATURE_ID":
  159. return VertexAttributeSemantic.FEATURE_ID;
  160. }
  161. return undefined;
  162. };
  163. /**
  164. * Gets the vertex attribute semantic matching the pnts semantic.
  165. *
  166. * @param {string} pntsSemantic The pnts semantic.
  167. *
  168. * @returns {VertexAttributeSemantic|undefined} The vertex attribute semantic, or undefined if there is no match.
  169. *
  170. * @private
  171. */
  172. VertexAttributeSemantic.fromPntsSemantic = function (pntsSemantic) {
  173. //>>includeStart('debug', pragmas.debug);
  174. Check.typeOf.string("pntsSemantic", pntsSemantic);
  175. //>>includeEnd('debug');
  176. switch (pntsSemantic) {
  177. case "POSITION":
  178. case "POSITION_QUANTIZED":
  179. return VertexAttributeSemantic.POSITION;
  180. case "RGBA":
  181. case "RGB":
  182. case "RGB565":
  183. return VertexAttributeSemantic.COLOR;
  184. case "NORMAL":
  185. case "NORMAL_OCT16P":
  186. return VertexAttributeSemantic.NORMAL;
  187. case "BATCH_ID":
  188. return VertexAttributeSemantic.FEATURE_ID;
  189. //>>includeStart('debug', pragmas.debug);
  190. default:
  191. throw new DeveloperError("pntsSemantic is not a valid value.");
  192. //>>includeEnd('debug');
  193. }
  194. };
  195. /**
  196. * Gets the GLSL type (such as <code>vec3</code> or <code>int</code>) for the
  197. * given vertex attribute.
  198. *
  199. * @param {VertexAttributeSemantic} semantic The semantic.
  200. *
  201. * @returns {string} The shader type.
  202. *
  203. * @private
  204. */
  205. VertexAttributeSemantic.getGlslType = function (semantic) {
  206. //>>includeStart('debug', pragmas.debug);
  207. Check.typeOf.string("semantic", semantic);
  208. //>>includeEnd('debug');
  209. switch (semantic) {
  210. case VertexAttributeSemantic.POSITION:
  211. case VertexAttributeSemantic.NORMAL:
  212. case VertexAttributeSemantic.TANGENT:
  213. return "vec3";
  214. case VertexAttributeSemantic.TEXCOORD:
  215. return "vec2";
  216. case VertexAttributeSemantic.COLOR:
  217. return "vec4";
  218. case VertexAttributeSemantic.JOINTS:
  219. return "ivec4";
  220. case VertexAttributeSemantic.WEIGHTS:
  221. return "vec4";
  222. case VertexAttributeSemantic.FEATURE_ID:
  223. return "int";
  224. //>>includeStart('debug', pragmas.debug);
  225. default:
  226. throw new DeveloperError("semantic is not a valid value.");
  227. //>>includeEnd('debug');
  228. }
  229. };
  230. /**
  231. * Gets the variable name for the given semantic and set index.
  232. *
  233. * @param {VertexAttributeSemantic} semantic The semantic.
  234. * @param {number} [setIndex] The set index.
  235. *
  236. * @returns {string} The variable name.
  237. *
  238. * @private
  239. */
  240. VertexAttributeSemantic.getVariableName = function (semantic, setIndex) {
  241. //>>includeStart('debug', pragmas.debug);
  242. Check.typeOf.string("semantic", semantic);
  243. //>>includeEnd('debug');
  244. let variableName = semanticToVariableName(semantic);
  245. if (defined(setIndex)) {
  246. variableName += `_${setIndex}`;
  247. }
  248. return variableName;
  249. };
  250. export default Object.freeze(VertexAttributeSemantic);