babylon.buffer.ts 5.6 KB

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