babylon.vertexBuffer.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. module BABYLON {
  2. export class VertexBuffer {
  3. private _mesh: Mesh;
  4. private _engine: Engine;
  5. private _buffer: WebGLBuffer;
  6. private _data: number[];
  7. private _updatable: boolean;
  8. private _kind: string;
  9. private _strideSize: number;
  10. constructor(mesh: Mesh, data: number[], kind: string, updatable: boolean, engine?: Engine) {
  11. this._mesh = mesh;
  12. this._engine = engine || mesh.getScene().getEngine();
  13. this._updatable = updatable;
  14. if (updatable) {
  15. this._buffer = this._engine.createDynamicVertexBuffer(data.length * 4);
  16. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  17. } else {
  18. this._buffer = this._engine.createVertexBuffer(data);
  19. }
  20. this._data = data;
  21. this._kind = kind;
  22. switch (kind) {
  23. case VertexBuffer.PositionKind:
  24. this._strideSize = 3;
  25. if (this._mesh) {
  26. this._mesh._resetPointsArrayCache();
  27. }
  28. break;
  29. case VertexBuffer.NormalKind:
  30. this._strideSize = 3;
  31. break;
  32. case VertexBuffer.UVKind:
  33. this._strideSize = 2;
  34. break;
  35. case VertexBuffer.UV2Kind:
  36. this._strideSize = 2;
  37. break;
  38. case VertexBuffer.ColorKind:
  39. this._strideSize = 3;
  40. break;
  41. case VertexBuffer.MatricesIndicesKind:
  42. this._strideSize = 4;
  43. break;
  44. case VertexBuffer.MatricesWeightsKind:
  45. this._strideSize = 4;
  46. break;
  47. }
  48. }
  49. // Properties
  50. public isUpdatable(): boolean {
  51. return this._updatable;
  52. }
  53. public getData(): number[] {
  54. return this._data;
  55. }
  56. public getBuffer(): WebGLBuffer {
  57. return this._buffer;
  58. }
  59. public getStrideSize(): number {
  60. return this._strideSize;
  61. }
  62. // Methods
  63. public update(data: number[]): void {
  64. if (!this._updatable) {
  65. console.log("You cannot update a non-updatable vertex buffer");
  66. return;
  67. }
  68. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  69. this._data = data;
  70. if (this._kind === BABYLON.VertexBuffer.PositionKind && this._mesh) {
  71. this._mesh._resetPointsArrayCache();
  72. }
  73. }
  74. public dispose(): void {
  75. this._engine._releaseBuffer(this._buffer);
  76. }
  77. // Enums
  78. public static PositionKind = "position";
  79. public static NormalKind = "normal";
  80. public static UVKind = "uv";
  81. public static UV2Kind = "uv2";
  82. public static ColorKind = "color";
  83. public static MatricesIndicesKind = "matricesIndices";
  84. public static MatricesWeightsKind = "matricesWeights";
  85. }
  86. }