babylon.glTFUtilities.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
  2. module BABYLON.GLTF2 {
  3. /**
  4. * @hidden
  5. */
  6. export class _GLTFUtilities {
  7. /**
  8. * Creates a buffer view based on the supplied arguments
  9. * @param bufferIndex index value of the specified buffer
  10. * @param byteOffset byte offset value
  11. * @param byteLength byte length of the bufferView
  12. * @param byteStride byte distance between conequential elements
  13. * @param name name of the buffer view
  14. * @returns bufferView for glTF
  15. */
  16. public static CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView {
  17. let bufferview: IBufferView = { buffer: bufferIndex, byteLength: byteLength };
  18. if (byteOffset) {
  19. bufferview.byteOffset = byteOffset;
  20. }
  21. if (name) {
  22. bufferview.name = name;
  23. }
  24. if (byteStride) {
  25. bufferview.byteStride = byteStride;
  26. }
  27. return bufferview;
  28. }
  29. /**
  30. * Creates an accessor based on the supplied arguments
  31. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  32. * @param name The name of the accessor
  33. * @param type The type of the accessor
  34. * @param componentType The datatype of components in the attribute
  35. * @param count The number of attributes referenced by this accessor
  36. * @param byteOffset The offset relative to the start of the bufferView in bytes
  37. * @param min Minimum value of each component in this attribute
  38. * @param max Maximum value of each component in this attribute
  39. * @returns accessor for glTF
  40. */
  41. public static CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor {
  42. let accessor: IAccessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  43. if (min != null) {
  44. accessor.min = min;
  45. }
  46. if (max != null) {
  47. accessor.max = max;
  48. }
  49. if (byteOffset != null) {
  50. accessor.byteOffset = byteOffset;
  51. }
  52. return accessor;
  53. }
  54. /**
  55. * Calculates the minimum and maximum values of an array of position floats
  56. * @param positions Positions array of a mesh
  57. * @param vertexStart Starting vertex offset to calculate min and max values
  58. * @param vertexCount Number of vertices to check for min and max values
  59. * @returns min number array and max number array
  60. */
  61. public static CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): { min: number[], max: number[] } {
  62. const min = [Infinity, Infinity, Infinity];
  63. const max = [-Infinity, -Infinity, -Infinity];
  64. const positionStrideSize = 3;
  65. let indexOffset: number;
  66. let position: Vector3;
  67. let vector: number[];
  68. if (vertexCount) {
  69. for (let i = vertexStart, length = vertexStart + vertexCount; i < length; ++i) {
  70. indexOffset = positionStrideSize * i;
  71. position = Vector3.FromArray(positions, indexOffset);
  72. if (convertToRightHandedSystem) {
  73. _GLTFUtilities.GetRightHandedPositionVector3FromRef(position);
  74. }
  75. vector = position.asArray();
  76. for (let j = 0; j < positionStrideSize; ++j) {
  77. let num = vector[j];
  78. if (num < min[j]) {
  79. min[j] = num;
  80. }
  81. if (num > max[j]) {
  82. max[j] = num;
  83. }
  84. ++indexOffset;
  85. }
  86. }
  87. }
  88. return { min, max };
  89. }
  90. /**
  91. * Converts a new right-handed Vector3
  92. * @param vector vector3 array
  93. * @returns right-handed Vector3
  94. */
  95. public static GetRightHandedPositionVector3(vector: Vector3): Vector3 {
  96. return new Vector3(vector.x, vector.y, -vector.z);
  97. }
  98. /**
  99. * Converts a Vector3 to right-handed
  100. * @param vector Vector3 to convert to right-handed
  101. */
  102. public static GetRightHandedPositionVector3FromRef(vector: Vector3) {
  103. vector.z *= -1;
  104. }
  105. /**
  106. * Converts a three element number array to right-handed
  107. * @param vector number array to convert to right-handed
  108. */
  109. public static GetRightHandedPositionArray3FromRef(vector: number[]) {
  110. vector[2] *= -1;
  111. }
  112. /**
  113. * Converts a new right-handed Vector3
  114. * @param vector vector3 array
  115. * @returns right-handed Vector3
  116. */
  117. public static GetRightHandedNormalVector3(vector: Vector3): Vector3 {
  118. return new Vector3(vector.x, vector.y, -vector.z);
  119. }
  120. /**
  121. * Converts a Vector3 to right-handed
  122. * @param vector Vector3 to convert to right-handed
  123. */
  124. public static GetRightHandedNormalVector3FromRef(vector: Vector3) {
  125. vector.z *= -1;
  126. }
  127. /**
  128. * Converts a three element number array to right-handed
  129. * @param vector number array to convert to right-handed
  130. */
  131. public static GetRightHandedNormalArray3FromRef(vector: number[]) {
  132. vector[2] *= -1;
  133. }
  134. /**
  135. * Converts a Vector4 to right-handed
  136. * @param vector Vector4 to convert to right-handed
  137. */
  138. public static GetRightHandedVector4FromRef(vector: Vector4) {
  139. vector.z *= -1;
  140. vector.w *= -1;
  141. }
  142. /**
  143. * Converts a Vector4 to right-handed
  144. * @param vector Vector4 to convert to right-handed
  145. */
  146. public static GetRightHandedArray4FromRef(vector: number[]) {
  147. vector[2] *= -1;
  148. vector[3] *= -1;
  149. }
  150. /**
  151. * Converts a Quaternion to right-handed
  152. * @param quaternion Source quaternion to convert to right-handed
  153. */
  154. public static GetRightHandedQuaternionFromRef(quaternion: Quaternion) {
  155. quaternion.x *= -1;
  156. quaternion.y *= -1;
  157. }
  158. /**
  159. * Converts a Quaternion to right-handed
  160. * @param quaternion Source quaternion to convert to right-handed
  161. */
  162. public static GetRightHandedQuaternionArrayFromRef(quaternion: number[]) {
  163. quaternion[0] *= -1;
  164. quaternion[1] *= -1;
  165. }
  166. }
  167. }