babylon.vertexBuffer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var VertexBuffer = (function () {
  4. function VertexBuffer(engine, data, kind, updatable, postponeInternalCreation, stride) {
  5. if (engine instanceof BABYLON.Mesh) {
  6. this._engine = engine.getScene().getEngine();
  7. }
  8. else {
  9. this._engine = engine;
  10. }
  11. this._updatable = updatable;
  12. this._data = data;
  13. if (!postponeInternalCreation) {
  14. this.create();
  15. }
  16. this._kind = kind;
  17. if (stride) {
  18. this._strideSize = stride;
  19. return;
  20. }
  21. // Deduce stride from kind
  22. switch (kind) {
  23. case VertexBuffer.PositionKind:
  24. this._strideSize = 3;
  25. break;
  26. case VertexBuffer.NormalKind:
  27. this._strideSize = 3;
  28. break;
  29. case VertexBuffer.UVKind:
  30. this._strideSize = 2;
  31. break;
  32. case VertexBuffer.UV2Kind:
  33. this._strideSize = 2;
  34. break;
  35. case VertexBuffer.ColorKind:
  36. this._strideSize = 4;
  37. break;
  38. case VertexBuffer.MatricesIndicesKind:
  39. this._strideSize = 4;
  40. break;
  41. case VertexBuffer.MatricesWeightsKind:
  42. this._strideSize = 4;
  43. break;
  44. }
  45. }
  46. // Properties
  47. VertexBuffer.prototype.isUpdatable = function () {
  48. return this._updatable;
  49. };
  50. VertexBuffer.prototype.getData = function () {
  51. return this._data;
  52. };
  53. VertexBuffer.prototype.getBuffer = function () {
  54. return this._buffer;
  55. };
  56. VertexBuffer.prototype.getStrideSize = function () {
  57. return this._strideSize;
  58. };
  59. // Methods
  60. VertexBuffer.prototype.create = function (data) {
  61. if (!data && this._buffer) {
  62. return; // nothing to do
  63. }
  64. data = data || this._data;
  65. if (!this._buffer) {
  66. if (this._updatable) {
  67. this._buffer = this._engine.createDynamicVertexBuffer(data.length * 4);
  68. }
  69. else {
  70. this._buffer = this._engine.createVertexBuffer(data);
  71. }
  72. }
  73. if (this._updatable) {
  74. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  75. this._data = data;
  76. }
  77. };
  78. VertexBuffer.prototype.update = function (data) {
  79. this.create(data);
  80. };
  81. VertexBuffer.prototype.updateDirectly = function (data, offset) {
  82. if (!this._buffer) {
  83. return;
  84. }
  85. if (this._updatable) {
  86. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset);
  87. this._data = null;
  88. }
  89. };
  90. VertexBuffer.prototype.dispose = function () {
  91. if (!this._buffer) {
  92. return;
  93. }
  94. if (this._engine._releaseBuffer(this._buffer)) {
  95. this._buffer = null;
  96. }
  97. };
  98. Object.defineProperty(VertexBuffer, "PositionKind", {
  99. get: function () {
  100. return VertexBuffer._PositionKind;
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. Object.defineProperty(VertexBuffer, "NormalKind", {
  106. get: function () {
  107. return VertexBuffer._NormalKind;
  108. },
  109. enumerable: true,
  110. configurable: true
  111. });
  112. Object.defineProperty(VertexBuffer, "UVKind", {
  113. get: function () {
  114. return VertexBuffer._UVKind;
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. Object.defineProperty(VertexBuffer, "UV2Kind", {
  120. get: function () {
  121. return VertexBuffer._UV2Kind;
  122. },
  123. enumerable: true,
  124. configurable: true
  125. });
  126. Object.defineProperty(VertexBuffer, "ColorKind", {
  127. get: function () {
  128. return VertexBuffer._ColorKind;
  129. },
  130. enumerable: true,
  131. configurable: true
  132. });
  133. Object.defineProperty(VertexBuffer, "MatricesIndicesKind", {
  134. get: function () {
  135. return VertexBuffer._MatricesIndicesKind;
  136. },
  137. enumerable: true,
  138. configurable: true
  139. });
  140. Object.defineProperty(VertexBuffer, "MatricesWeightsKind", {
  141. get: function () {
  142. return VertexBuffer._MatricesWeightsKind;
  143. },
  144. enumerable: true,
  145. configurable: true
  146. });
  147. // Enums
  148. VertexBuffer._PositionKind = "position";
  149. VertexBuffer._NormalKind = "normal";
  150. VertexBuffer._UVKind = "uv";
  151. VertexBuffer._UV2Kind = "uv2";
  152. VertexBuffer._ColorKind = "color";
  153. VertexBuffer._MatricesIndicesKind = "matricesIndices";
  154. VertexBuffer._MatricesWeightsKind = "matricesWeights";
  155. return VertexBuffer;
  156. })();
  157. BABYLON.VertexBuffer = VertexBuffer;
  158. })(BABYLON || (BABYLON = {}));
  159. //# sourceMappingURL=babylon.vertexBuffer.js.map