babylon.vertexBuffer.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * This function will create a new buffer if the current one is not updatable
  129. * Returns the updated WebGLBuffer.
  130. */
  131. public update(data: FloatArray): void {
  132. return this._buffer.update(data);
  133. }
  134. /**
  135. * Updates directly the underlying WebGLBuffer according to the passed numeric array or Float32Array.
  136. * Returns the directly updated WebGLBuffer.
  137. */
  138. public updateDirectly(data: Float32Array, offset: number): void {
  139. return this._buffer.updateDirectly(data, offset);
  140. }
  141. /**
  142. * Disposes the VertexBuffer and the underlying WebGLBuffer.
  143. */
  144. public dispose(): void {
  145. if (this._ownsBuffer) {
  146. this._buffer.dispose();
  147. }
  148. }
  149. // Enums
  150. private static _PositionKind = "position";
  151. private static _NormalKind = "normal";
  152. private static _TangentKind = "tangent";
  153. private static _UVKind = "uv";
  154. private static _UV2Kind = "uv2";
  155. private static _UV3Kind = "uv3";
  156. private static _UV4Kind = "uv4";
  157. private static _UV5Kind = "uv5";
  158. private static _UV6Kind = "uv6";
  159. private static _ColorKind = "color";
  160. private static _MatricesIndicesKind = "matricesIndices";
  161. private static _MatricesWeightsKind = "matricesWeights";
  162. private static _MatricesIndicesExtraKind = "matricesIndicesExtra";
  163. private static _MatricesWeightsExtraKind = "matricesWeightsExtra";
  164. public static get PositionKind(): string {
  165. return VertexBuffer._PositionKind;
  166. }
  167. public static get NormalKind(): string {
  168. return VertexBuffer._NormalKind;
  169. }
  170. public static get TangentKind(): string {
  171. return VertexBuffer._TangentKind;
  172. }
  173. public static get UVKind(): string {
  174. return VertexBuffer._UVKind;
  175. }
  176. public static get UV2Kind(): string {
  177. return VertexBuffer._UV2Kind;
  178. }
  179. public static get UV3Kind(): string {
  180. return VertexBuffer._UV3Kind;
  181. }
  182. public static get UV4Kind(): string {
  183. return VertexBuffer._UV4Kind;
  184. }
  185. public static get UV5Kind(): string {
  186. return VertexBuffer._UV5Kind;
  187. }
  188. public static get UV6Kind(): string {
  189. return VertexBuffer._UV6Kind;
  190. }
  191. public static get ColorKind(): string {
  192. return VertexBuffer._ColorKind;
  193. }
  194. public static get MatricesIndicesKind(): string {
  195. return VertexBuffer._MatricesIndicesKind;
  196. }
  197. public static get MatricesWeightsKind(): string {
  198. return VertexBuffer._MatricesWeightsKind;
  199. }
  200. public static get MatricesIndicesExtraKind(): string {
  201. return VertexBuffer._MatricesIndicesExtraKind;
  202. }
  203. public static get MatricesWeightsExtraKind(): string {
  204. return VertexBuffer._MatricesWeightsExtraKind;
  205. }
  206. }
  207. }