babylon.vertexBuffer.ts 4.5 KB

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