babylon.vertexBuffer.ts 7.5 KB

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