babylon.vertexBuffer.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. case VertexBuffer.UV2Kind:
  31. case VertexBuffer.UV3Kind:
  32. case VertexBuffer.UV4Kind:
  33. case VertexBuffer.UV5Kind:
  34. case VertexBuffer.UV6Kind:
  35. this._strideSize = 2;
  36. break;
  37. case VertexBuffer.ColorKind:
  38. this._strideSize = 4;
  39. break;
  40. case VertexBuffer.MatricesIndicesKind:
  41. this._strideSize = 4;
  42. break;
  43. case VertexBuffer.MatricesWeightsKind:
  44. this._strideSize = 4;
  45. break;
  46. }
  47. }
  48. // Properties
  49. VertexBuffer.prototype.isUpdatable = function () {
  50. return this._updatable;
  51. };
  52. VertexBuffer.prototype.getData = function () {
  53. return this._data;
  54. };
  55. VertexBuffer.prototype.getBuffer = function () {
  56. return this._buffer;
  57. };
  58. VertexBuffer.prototype.getStrideSize = function () {
  59. return this._strideSize;
  60. };
  61. // Methods
  62. VertexBuffer.prototype.create = function (data) {
  63. if (!data && this._buffer) {
  64. return; // nothing to do
  65. }
  66. data = data || this._data;
  67. if (!this._buffer) {
  68. if (this._updatable) {
  69. this._buffer = this._engine.createDynamicVertexBuffer(data.length * 4);
  70. }
  71. else {
  72. this._buffer = this._engine.createVertexBuffer(data);
  73. }
  74. }
  75. if (this._updatable) {
  76. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  77. this._data = data;
  78. }
  79. };
  80. VertexBuffer.prototype.update = function (data) {
  81. this.create(data);
  82. };
  83. VertexBuffer.prototype.updateDirectly = function (data, offset) {
  84. if (!this._buffer) {
  85. return;
  86. }
  87. if (this._updatable) {
  88. this._engine.updateDynamicVertexBuffer(this._buffer, data, offset);
  89. this._data = null;
  90. }
  91. };
  92. VertexBuffer.prototype.dispose = function () {
  93. if (!this._buffer) {
  94. return;
  95. }
  96. if (this._engine._releaseBuffer(this._buffer)) {
  97. this._buffer = null;
  98. }
  99. };
  100. Object.defineProperty(VertexBuffer, "PositionKind", {
  101. get: function () {
  102. return VertexBuffer._PositionKind;
  103. },
  104. enumerable: true,
  105. configurable: true
  106. });
  107. Object.defineProperty(VertexBuffer, "NormalKind", {
  108. get: function () {
  109. return VertexBuffer._NormalKind;
  110. },
  111. enumerable: true,
  112. configurable: true
  113. });
  114. Object.defineProperty(VertexBuffer, "UVKind", {
  115. get: function () {
  116. return VertexBuffer._UVKind;
  117. },
  118. enumerable: true,
  119. configurable: true
  120. });
  121. Object.defineProperty(VertexBuffer, "UV2Kind", {
  122. get: function () {
  123. return VertexBuffer._UV2Kind;
  124. },
  125. enumerable: true,
  126. configurable: true
  127. });
  128. Object.defineProperty(VertexBuffer, "UV3Kind", {
  129. get: function () {
  130. return VertexBuffer._UV3Kind;
  131. },
  132. enumerable: true,
  133. configurable: true
  134. });
  135. Object.defineProperty(VertexBuffer, "UV4Kind", {
  136. get: function () {
  137. return VertexBuffer._UV4Kind;
  138. },
  139. enumerable: true,
  140. configurable: true
  141. });
  142. Object.defineProperty(VertexBuffer, "UV5Kind", {
  143. get: function () {
  144. return VertexBuffer._UV5Kind;
  145. },
  146. enumerable: true,
  147. configurable: true
  148. });
  149. Object.defineProperty(VertexBuffer, "UV6Kind", {
  150. get: function () {
  151. return VertexBuffer._UV6Kind;
  152. },
  153. enumerable: true,
  154. configurable: true
  155. });
  156. Object.defineProperty(VertexBuffer, "ColorKind", {
  157. get: function () {
  158. return VertexBuffer._ColorKind;
  159. },
  160. enumerable: true,
  161. configurable: true
  162. });
  163. Object.defineProperty(VertexBuffer, "MatricesIndicesKind", {
  164. get: function () {
  165. return VertexBuffer._MatricesIndicesKind;
  166. },
  167. enumerable: true,
  168. configurable: true
  169. });
  170. Object.defineProperty(VertexBuffer, "MatricesWeightsKind", {
  171. get: function () {
  172. return VertexBuffer._MatricesWeightsKind;
  173. },
  174. enumerable: true,
  175. configurable: true
  176. });
  177. // Enums
  178. VertexBuffer._PositionKind = "position";
  179. VertexBuffer._NormalKind = "normal";
  180. VertexBuffer._UVKind = "uv";
  181. VertexBuffer._UV2Kind = "uv2";
  182. VertexBuffer._UV3Kind = "uv3";
  183. VertexBuffer._UV4Kind = "uv4";
  184. VertexBuffer._UV5Kind = "uv5";
  185. VertexBuffer._UV6Kind = "uv6";
  186. VertexBuffer._ColorKind = "color";
  187. VertexBuffer._MatricesIndicesKind = "matricesIndices";
  188. VertexBuffer._MatricesWeightsKind = "matricesWeights";
  189. return VertexBuffer;
  190. })();
  191. BABYLON.VertexBuffer = VertexBuffer;
  192. })(BABYLON || (BABYLON = {}));
  193. //# sourceMappingURL=babylon.vertexBuffer.js.map