babylon.buffer.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. module BABYLON {
  2. export class Buffer {
  3. private _engine: Engine;
  4. private _buffer: WebGLBuffer;
  5. private _data: number[] | Float32Array;
  6. private _updatable: boolean;
  7. private _strideSize: number;
  8. private _instanced: boolean;
  9. private _instanceDivisor: number;
  10. constructor(engine: any, data: number[] | Float32Array, updatable: boolean, stride: number, postponeInternalCreation?: boolean, instanced?: 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. 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): 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, this._instanced, offset, size);
  29. }
  30. // Properties
  31. public isUpdatable(): boolean {
  32. return this._updatable;
  33. }
  34. public getData(): number[] | Float32Array {
  35. return this._data;
  36. }
  37. public getBuffer(): 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?: number[] | Float32Array): void {
  59. if (!data && this._buffer) {
  60. return; // nothing to do
  61. }
  62. data = data || this._data;
  63. if (!this._buffer) { // create buffer
  64. if (this._updatable) {
  65. this._buffer = this._engine.createDynamicVertexBuffer(data);
  66. this._data = data;
  67. } else {
  68. this._buffer = this._engine.createVertexBuffer(data);
  69. }
  70. } else if (this._updatable) { // update buffer
  71. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  72. this._data = data;
  73. }
  74. }
  75. public update(data: number[] | Float32Array): void {
  76. this.create(data);
  77. }
  78. public updateDirectly(data: Float32Array, offset: number, vertexCount?: number): void {
  79. if (!this._buffer) {
  80. return;
  81. }
  82. if (this._updatable) { // update buffer
  83. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset, (vertexCount ? vertexCount * this.getStrideSize() : undefined));
  84. this._data = null;
  85. }
  86. }
  87. public dispose(): void {
  88. if (!this._buffer) {
  89. return;
  90. }
  91. if (this._engine._releaseBuffer(this._buffer)) {
  92. this._buffer = null;
  93. }
  94. }
  95. }
  96. }