babylon.vertexBuffer.ts 3.1 KB

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