babylon.vertexBuffer.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. module BABYLON {
  2. export class VertexBuffer {
  3. private _mesh: Mesh;
  4. private _engine: Engine;
  5. private _buffer: WebGLBuffer;
  6. private _data: any;
  7. private _updatable: boolean;
  8. private _kind: string;
  9. private _strideSize: number;
  10. constructor(engine: any, data: any, kind: string, updatable: boolean, postponeInternalCreation?: boolean, stride?: number) {
  11. if (engine instanceof Mesh) { // old versions of BABYLON.VertexBuffer accepted 'mesh' instead of 'engine'
  12. this._engine = engine.getScene().getEngine();
  13. }
  14. else {
  15. this._engine = engine;
  16. }
  17. this._updatable = updatable;
  18. this._data = data;
  19. if (!postponeInternalCreation) { // by default
  20. this.create();
  21. }
  22. this._kind = kind;
  23. if (stride) {
  24. this._strideSize = stride;
  25. return;
  26. }
  27. // Deduce stride from kind
  28. switch (kind) {
  29. case VertexBuffer.PositionKind:
  30. this._strideSize = 3;
  31. break;
  32. case VertexBuffer.NormalKind:
  33. this._strideSize = 3;
  34. break;
  35. case VertexBuffer.UVKind:
  36. case VertexBuffer.UV2Kind:
  37. case VertexBuffer.UV3Kind:
  38. case VertexBuffer.UV4Kind:
  39. case VertexBuffer.UV5Kind:
  40. case VertexBuffer.UV6Kind:
  41. this._strideSize = 2;
  42. break;
  43. case VertexBuffer.ColorKind:
  44. this._strideSize = 4;
  45. break;
  46. case VertexBuffer.MatricesIndicesKind:
  47. this._strideSize = 4;
  48. break;
  49. case VertexBuffer.MatricesWeightsKind:
  50. this._strideSize = 4;
  51. break;
  52. }
  53. }
  54. // Properties
  55. public isUpdatable(): boolean {
  56. return this._updatable;
  57. }
  58. public getData(): any {
  59. return this._data;
  60. }
  61. public getBuffer(): WebGLBuffer {
  62. return this._buffer;
  63. }
  64. public getStrideSize(): number {
  65. return this._strideSize;
  66. }
  67. // Methods
  68. public create(data?: any): void {
  69. if (!data && this._buffer) {
  70. return; // nothing to do
  71. }
  72. data = data || this._data;
  73. if (!this._buffer) { // create buffer
  74. if (this._updatable) {
  75. this._buffer = this._engine.createDynamicVertexBuffer(data.length * 4);
  76. } else {
  77. this._buffer = this._engine.createVertexBuffer(data);
  78. }
  79. }
  80. if (this._updatable) { // update buffer
  81. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  82. this._data = data;
  83. }
  84. }
  85. public update(data: any): void {
  86. this.create(data);
  87. }
  88. public updateDirectly(data: Float32Array, offset: number): void {
  89. if (!this._buffer) {
  90. return;
  91. }
  92. if (this._updatable) { // update buffer
  93. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset);
  94. this._data = null;
  95. }
  96. }
  97. public dispose(): void {
  98. if (!this._buffer) {
  99. return;
  100. }
  101. if (this._engine._releaseBuffer(this._buffer)) {
  102. this._buffer = null;
  103. }
  104. }
  105. // Enums
  106. private static _PositionKind = "position";
  107. private static _NormalKind = "normal";
  108. private static _UVKind = "uv";
  109. private static _UV2Kind = "uv2";
  110. private static _UV3Kind = "uv3";
  111. private static _UV4Kind = "uv4";
  112. private static _UV5Kind = "uv5";
  113. private static _UV6Kind = "uv6";
  114. private static _ColorKind = "color";
  115. private static _MatricesIndicesKind = "matricesIndices";
  116. private static _MatricesWeightsKind = "matricesWeights";
  117. public static get PositionKind(): string {
  118. return VertexBuffer._PositionKind;
  119. }
  120. public static get NormalKind(): string {
  121. return VertexBuffer._NormalKind;
  122. }
  123. public static get UVKind(): string {
  124. return VertexBuffer._UVKind;
  125. }
  126. public static get UV2Kind(): string {
  127. return VertexBuffer._UV2Kind;
  128. }
  129. public static get UV3Kind(): string {
  130. return VertexBuffer._UV3Kind;
  131. }
  132. public static get UV4Kind(): string {
  133. return VertexBuffer._UV4Kind;
  134. }
  135. public static get UV5Kind(): string {
  136. return VertexBuffer._UV5Kind;
  137. }
  138. public static get UV6Kind(): string {
  139. return VertexBuffer._UV6Kind;
  140. }
  141. public static get ColorKind(): string {
  142. return VertexBuffer._ColorKind;
  143. }
  144. public static get MatricesIndicesKind(): string {
  145. return VertexBuffer._MatricesIndicesKind;
  146. }
  147. public static get MatricesWeightsKind(): string {
  148. return VertexBuffer._MatricesWeightsKind;
  149. }
  150. }
  151. }