babylon.buffer.ts 5.7 KB

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