babylon.buffer.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. constructor(engine: any, data: number[] | Float32Array, updatable: boolean, stride: number, postponeInternalCreation?: boolean, instanced?: boolean) {
  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._data = data;
  18. this._strideSize = stride;
  19. if (!postponeInternalCreation) { // by default
  20. this.create();
  21. }
  22. this._instanced = instanced;
  23. }
  24. public createVertexBuffer(kind: string, offset: number, size: number, stride?: number): VertexBuffer {
  25. // a lot of these parameters are ignored as they are overriden by the buffer
  26. return new VertexBuffer(this._engine, this, kind, this._updatable, true, stride, this._instanced, offset, size);
  27. }
  28. // Properties
  29. public isUpdatable(): boolean {
  30. return this._updatable;
  31. }
  32. public getData(): number[] | Float32Array {
  33. return this._data;
  34. }
  35. public getBuffer(): WebGLBuffer {
  36. return this._buffer;
  37. }
  38. public getStrideSize(): number {
  39. return this._strideSize;
  40. }
  41. public getIsInstanced(): boolean {
  42. return this._instanced;
  43. }
  44. // Methods
  45. public create(data?: number[] | Float32Array): void {
  46. if (!data && this._buffer) {
  47. return; // nothing to do
  48. }
  49. data = data || this._data;
  50. if (!this._buffer) { // create buffer
  51. if (this._updatable) {
  52. this._buffer = this._engine.createDynamicVertexBuffer(data);
  53. this._data = data;
  54. } else {
  55. this._buffer = this._engine.createVertexBuffer(data);
  56. }
  57. } else if (this._updatable) { // update buffer
  58. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  59. this._data = data;
  60. }
  61. }
  62. public update(data: number[] | Float32Array): void {
  63. this.create(data);
  64. }
  65. public updateDirectly(data: Float32Array, offset: number, vertexCount?: number): void {
  66. if (!this._buffer) {
  67. return;
  68. }
  69. if (this._updatable) { // update buffer
  70. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset, (vertexCount ? vertexCount * this.getStrideSize() : undefined));
  71. this._data = null;
  72. }
  73. }
  74. public dispose(): void {
  75. if (!this._buffer) {
  76. return;
  77. }
  78. if (this._engine._releaseBuffer(this._buffer)) {
  79. this._buffer = null;
  80. }
  81. }
  82. }
  83. }