greasedLineBaseMesh.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { GreasedLinePluginMaterial } from "../../Materials/GreasedLine/greasedLinePluginMaterial.js";
  2. import { Mesh } from "../mesh.js";
  3. import { Buffer } from "../../Buffers/buffer.js";
  4. import { VertexData } from "../mesh.vertexData.js";
  5. import { DeepCopier } from "../../Misc/deepCopier.js";
  6. import { GreasedLineSimpleMaterial } from "../../Materials/GreasedLine/greasedLineSimpleMaterial.js";
  7. /**
  8. * In POINTS_MODE_POINTS every array of points will become the center (backbone) of the ribbon. The ribbon will be expanded by `width / 2` to `+direction` and `-direction` as well.
  9. * In POINTS_MODE_PATHS every array of points specifies an edge. These will be used to build one ribbon.
  10. */
  11. export var GreasedLineRibbonPointsMode;
  12. (function (GreasedLineRibbonPointsMode) {
  13. GreasedLineRibbonPointsMode[GreasedLineRibbonPointsMode["POINTS_MODE_POINTS"] = 0] = "POINTS_MODE_POINTS";
  14. GreasedLineRibbonPointsMode[GreasedLineRibbonPointsMode["POINTS_MODE_PATHS"] = 1] = "POINTS_MODE_PATHS";
  15. })(GreasedLineRibbonPointsMode || (GreasedLineRibbonPointsMode = {}));
  16. /**
  17. * FACES_MODE_SINGLE_SIDED single sided with back face culling. Default value.
  18. * FACES_MODE_SINGLE_SIDED_NO_BACKFACE_CULLING single sided without back face culling. Sets backFaceCulling = false on the material so it affects all line ribbons added to the line ribbon instance.
  19. * FACES_MODE_DOUBLE_SIDED extra back faces are created. This doubles the amount of faces of the mesh.
  20. */
  21. export var GreasedLineRibbonFacesMode;
  22. (function (GreasedLineRibbonFacesMode) {
  23. GreasedLineRibbonFacesMode[GreasedLineRibbonFacesMode["FACES_MODE_SINGLE_SIDED"] = 0] = "FACES_MODE_SINGLE_SIDED";
  24. GreasedLineRibbonFacesMode[GreasedLineRibbonFacesMode["FACES_MODE_SINGLE_SIDED_NO_BACKFACE_CULLING"] = 1] = "FACES_MODE_SINGLE_SIDED_NO_BACKFACE_CULLING";
  25. GreasedLineRibbonFacesMode[GreasedLineRibbonFacesMode["FACES_MODE_DOUBLE_SIDED"] = 2] = "FACES_MODE_DOUBLE_SIDED";
  26. })(GreasedLineRibbonFacesMode || (GreasedLineRibbonFacesMode = {}));
  27. /**
  28. * Only with POINTS_MODE_PATHS.
  29. * AUTO_DIRECTIONS_FROM_FIRST_SEGMENT sets the direction (slope) of the ribbon from the direction of the first line segment. Recommended.
  30. * AUTO_DIRECTIONS_FROM_ALL_SEGMENTS in this mode the direction (slope) will be calculated for each line segment according to the direction vector between each point of the line segments. Slow method.
  31. * AUTO_DIRECTIONS_ENHANCED in this mode the direction (slope) will be calculated for each line segment according to the direction vector between each point of the line segments using a more sophisitcaed algorithm. Slowest method.
  32. * AUTO_DIRECTIONS_FACE_TO in this mode the direction (slope) will be calculated for each line segment according to the direction vector between each point of the line segments and a direction (face-to) vector specified in direction. The resulting line will face to the direction of this face-to vector.
  33. * AUTO_DIRECTIONS_NONE you have to set the direction (slope) manually. Recommended.
  34. */
  35. export var GreasedLineRibbonAutoDirectionMode;
  36. (function (GreasedLineRibbonAutoDirectionMode) {
  37. GreasedLineRibbonAutoDirectionMode[GreasedLineRibbonAutoDirectionMode["AUTO_DIRECTIONS_FROM_FIRST_SEGMENT"] = 0] = "AUTO_DIRECTIONS_FROM_FIRST_SEGMENT";
  38. GreasedLineRibbonAutoDirectionMode[GreasedLineRibbonAutoDirectionMode["AUTO_DIRECTIONS_FROM_ALL_SEGMENTS"] = 1] = "AUTO_DIRECTIONS_FROM_ALL_SEGMENTS";
  39. GreasedLineRibbonAutoDirectionMode[GreasedLineRibbonAutoDirectionMode["AUTO_DIRECTIONS_ENHANCED"] = 2] = "AUTO_DIRECTIONS_ENHANCED";
  40. GreasedLineRibbonAutoDirectionMode[GreasedLineRibbonAutoDirectionMode["AUTO_DIRECTIONS_FACE_TO"] = 3] = "AUTO_DIRECTIONS_FACE_TO";
  41. GreasedLineRibbonAutoDirectionMode[GreasedLineRibbonAutoDirectionMode["AUTO_DIRECTIONS_NONE"] = 99] = "AUTO_DIRECTIONS_NONE";
  42. })(GreasedLineRibbonAutoDirectionMode || (GreasedLineRibbonAutoDirectionMode = {}));
  43. /**
  44. * GreasedLineBaseMesh
  45. */
  46. export class GreasedLineBaseMesh extends Mesh {
  47. constructor(name, scene, _options) {
  48. super(name, scene, null, null, false, false);
  49. this.name = name;
  50. this._options = _options;
  51. this._lazy = false;
  52. this._updatable = false;
  53. this._engine = scene.getEngine();
  54. this._lazy = _options.lazy ?? false;
  55. this._updatable = _options.updatable ?? false;
  56. this._vertexPositions = [];
  57. this._indices = [];
  58. this._uvs = [];
  59. this._points = [];
  60. this._colorPointers = _options.colorPointers ?? [];
  61. this._widths = _options.widths ?? new Array(_options.points.length).fill(1);
  62. }
  63. /**
  64. * "GreasedLineMesh"
  65. * @returns "GreasedLineMesh"
  66. */
  67. getClassName() {
  68. return "GreasedLineMesh";
  69. }
  70. _updateWidthsWithValue(defaulValue) {
  71. let pointCount = 0;
  72. for (const points of this._points) {
  73. pointCount += points.length;
  74. }
  75. const countDiff = (pointCount / 3) * 2 - this._widths.length;
  76. for (let i = 0; i < countDiff; i++) {
  77. this._widths.push(defaulValue);
  78. }
  79. }
  80. /**
  81. * Updated a lazy line. Rerenders the line and updates boundinfo as well.
  82. */
  83. updateLazy() {
  84. this._setPoints(this._points);
  85. if (!this._options.colorPointers) {
  86. this._updateColorPointers();
  87. }
  88. this._createVertexBuffers(this._options.ribbonOptions?.smoothShading);
  89. this.refreshBoundingInfo();
  90. this.greasedLineMaterial?.updateLazy();
  91. }
  92. /**
  93. * Adds new points to the line. It doesn't rerenders the line if in lazy mode.
  94. * @param points points table
  95. * @param options optional options
  96. */
  97. addPoints(points, options) {
  98. for (const p of points) {
  99. this._points.push(p);
  100. }
  101. if (!this._lazy) {
  102. this.setPoints(this._points, options);
  103. }
  104. }
  105. /**
  106. * Dispose the line and it's resources
  107. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  108. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  109. */
  110. dispose(doNotRecurse, disposeMaterialAndTextures = false) {
  111. super.dispose(doNotRecurse, disposeMaterialAndTextures);
  112. }
  113. /**
  114. * @returns true if the mesh was created in lazy mode
  115. */
  116. isLazy() {
  117. return this._lazy;
  118. }
  119. /**
  120. * Returns the UVs
  121. */
  122. get uvs() {
  123. return this._uvs;
  124. }
  125. /**
  126. * Sets the UVs
  127. * @param uvs the UVs
  128. */
  129. set uvs(uvs) {
  130. this._uvs = uvs instanceof Float32Array ? uvs : new Float32Array(uvs);
  131. this._createVertexBuffers();
  132. }
  133. /**
  134. * Returns the points offsets
  135. * Return the points offsets
  136. */
  137. get offsets() {
  138. return this._offsets;
  139. }
  140. /**
  141. * Sets point offests
  142. * @param offsets offset table [x,y,z, x,y,z, ....]
  143. */
  144. set offsets(offsets) {
  145. this._offsets = offsets;
  146. if (!this._offsetsBuffer) {
  147. this._createOffsetsBuffer(offsets);
  148. }
  149. else {
  150. this._offsetsBuffer.update(offsets);
  151. }
  152. }
  153. /**
  154. * Gets widths at each line point like [widthLower, widthUpper, widthLower, widthUpper, ...]
  155. */
  156. get widths() {
  157. return this._widths;
  158. }
  159. /**
  160. * Sets widths at each line point
  161. * @param widths width table [widthLower, widthUpper, widthLower, widthUpper ...]
  162. */
  163. set widths(widths) {
  164. this._widths = widths;
  165. if (!this._lazy) {
  166. this._widthsBuffer && this._widthsBuffer.update(widths);
  167. }
  168. }
  169. /**
  170. * Gets the color pointer. Each vertex need a color pointer. These color pointers points to the colors in the color table @see colors
  171. */
  172. get colorPointers() {
  173. return this._colorPointers;
  174. }
  175. /**
  176. * Sets the color pointer
  177. * @param colorPointers array of color pointer in the colors array. One pointer for every vertex is needed.
  178. */
  179. set colorPointers(colorPointers) {
  180. this._colorPointers = colorPointers;
  181. if (!this._lazy) {
  182. this._colorPointersBuffer && this._colorPointersBuffer.update(colorPointers);
  183. }
  184. }
  185. /**
  186. * Gets the pluginMaterial associated with line
  187. */
  188. get greasedLineMaterial() {
  189. if (this.material && this.material instanceof GreasedLineSimpleMaterial) {
  190. return this.material;
  191. }
  192. const materialPlugin = this.material?.pluginManager?.getPlugin(GreasedLinePluginMaterial.GREASED_LINE_MATERIAL_NAME);
  193. if (materialPlugin) {
  194. return materialPlugin;
  195. }
  196. return;
  197. }
  198. /**
  199. * Return copy the points.
  200. */
  201. get points() {
  202. const pointsCopy = [];
  203. DeepCopier.DeepCopy(this._points, pointsCopy);
  204. return pointsCopy;
  205. }
  206. /**
  207. * Sets line points and rerenders the line.
  208. * @param points points table
  209. * @param options optional options
  210. */
  211. setPoints(points, options) {
  212. this._points = points;
  213. this._updateWidths();
  214. if (!options?.colorPointers) {
  215. this._updateColorPointers();
  216. }
  217. this._setPoints(points, options);
  218. }
  219. _initGreasedLine() {
  220. this._vertexPositions = [];
  221. this._indices = [];
  222. this._uvs = [];
  223. }
  224. _createLineOptions() {
  225. const lineOptions = {
  226. points: this._points,
  227. colorPointers: this._colorPointers,
  228. lazy: this._lazy,
  229. updatable: this._updatable,
  230. uvs: this._uvs,
  231. widths: this._widths,
  232. ribbonOptions: this._options.ribbonOptions,
  233. };
  234. return lineOptions;
  235. }
  236. /**
  237. * Serializes this GreasedLineMesh
  238. * @param serializationObject object to write serialization to
  239. */
  240. serialize(serializationObject) {
  241. super.serialize(serializationObject);
  242. serializationObject.type = this.getClassName();
  243. serializationObject.lineOptions = this._createLineOptions();
  244. }
  245. _createVertexBuffers(computeNormals = false) {
  246. const vertexData = new VertexData();
  247. vertexData.positions = this._vertexPositions;
  248. vertexData.indices = this._indices;
  249. vertexData.uvs = this._uvs;
  250. if (computeNormals) {
  251. vertexData.normals = [];
  252. VertexData.ComputeNormals(this._vertexPositions, this._indices, vertexData.normals);
  253. }
  254. vertexData.applyToMesh(this, this._options.updatable);
  255. return vertexData;
  256. }
  257. _createOffsetsBuffer(offsets) {
  258. const engine = this._scene.getEngine();
  259. const offsetBuffer = new Buffer(engine, offsets, this._updatable, 3);
  260. this.setVerticesBuffer(offsetBuffer.createVertexBuffer("grl_offsets", 0, 3));
  261. this._offsetsBuffer = offsetBuffer;
  262. }
  263. }
  264. //# sourceMappingURL=greasedLineBaseMesh.js.map