智慧水务管理系统 - 精河县供水工程综合管理平台

VertexAttributeSemantic.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. * Gaussian Splat Scale
  70. *
  71. * @type {string}
  72. * @constant
  73. */
  74. SCALE: "KHR_gaussian_splatting:SCALE",
  75. /**
  76. * Gaussian Splat Rotation
  77. *
  78. * @type {string}
  79. * @constant
  80. */
  81. ROTATION: "KHR_gaussian_splatting:ROTATION",
  82. /**
  83. * Per-vertex cumulative distance for line patterning.
  84. *
  85. * @type {string}
  86. * @constant
  87. */
  88. CUMULATIVE_DISTANCE: "BENTLEY_materials_line_style:CUMULATIVE_DISTANCE",
  89. };
  90. function semanticToVariableName(semantic) {
  91. switch (semantic) {
  92. case VertexAttributeSemantic.POSITION:
  93. return "positionMC";
  94. case VertexAttributeSemantic.NORMAL:
  95. return "normalMC";
  96. case VertexAttributeSemantic.TANGENT:
  97. return "tangentMC";
  98. case VertexAttributeSemantic.TEXCOORD:
  99. return "texCoord";
  100. case VertexAttributeSemantic.COLOR:
  101. return "color";
  102. case VertexAttributeSemantic.JOINTS:
  103. return "joints";
  104. case VertexAttributeSemantic.WEIGHTS:
  105. return "weights";
  106. case VertexAttributeSemantic.FEATURE_ID:
  107. return "featureId";
  108. case VertexAttributeSemantic.SCALE:
  109. return "scale";
  110. case VertexAttributeSemantic.ROTATION:
  111. return "rotation";
  112. case VertexAttributeSemantic.CUMULATIVE_DISTANCE:
  113. return "cumulativeDistance";
  114. //>>includeStart('debug', pragmas.debug);
  115. default:
  116. throw new DeveloperError("semantic is not a valid value.");
  117. //>>includeEnd('debug');
  118. }
  119. }
  120. /**
  121. * Returns whether the vertex attribute semantic can have a set index.
  122. *
  123. * @param {VertexAttributeSemantic} semantic The semantic.
  124. *
  125. * @returns {boolean} Whether the semantic can have a set index.
  126. *
  127. * @private
  128. */
  129. VertexAttributeSemantic.hasSetIndex = function (semantic) {
  130. //>>includeStart('debug', pragmas.debug);
  131. Check.typeOf.string("semantic", semantic);
  132. //>>includeEnd('debug');
  133. switch (semantic) {
  134. case VertexAttributeSemantic.POSITION:
  135. case VertexAttributeSemantic.NORMAL:
  136. case VertexAttributeSemantic.TANGENT:
  137. case VertexAttributeSemantic.CUMULATIVE_DISTANCE:
  138. return false;
  139. case VertexAttributeSemantic.TEXCOORD:
  140. case VertexAttributeSemantic.COLOR:
  141. case VertexAttributeSemantic.JOINTS:
  142. case VertexAttributeSemantic.WEIGHTS:
  143. case VertexAttributeSemantic.FEATURE_ID:
  144. case VertexAttributeSemantic.SCALE:
  145. case VertexAttributeSemantic.ROTATION:
  146. return true;
  147. //>>includeStart('debug', pragmas.debug);
  148. default:
  149. throw new DeveloperError("semantic is not a valid value.");
  150. //>>includeEnd('debug');
  151. }
  152. };
  153. /**
  154. * Gets the vertex attribute semantic matching the glTF semantic.
  155. *
  156. * @param {string} gltfSemantic The glTF semantic.
  157. *
  158. * @returns {VertexAttributeSemantic|undefined} The vertex attribute semantic, or undefined if there is no match.
  159. *
  160. * @private
  161. */
  162. VertexAttributeSemantic.fromGltfSemantic = function (gltfSemantic) {
  163. //>>includeStart('debug', pragmas.debug);
  164. Check.typeOf.string("gltfSemantic", gltfSemantic);
  165. //>>includeEnd('debug');
  166. let semantic = gltfSemantic;
  167. // Strip the set index from the semantic
  168. const setIndexRegex = /^(\w+)_\d+$/;
  169. const setIndexMatch = setIndexRegex.exec(gltfSemantic);
  170. if (setIndexMatch !== null) {
  171. semantic = setIndexMatch[1];
  172. }
  173. switch (semantic) {
  174. case "POSITION":
  175. return VertexAttributeSemantic.POSITION;
  176. case "NORMAL":
  177. return VertexAttributeSemantic.NORMAL;
  178. case "TANGENT":
  179. return VertexAttributeSemantic.TANGENT;
  180. case "TEXCOORD":
  181. return VertexAttributeSemantic.TEXCOORD;
  182. case "COLOR":
  183. return VertexAttributeSemantic.COLOR;
  184. case "JOINTS":
  185. return VertexAttributeSemantic.JOINTS;
  186. case "WEIGHTS":
  187. return VertexAttributeSemantic.WEIGHTS;
  188. case "_FEATURE_ID":
  189. return VertexAttributeSemantic.FEATURE_ID;
  190. case "KHR_gaussian_splatting:SCALE":
  191. case "_SCALE":
  192. return VertexAttributeSemantic.SCALE;
  193. case "KHR_gaussian_splatting:ROTATION":
  194. case "_ROTATION":
  195. return VertexAttributeSemantic.ROTATION;
  196. case "BENTLEY_materials_line_style:CUMULATIVE_DISTANCE":
  197. return VertexAttributeSemantic.CUMULATIVE_DISTANCE;
  198. }
  199. return undefined;
  200. };
  201. /**
  202. * Gets the vertex attribute semantic matching the pnts semantic.
  203. *
  204. * @param {string} pntsSemantic The pnts semantic.
  205. *
  206. * @returns {VertexAttributeSemantic|undefined} The vertex attribute semantic, or undefined if there is no match.
  207. *
  208. * @private
  209. */
  210. VertexAttributeSemantic.fromPntsSemantic = function (pntsSemantic) {
  211. //>>includeStart('debug', pragmas.debug);
  212. Check.typeOf.string("pntsSemantic", pntsSemantic);
  213. //>>includeEnd('debug');
  214. switch (pntsSemantic) {
  215. case "POSITION":
  216. case "POSITION_QUANTIZED":
  217. return VertexAttributeSemantic.POSITION;
  218. case "RGBA":
  219. case "RGB":
  220. case "RGB565":
  221. return VertexAttributeSemantic.COLOR;
  222. case "NORMAL":
  223. case "NORMAL_OCT16P":
  224. return VertexAttributeSemantic.NORMAL;
  225. case "BATCH_ID":
  226. return VertexAttributeSemantic.FEATURE_ID;
  227. //>>includeStart('debug', pragmas.debug);
  228. default:
  229. throw new DeveloperError("pntsSemantic is not a valid value.");
  230. //>>includeEnd('debug');
  231. }
  232. };
  233. /**
  234. * Gets the GLSL type (such as <code>vec3</code> or <code>int</code>) for the
  235. * given vertex attribute.
  236. *
  237. * @param {VertexAttributeSemantic} semantic The semantic.
  238. *
  239. * @returns {string} The shader type.
  240. *
  241. * @private
  242. */
  243. VertexAttributeSemantic.getGlslType = function (semantic) {
  244. //>>includeStart('debug', pragmas.debug);
  245. Check.typeOf.string("semantic", semantic);
  246. //>>includeEnd('debug');
  247. switch (semantic) {
  248. case VertexAttributeSemantic.POSITION:
  249. case VertexAttributeSemantic.NORMAL:
  250. case VertexAttributeSemantic.TANGENT:
  251. return "vec3";
  252. case VertexAttributeSemantic.TEXCOORD:
  253. return "vec2";
  254. case VertexAttributeSemantic.COLOR:
  255. return "vec4";
  256. case VertexAttributeSemantic.JOINTS:
  257. return "ivec4";
  258. case VertexAttributeSemantic.WEIGHTS:
  259. return "vec4";
  260. case VertexAttributeSemantic.FEATURE_ID:
  261. return "int";
  262. case VertexAttributeSemantic.SCALE:
  263. return "vec3";
  264. case VertexAttributeSemantic.ROTATION:
  265. return "vec4";
  266. case VertexAttributeSemantic.CUMULATIVE_DISTANCE:
  267. return "float";
  268. case VertexAttributeSemantic.OPACITY:
  269. return "float";
  270. //>>includeStart('debug', pragmas.debug);
  271. default:
  272. throw new DeveloperError("semantic is not a valid value.");
  273. //>>includeEnd('debug');
  274. }
  275. };
  276. /**
  277. * Gets the variable name for the given semantic and set index.
  278. *
  279. * @param {VertexAttributeSemantic} semantic The semantic.
  280. * @param {number} [setIndex] The set index.
  281. *
  282. * @returns {string} The variable name.
  283. *
  284. * @private
  285. */
  286. VertexAttributeSemantic.getVariableName = function (semantic, setIndex) {
  287. //>>includeStart('debug', pragmas.debug);
  288. Check.typeOf.string("semantic", semantic);
  289. //>>includeEnd('debug');
  290. let variableName = semanticToVariableName(semantic);
  291. if (defined(setIndex)) {
  292. variableName += `_${setIndex}`;
  293. }
  294. return variableName;
  295. };
  296. Object.freeze(VertexAttributeSemantic);
  297. export default VertexAttributeSemantic;