babylon.buffer.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. module BABYLON {
  2. export class Buffer {
  3. private _engine: Engine;
  4. private _buffer: Nullable<WebGLBuffer>;
  5. private _data: Nullable<FloatArray>;
  6. private _updatable: boolean;
  7. private _strideSize: number;
  8. private _instanced: boolean;
  9. constructor(engine: any, data: FloatArray, updatable: boolean, stride: number, postponeInternalCreation?: boolean, instanced: boolean = false) {
  10. if (engine instanceof Mesh) { // old versions of BABYLON.VertexBuffer accepted 'mesh' instead of 'engine'
  11. this._engine = engine.getScene().getEngine();
  12. }
  13. else {
  14. this._engine = engine;
  15. }
  16. this._updatable = updatable;
  17. this._instanced = instanced;
  18. this._data = data;
  19. this._strideSize = stride;
  20. if (!postponeInternalCreation) { // by default
  21. this.create();
  22. }
  23. }
  24. /**
  25. * Create a new {BABYLON.VertexBuffer} based on the current buffer
  26. * @param kind defines the vertex buffer kind (position, normal, etc.)
  27. * @param offset defines offset in the buffer (0 by default)
  28. * @param size defines the size in floats of attributes (position is 3 for instance)
  29. * @param stride defines the stride size in floats in the buffer (the offset to apply to reach next value when data is interleaved)
  30. * @param instanced defines if the vertex buffer contains indexed data
  31. * @returns the new vertex buffer
  32. */
  33. public createVertexBuffer(kind: string, offset: number, size: number, stride?: number, instanced?: boolean): VertexBuffer {
  34. // a lot of these parameters are ignored as they are overriden by the buffer
  35. return new VertexBuffer(this._engine, this, kind, this._updatable, true, stride ? stride : this._strideSize, instanced === undefined ? this._instanced : instanced, offset, size);
  36. }
  37. // Properties
  38. public isUpdatable(): boolean {
  39. return this._updatable;
  40. }
  41. public getData(): Nullable<FloatArray> {
  42. return this._data;
  43. }
  44. public getBuffer(): Nullable<WebGLBuffer> {
  45. return this._buffer;
  46. }
  47. public getStrideSize(): number {
  48. return this._strideSize;
  49. }
  50. // public getIsInstanced(): boolean {
  51. // return this._instanced;
  52. // }
  53. // public get instanceDivisor(): number {
  54. // return this._instanceDivisor;
  55. // }
  56. // public set instanceDivisor(value: number) {
  57. // this._instanceDivisor = value;
  58. // if (value == 0) {
  59. // this._instanced = false;
  60. // } else {
  61. // this._instanced = true;
  62. // }
  63. // }
  64. // Methods
  65. public create(data: Nullable<FloatArray> = null): void {
  66. if (!data && this._buffer) {
  67. return; // nothing to do
  68. }
  69. data = data || this._data;
  70. if (!data) {
  71. return;
  72. }
  73. if (!this._buffer) { // create buffer
  74. if (this._updatable) {
  75. this._buffer = this._engine.createDynamicVertexBuffer(data);
  76. this._data = data;
  77. } else {
  78. this._buffer = this._engine.createVertexBuffer(data);
  79. }
  80. } else if (this._updatable) { // update buffer
  81. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  82. this._data = data;
  83. }
  84. }
  85. public _rebuild(): void {
  86. this._buffer = null;
  87. this.create(this._data);
  88. }
  89. public update(data: FloatArray): void {
  90. this.create(data);
  91. }
  92. public updateDirectly(data: Float32Array, offset: number, vertexCount?: number): void {
  93. if (!this._buffer) {
  94. return;
  95. }
  96. if (this._updatable) { // update buffer
  97. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset, (vertexCount ? vertexCount * this.getStrideSize() : undefined));
  98. this._data = null;
  99. }
  100. }
  101. public dispose(): void {
  102. if (!this._buffer) {
  103. return;
  104. }
  105. if (this._engine._releaseBuffer(this._buffer)) {
  106. this._buffer = null;
  107. }
  108. }
  109. }
  110. }