babylon.buffer.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. private _instanceDivisor: number;
  10. constructor(engine: any, data: FloatArray, updatable: boolean, stride: number, postponeInternalCreation?: boolean, instanced: boolean = false) {
  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. this._strideSize = stride;
  20. if (!postponeInternalCreation) { // by default
  21. this.create();
  22. }
  23. this._instanced = instanced;
  24. this._instanceDivisor = instanced ? 1 : 0;
  25. }
  26. public createVertexBuffer(kind: string, offset: number, size: number, stride?: number, instanced?: boolean): VertexBuffer {
  27. // a lot of these parameters are ignored as they are overriden by the buffer
  28. return new VertexBuffer(this._engine, this, kind, this._updatable, true, stride ? stride : this._strideSize, instanced !== undefined ? instanced : this._instanced, offset, size);
  29. }
  30. // Properties
  31. public isUpdatable(): boolean {
  32. return this._updatable;
  33. }
  34. public getData(): Nullable<FloatArray> {
  35. return this._data;
  36. }
  37. public getBuffer(): Nullable<WebGLBuffer> {
  38. return this._buffer;
  39. }
  40. public getStrideSize(): number {
  41. return this._strideSize;
  42. }
  43. public getIsInstanced(): boolean {
  44. return this._instanced;
  45. }
  46. public get instanceDivisor(): number {
  47. return this._instanceDivisor;
  48. }
  49. public set instanceDivisor(value: number) {
  50. this._instanceDivisor = value;
  51. if (value == 0) {
  52. this._instanced = false;
  53. } else {
  54. this._instanced = true;
  55. }
  56. }
  57. // Methods
  58. public create(data: Nullable<FloatArray> = null): void {
  59. if (!data && this._buffer) {
  60. return; // nothing to do
  61. }
  62. data = data || this._data;
  63. if (!data) {
  64. return;
  65. }
  66. if (!this._buffer) { // create buffer
  67. if (this._updatable) {
  68. this._buffer = this._engine.createDynamicVertexBuffer(data);
  69. this._data = data;
  70. } else {
  71. this._buffer = this._engine.createVertexBuffer(data);
  72. }
  73. } else if (this._updatable) { // update buffer
  74. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  75. this._data = data;
  76. }
  77. }
  78. public _rebuild(): void {
  79. this._buffer = null;
  80. this.create(this._data);
  81. }
  82. public update(data: FloatArray): void {
  83. this.create(data);
  84. }
  85. public updateDirectly(data: Float32Array, offset: number, vertexCount?: number): void {
  86. if (!this._buffer) {
  87. return;
  88. }
  89. if (this._updatable) { // update buffer
  90. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset, (vertexCount ? vertexCount * this.getStrideSize() : undefined));
  91. this._data = null;
  92. }
  93. }
  94. public dispose(): void {
  95. if (!this._buffer) {
  96. return;
  97. }
  98. if (this._engine._releaseBuffer(this._buffer)) {
  99. this._buffer = null;
  100. }
  101. }
  102. }
  103. }