babylon.vertexBuffer.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. module BABYLON {
  2. export class VertexBuffer {
  3. private _buffer: Buffer;
  4. private _kind: string;
  5. private _offset: number;
  6. private _size: number;
  7. private _stride: number;
  8. private _ownsBuffer: boolean;
  9. constructor(engine: any, data: FloatArray | Buffer, kind: string, updatable: boolean, postponeInternalCreation?: boolean, stride?: number, instanced?: boolean, offset?: number, size?: number) {
  10. if (!stride) {
  11. stride = VertexBuffer.DeduceStride(kind);
  12. }
  13. if (data instanceof Buffer) {
  14. if (!stride) {
  15. stride = data.getStrideSize();
  16. }
  17. this._buffer = data;
  18. this._ownsBuffer = false;
  19. } else {
  20. this._buffer = new Buffer(engine, <FloatArray>data, updatable, stride, postponeInternalCreation, instanced);
  21. this._ownsBuffer = true;
  22. }
  23. this._stride = stride;
  24. this._offset = offset ? offset : 0;
  25. this._size = size ? size : stride;
  26. this._kind = kind;
  27. }
  28. public _rebuild(): void {
  29. if (!this._buffer) {
  30. return;
  31. }
  32. this._buffer._rebuild();
  33. }
  34. /**
  35. * Returns the kind of the VertexBuffer (string).
  36. */
  37. public getKind(): string {
  38. return this._kind;
  39. }
  40. // Properties
  41. /**
  42. * Boolean : is the VertexBuffer updatable ?
  43. */
  44. public isUpdatable(): boolean {
  45. return this._buffer.isUpdatable();
  46. }
  47. /**
  48. * Returns an array of numbers or a Float32Array containing the VertexBuffer data.
  49. */
  50. public getData(): Nullable<FloatArray> {
  51. return this._buffer.getData();
  52. }
  53. /**
  54. * Returns the WebGLBuffer associated to the VertexBuffer.
  55. */
  56. public getBuffer(): Nullable<WebGLBuffer> {
  57. return this._buffer.getBuffer();
  58. }
  59. /**
  60. * Returns the stride of the VertexBuffer (integer).
  61. */
  62. public getStrideSize(): number {
  63. return this._stride;
  64. }
  65. /**
  66. * Returns the offset (integer).
  67. */
  68. public getOffset(): number {
  69. return this._offset;
  70. }
  71. /**
  72. * Returns the VertexBuffer total size (integer).
  73. */
  74. public getSize(): number {
  75. return this._size;
  76. }
  77. /**
  78. * Boolean : is the WebGLBuffer of the VertexBuffer instanced now ?
  79. */
  80. public getIsInstanced(): boolean {
  81. return this._buffer.getIsInstanced();
  82. }
  83. /**
  84. * Returns the instancing divisor, zero for non-instanced (integer).
  85. */
  86. public getInstanceDivisor(): number {
  87. return this._buffer.instanceDivisor;
  88. }
  89. // Methods
  90. /**
  91. * Creates the underlying WebGLBuffer from the passed numeric array or Float32Array.
  92. * Returns the created WebGLBuffer.
  93. */
  94. public create(data?: FloatArray): void {
  95. return this._buffer.create(data);
  96. }
  97. /**
  98. * Updates the underlying WebGLBuffer according to the passed numeric array or Float32Array.
  99. * This function will create a new buffer if the current one is not updatable
  100. * Returns the updated WebGLBuffer.
  101. */
  102. public update(data: FloatArray): void {
  103. return this._buffer.update(data);
  104. }
  105. /**
  106. * Updates directly the underlying WebGLBuffer according to the passed numeric array or Float32Array.
  107. * Returns the directly updated WebGLBuffer.
  108. */
  109. public updateDirectly(data: Float32Array, offset: number): void {
  110. return this._buffer.updateDirectly(data, offset);
  111. }
  112. /**
  113. * Disposes the VertexBuffer and the underlying WebGLBuffer.
  114. */
  115. public dispose(): void {
  116. if (this._ownsBuffer) {
  117. this._buffer.dispose();
  118. }
  119. }
  120. // Enums
  121. private static _PositionKind = "position";
  122. private static _NormalKind = "normal";
  123. private static _TangentKind = "tangent";
  124. private static _UVKind = "uv";
  125. private static _UV2Kind = "uv2";
  126. private static _UV3Kind = "uv3";
  127. private static _UV4Kind = "uv4";
  128. private static _UV5Kind = "uv5";
  129. private static _UV6Kind = "uv6";
  130. private static _ColorKind = "color";
  131. private static _MatricesIndicesKind = "matricesIndices";
  132. private static _MatricesWeightsKind = "matricesWeights";
  133. private static _MatricesIndicesExtraKind = "matricesIndicesExtra";
  134. private static _MatricesWeightsExtraKind = "matricesWeightsExtra";
  135. public static get PositionKind(): string {
  136. return VertexBuffer._PositionKind;
  137. }
  138. public static get NormalKind(): string {
  139. return VertexBuffer._NormalKind;
  140. }
  141. public static get TangentKind(): string {
  142. return VertexBuffer._TangentKind;
  143. }
  144. public static get UVKind(): string {
  145. return VertexBuffer._UVKind;
  146. }
  147. public static get UV2Kind(): string {
  148. return VertexBuffer._UV2Kind;
  149. }
  150. public static get UV3Kind(): string {
  151. return VertexBuffer._UV3Kind;
  152. }
  153. public static get UV4Kind(): string {
  154. return VertexBuffer._UV4Kind;
  155. }
  156. public static get UV5Kind(): string {
  157. return VertexBuffer._UV5Kind;
  158. }
  159. public static get UV6Kind(): string {
  160. return VertexBuffer._UV6Kind;
  161. }
  162. public static get ColorKind(): string {
  163. return VertexBuffer._ColorKind;
  164. }
  165. public static get MatricesIndicesKind(): string {
  166. return VertexBuffer._MatricesIndicesKind;
  167. }
  168. public static get MatricesWeightsKind(): string {
  169. return VertexBuffer._MatricesWeightsKind;
  170. }
  171. public static get MatricesIndicesExtraKind(): string {
  172. return VertexBuffer._MatricesIndicesExtraKind;
  173. }
  174. public static get MatricesWeightsExtraKind(): string {
  175. return VertexBuffer._MatricesWeightsExtraKind;
  176. }
  177. /**
  178. * Deduces the stride given a kind.
  179. * @param kind The kind string to deduce
  180. * @returns The deduced stride
  181. */
  182. public static DeduceStride(kind: string): number {
  183. switch (kind) {
  184. case VertexBuffer.PositionKind:
  185. return 3;
  186. case VertexBuffer.NormalKind:
  187. return 3;
  188. case VertexBuffer.UVKind:
  189. case VertexBuffer.UV2Kind:
  190. case VertexBuffer.UV3Kind:
  191. case VertexBuffer.UV4Kind:
  192. case VertexBuffer.UV5Kind:
  193. case VertexBuffer.UV6Kind:
  194. return 2;
  195. case VertexBuffer.TangentKind:
  196. case VertexBuffer.ColorKind:
  197. return 4;
  198. case VertexBuffer.MatricesIndicesKind:
  199. case VertexBuffer.MatricesIndicesExtraKind:
  200. return 4;
  201. case VertexBuffer.MatricesWeightsKind:
  202. case VertexBuffer.MatricesWeightsExtraKind:
  203. return 4;
  204. default:
  205. throw new Error("Invalid kind '" + kind + "'");
  206. }
  207. }
  208. }
  209. }