babylon.vertexBuffer.ts 7.6 KB

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