babylon.vertexBuffer.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. module BABYLON {
  2. export class VertexBuffer {
  3. private _mesh; //ANY
  4. private _engine; //ANY
  5. private _buffer;
  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 getStrideSize(): number {
  58. return this._strideSize;
  59. }
  60. // Methods
  61. public update(data: number[]): void {
  62. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  63. this._data = data;
  64. if (this._kind === BABYLON.VertexBuffer.PositionKind && this._mesh) {
  65. this._mesh._resetPointsArrayCache();
  66. }
  67. }
  68. public dispose(): void {
  69. this._engine._releaseBuffer(this._buffer);
  70. }
  71. // Enums
  72. public static PositionKind = "position";
  73. public static NormalKind = "normal";
  74. public static UVKind = "uv";
  75. public static UV2Kind = "uv2";
  76. public static ColorKind = "color";
  77. public static MatricesIndicesKind = "matricesIndices";
  78. public static MatricesWeightsKind = "matricesWeights";
  79. }
  80. }