babylon.vertexBuffer.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. module BABYLON {
  2. export class VertexBuffer {
  3. private _mesh: Mesh;
  4. private _engine: Engine;
  5. private _buffer: WebGLBuffer;
  6. private _data: number[];
  7. private _updatable: boolean;
  8. private _kind: string;
  9. private _strideSize: number;
  10. constructor(engine: any, data: number[], 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. this._strideSize = 2;
  37. break;
  38. case VertexBuffer.UV2Kind:
  39. this._strideSize = 2;
  40. break;
  41. case VertexBuffer.ColorKind:
  42. this._strideSize = 4;
  43. break;
  44. case VertexBuffer.MatricesIndicesKind:
  45. this._strideSize = 4;
  46. break;
  47. case VertexBuffer.MatricesWeightsKind:
  48. this._strideSize = 4;
  49. break;
  50. }
  51. }
  52. // Properties
  53. public isUpdatable(): boolean {
  54. return this._updatable;
  55. }
  56. public getData(): number[] {
  57. return this._data;
  58. }
  59. public getBuffer(): WebGLBuffer {
  60. return this._buffer;
  61. }
  62. public getStrideSize(): number {
  63. return this._strideSize;
  64. }
  65. // Methods
  66. public create(data?: number[]): void {
  67. if (!data && this._buffer) {
  68. return; // nothing to do
  69. }
  70. data = data || this._data;
  71. if (!this._buffer) { // create buffer
  72. if (this._updatable) {
  73. this._buffer = this._engine.createDynamicVertexBuffer(data.length * 4);
  74. } else {
  75. this._buffer = this._engine.createVertexBuffer(data);
  76. }
  77. }
  78. if (this._updatable) { // update buffer
  79. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  80. this._data = data;
  81. }
  82. }
  83. public update(data: number[]): void {
  84. this.create(data);
  85. }
  86. public updateDirectly(data: Float32Array): void {
  87. if (!this._buffer) {
  88. return;
  89. }
  90. if (this._updatable) { // update buffer
  91. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  92. this._data = null;
  93. }
  94. }
  95. public dispose(): void {
  96. if (!this._buffer) {
  97. return;
  98. }
  99. if (this._engine._releaseBuffer(this._buffer)) {
  100. this._buffer = null;
  101. }
  102. }
  103. // Enums
  104. private static _PositionKind = "position";
  105. private static _NormalKind = "normal";
  106. private static _UVKind = "uv";
  107. private static _UV2Kind = "uv2";
  108. private static _ColorKind = "color";
  109. private static _MatricesIndicesKind = "matricesIndices";
  110. private static _MatricesWeightsKind = "matricesWeights";
  111. public static get PositionKind(): string {
  112. return VertexBuffer._PositionKind;
  113. }
  114. public static get NormalKind(): string {
  115. return VertexBuffer._NormalKind;
  116. }
  117. public static get UVKind(): string {
  118. return VertexBuffer._UVKind;
  119. }
  120. public static get UV2Kind(): string {
  121. return VertexBuffer._UV2Kind;
  122. }
  123. public static get ColorKind(): string {
  124. return VertexBuffer._ColorKind;
  125. }
  126. public static get MatricesIndicesKind(): string {
  127. return VertexBuffer._MatricesIndicesKind;
  128. }
  129. public static get MatricesWeightsKind(): string {
  130. return VertexBuffer._MatricesWeightsKind;
  131. }
  132. }
  133. }