greasedLinePluginMaterial.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. import { RawTexture } from "../Textures/rawTexture.js";
  2. import { MaterialPluginBase } from "../materialPluginBase.js";
  3. import { Vector2, TmpVectors } from "../../Maths/math.vector.js";
  4. import { MaterialDefines } from "../materialDefines.js";
  5. import { RegisterClass } from "../../Misc/typeStore.js";
  6. import { GreasedLineMeshColorDistributionType, GreasedLineMeshColorMode } from "./greasedLineMaterialInterfaces.js";
  7. import { GreasedLineMaterialDefaults } from "./greasedLineMaterialDefaults.js";
  8. import { GreasedLineTools } from "../../Misc/greasedLineTools.js";
  9. /**
  10. * @internal
  11. */
  12. export class MaterialGreasedLineDefines extends MaterialDefines {
  13. constructor() {
  14. super(...arguments);
  15. /**
  16. * The material has a color option specified
  17. */
  18. // eslint-disable-next-line @typescript-eslint/naming-convention
  19. this.GREASED_LINE_HAS_COLOR = false;
  20. /**
  21. * The material's size attenuation optiom
  22. */
  23. // eslint-disable-next-line @typescript-eslint/naming-convention
  24. this.GREASED_LINE_SIZE_ATTENUATION = false;
  25. /**
  26. * The type of color distribution is set to line this value equals to true.
  27. */
  28. // eslint-disable-next-line @typescript-eslint/naming-convention
  29. this.GREASED_LINE_COLOR_DISTRIBUTION_TYPE_LINE = false;
  30. /**
  31. * True if scene is in right handed coordinate system.
  32. */
  33. // eslint-disable-next-line @typescript-eslint/naming-convention
  34. this.GREASED_LINE_RIGHT_HANDED_COORDINATE_SYSTEM = false;
  35. /**
  36. * True if the line is in camera facing mode
  37. */
  38. // eslint-disable-next-line @typescript-eslint/naming-convention
  39. this.GREASED_LINE_CAMERA_FACING = true;
  40. }
  41. }
  42. /**
  43. * GreasedLinePluginMaterial for GreasedLineMesh/GreasedLineRibbonMesh.
  44. * Use the GreasedLineBuilder.CreateGreasedLineMaterial function to create and instance of this class.
  45. */
  46. export class GreasedLinePluginMaterial extends MaterialPluginBase {
  47. /**
  48. * Creates a new instance of the GreasedLinePluginMaterial
  49. * @param material base material for the plugin
  50. * @param scene the scene
  51. * @param options plugin options
  52. */
  53. constructor(material, scene, options) {
  54. options = options || {
  55. color: GreasedLineMaterialDefaults.DEFAULT_COLOR,
  56. };
  57. const defines = new MaterialGreasedLineDefines();
  58. defines.GREASED_LINE_HAS_COLOR = !!options.color && !options.useColors;
  59. defines.GREASED_LINE_SIZE_ATTENUATION = options.sizeAttenuation ?? false;
  60. defines.GREASED_LINE_COLOR_DISTRIBUTION_TYPE_LINE = options.colorDistributionType === GreasedLineMeshColorDistributionType.COLOR_DISTRIBUTION_TYPE_LINE;
  61. defines.GREASED_LINE_RIGHT_HANDED_COORDINATE_SYSTEM = (scene ?? material.getScene()).useRightHandedSystem;
  62. defines.GREASED_LINE_CAMERA_FACING = options.cameraFacing ?? true;
  63. super(material, GreasedLinePluginMaterial.GREASED_LINE_MATERIAL_NAME, 200, defines);
  64. /**
  65. * You can provide a colorsTexture to use instead of one generated from the 'colors' option
  66. */
  67. this.colorsTexture = null;
  68. this._scene = scene ?? material.getScene();
  69. this._engine = this._scene.getEngine();
  70. this._cameraFacing = options.cameraFacing ?? true;
  71. this.visibility = options.visibility ?? 1;
  72. this.useDash = options.useDash ?? false;
  73. this.dashRatio = options.dashRatio ?? 0.5;
  74. this.dashOffset = options.dashOffset ?? 0;
  75. this.width = options.width ? options.width : options.sizeAttenuation ? GreasedLineMaterialDefaults.DEFAULT_WIDTH_ATTENUATED : GreasedLineMaterialDefaults.DEFAULT_WIDTH;
  76. this._sizeAttenuation = options.sizeAttenuation ?? false;
  77. this.colorMode = options.colorMode ?? GreasedLineMeshColorMode.COLOR_MODE_SET;
  78. this._color = options.color ?? null;
  79. this.useColors = options.useColors ?? false;
  80. this._colorsDistributionType = options.colorDistributionType ?? GreasedLineMeshColorDistributionType.COLOR_DISTRIBUTION_TYPE_SEGMENT;
  81. this.colorsSampling = options.colorsSampling ?? RawTexture.NEAREST_NEAREST;
  82. this._colors = options.colors ?? null;
  83. this.dashCount = options.dashCount ?? 1; // calculate the _dashArray value, call the setter
  84. this.resolution = options.resolution ?? new Vector2(this._engine.getRenderWidth(), this._engine.getRenderHeight()); // calculate aspect call the setter
  85. if (options.colorsTexture) {
  86. this.colorsTexture = options.colorsTexture; // colorsTexture from options takes precedence
  87. }
  88. else {
  89. if (this._colors) {
  90. this.colorsTexture = GreasedLineTools.CreateColorsTexture(`${material.name}-colors-texture`, this._colors, this.colorsSampling, this._scene);
  91. }
  92. else {
  93. this._color = this._color ?? GreasedLineMaterialDefaults.DEFAULT_COLOR;
  94. GreasedLineTools.PrepareEmptyColorsTexture(this._scene);
  95. }
  96. }
  97. this._engine.onDisposeObservable.add(() => {
  98. GreasedLineTools.DisposeEmptyColorsTexture();
  99. });
  100. this._enable(true); // always enabled
  101. }
  102. /**
  103. * Get the shader attributes
  104. * @param attributes array which will be filled with the attributes
  105. */
  106. getAttributes(attributes) {
  107. attributes.push("grl_offsets");
  108. attributes.push("grl_widths");
  109. attributes.push("grl_colorPointers");
  110. attributes.push("grl_counters");
  111. if (this._cameraFacing) {
  112. attributes.push("grl_previousAndSide");
  113. attributes.push("grl_nextAndCounters");
  114. }
  115. else {
  116. attributes.push("grl_slopes");
  117. }
  118. }
  119. /**
  120. * Get the shader samplers
  121. * @param samplers
  122. */
  123. getSamplers(samplers) {
  124. samplers.push("grl_colors");
  125. }
  126. /**
  127. * Get the shader textures
  128. * @param activeTextures array which will be filled with the textures
  129. */
  130. getActiveTextures(activeTextures) {
  131. if (this.colorsTexture) {
  132. activeTextures.push(this.colorsTexture);
  133. }
  134. }
  135. /**
  136. * Get the shader uniforms
  137. * @returns uniforms
  138. */
  139. getUniforms() {
  140. const ubo = [
  141. { name: "grl_singleColor", size: 3, type: "vec3" },
  142. { name: "grl_dashOptions", size: 4, type: "vec4" },
  143. { name: "grl_colorMode_visibility_colorsWidth_useColors", size: 4, type: "vec4" },
  144. ];
  145. if (this._cameraFacing) {
  146. ubo.push({ name: "grl_projection", size: 16, type: "mat4" }, { name: "grl_aspect_resolution_lineWidth", size: 4, type: "vec4" });
  147. }
  148. return {
  149. ubo,
  150. vertex: this._cameraFacing
  151. ? `
  152. uniform vec4 grl_aspect_resolution_lineWidth;
  153. uniform mat4 grl_projection;
  154. `
  155. : "",
  156. fragment: `
  157. uniform vec4 grl_dashOptions;
  158. uniform vec4 grl_colorMode_visibility_colorsWidth_useColors;
  159. uniform vec3 grl_singleColor;
  160. `,
  161. };
  162. }
  163. // only getter, it doesn't make sense to use this plugin on a mesh other than GreasedLineMesh
  164. // and it doesn't make sense to disable it on the mesh
  165. get isEnabled() {
  166. return true;
  167. }
  168. /**
  169. * Bind the uniform buffer
  170. * @param uniformBuffer
  171. */
  172. bindForSubMesh(uniformBuffer) {
  173. if (this._cameraFacing) {
  174. const activeCamera = this._scene.activeCamera;
  175. if (activeCamera) {
  176. const projection = activeCamera.getProjectionMatrix();
  177. uniformBuffer.updateMatrix("grl_projection", projection);
  178. }
  179. else {
  180. throw Error("GreasedLinePluginMaterial requires an active camera.");
  181. }
  182. const resolutionLineWidth = TmpVectors.Vector4[0];
  183. resolutionLineWidth.x = this._aspect;
  184. resolutionLineWidth.y = this._resolution.x;
  185. resolutionLineWidth.z = this._resolution.y;
  186. resolutionLineWidth.w = this.width;
  187. uniformBuffer.updateVector4("grl_aspect_resolution_lineWidth", resolutionLineWidth);
  188. }
  189. const dashOptions = TmpVectors.Vector4[0];
  190. dashOptions.x = GreasedLineTools.BooleanToNumber(this.useDash);
  191. dashOptions.y = this._dashArray;
  192. dashOptions.z = this.dashOffset;
  193. dashOptions.w = this.dashRatio;
  194. uniformBuffer.updateVector4("grl_dashOptions", dashOptions);
  195. const colorModeVisibilityColorsWidthUseColors = TmpVectors.Vector4[1];
  196. colorModeVisibilityColorsWidthUseColors.x = this.colorMode;
  197. colorModeVisibilityColorsWidthUseColors.y = this.visibility;
  198. colorModeVisibilityColorsWidthUseColors.z = this.colorsTexture ? this.colorsTexture.getSize().width : 0;
  199. colorModeVisibilityColorsWidthUseColors.w = GreasedLineTools.BooleanToNumber(this.useColors);
  200. uniformBuffer.updateVector4("grl_colorMode_visibility_colorsWidth_useColors", colorModeVisibilityColorsWidthUseColors);
  201. if (this._color) {
  202. uniformBuffer.updateColor3("grl_singleColor", this._color);
  203. }
  204. uniformBuffer.setTexture("grl_colors", this.colorsTexture ?? GreasedLineMaterialDefaults.EmptyColorsTexture);
  205. }
  206. /**
  207. * Prepare the defines
  208. * @param defines
  209. * @param _scene
  210. * @param _mesh
  211. */
  212. prepareDefines(defines, _scene, _mesh) {
  213. defines.GREASED_LINE_HAS_COLOR = !!this.color && !this.useColors;
  214. defines.GREASED_LINE_SIZE_ATTENUATION = this._sizeAttenuation;
  215. defines.GREASED_LINE_COLOR_DISTRIBUTION_TYPE_LINE = this._colorsDistributionType === GreasedLineMeshColorDistributionType.COLOR_DISTRIBUTION_TYPE_LINE;
  216. defines.GREASED_LINE_RIGHT_HANDED_COORDINATE_SYSTEM = _scene.useRightHandedSystem;
  217. defines.GREASED_LINE_CAMERA_FACING = this._cameraFacing;
  218. }
  219. /**
  220. * Get the class name
  221. * @returns class name
  222. */
  223. getClassName() {
  224. return GreasedLinePluginMaterial.GREASED_LINE_MATERIAL_NAME;
  225. }
  226. /**
  227. * Get shader code
  228. * @param shaderType vertex/fragment
  229. * @returns shader code
  230. */
  231. getCustomCode(shaderType) {
  232. if (shaderType === "vertex") {
  233. const obj = {
  234. // eslint-disable-next-line @typescript-eslint/naming-convention
  235. CUSTOM_VERTEX_DEFINITIONS: `
  236. attribute float grl_widths;
  237. attribute vec3 grl_offsets;
  238. attribute float grl_colorPointers;
  239. varying float grlCounters;
  240. varying float grlColorPointer;
  241. #ifdef GREASED_LINE_CAMERA_FACING
  242. attribute vec4 grl_previousAndSide;
  243. attribute vec4 grl_nextAndCounters;
  244. vec2 grlFix( vec4 i, float aspect ) {
  245. vec2 res = i.xy / i.w;
  246. res.x *= aspect;
  247. return res;
  248. }
  249. #else
  250. attribute vec3 grl_slopes;
  251. attribute float grl_counters;
  252. #endif
  253. `,
  254. // eslint-disable-next-line @typescript-eslint/naming-convention
  255. CUSTOM_VERTEX_UPDATE_POSITION: `
  256. #ifdef GREASED_LINE_CAMERA_FACING
  257. vec3 grlPositionOffset = grl_offsets;
  258. positionUpdated += grlPositionOffset;
  259. #else
  260. positionUpdated = (positionUpdated + grl_offsets) + (grl_slopes * grl_widths);
  261. #endif
  262. `,
  263. // eslint-disable-next-line @typescript-eslint/naming-convention
  264. CUSTOM_VERTEX_MAIN_END: `
  265. grlColorPointer = grl_colorPointers;
  266. #ifdef GREASED_LINE_CAMERA_FACING
  267. float grlAspect = grl_aspect_resolution_lineWidth.x;
  268. float grlBaseWidth = grl_aspect_resolution_lineWidth.w;
  269. vec3 grlPrevious = grl_previousAndSide.xyz;
  270. float grlSide = grl_previousAndSide.w;
  271. vec3 grlNext = grl_nextAndCounters.xyz;
  272. grlCounters = grl_nextAndCounters.w;
  273. mat4 grlMatrix = viewProjection * finalWorld;
  274. vec4 grlFinalPosition = grlMatrix * vec4( positionUpdated , 1.0 );
  275. vec4 grlPrevPos = grlMatrix * vec4( grlPrevious + grlPositionOffset, 1.0 );
  276. vec4 grlNextPos = grlMatrix * vec4( grlNext + grlPositionOffset, 1.0 );
  277. vec2 grlCurrentP = grlFix( grlFinalPosition, grlAspect );
  278. vec2 grlPrevP = grlFix( grlPrevPos, grlAspect );
  279. vec2 grlNextP = grlFix( grlNextPos, grlAspect );
  280. float grlWidth = grlBaseWidth * grl_widths;
  281. vec2 grlDir;
  282. if( grlNextP == grlCurrentP ) grlDir = normalize( grlCurrentP - grlPrevP );
  283. else if( grlPrevP == grlCurrentP ) grlDir = normalize( grlNextP - grlCurrentP );
  284. else {
  285. vec2 grlDir1 = normalize( grlCurrentP - grlPrevP );
  286. vec2 grlDir2 = normalize( grlNextP - grlCurrentP );
  287. grlDir = normalize( grlDir1 + grlDir2 );
  288. }
  289. vec4 grlNormal = vec4( -grlDir.y, grlDir.x, 0., 1. );
  290. #ifdef GREASED_LINE_RIGHT_HANDED_COORDINATE_SYSTEM
  291. grlNormal.xy *= -.5 * grlWidth;
  292. #else
  293. grlNormal.xy *= .5 * grlWidth;
  294. #endif
  295. grlNormal *= grl_projection;
  296. #ifdef GREASED_LINE_SIZE_ATTENUATION
  297. grlNormal.xy *= grlFinalPosition.w;
  298. grlNormal.xy /= ( vec4( grl_aspect_resolution_lineWidth.yz, 0., 1. ) * grl_projection ).xy;
  299. #endif
  300. grlFinalPosition.xy += grlNormal.xy * grlSide;
  301. gl_Position = grlFinalPosition;
  302. vPositionW = vec3(grlFinalPosition);
  303. #else
  304. grlCounters = grl_counters;
  305. #endif
  306. `,
  307. };
  308. this._cameraFacing && (obj["!gl_Position\\=viewProjection\\*worldPos;"] = "//"); // not needed for camera facing GRL
  309. return obj;
  310. }
  311. if (shaderType === "fragment") {
  312. return {
  313. // eslint-disable-next-line @typescript-eslint/naming-convention
  314. CUSTOM_FRAGMENT_DEFINITIONS: `
  315. varying float grlCounters;
  316. varying float grlColorPointer;
  317. uniform sampler2D grl_colors;
  318. `,
  319. // eslint-disable-next-line @typescript-eslint/naming-convention
  320. CUSTOM_FRAGMENT_MAIN_END: `
  321. float grlColorMode = grl_colorMode_visibility_colorsWidth_useColors.x;
  322. float grlVisibility = grl_colorMode_visibility_colorsWidth_useColors.y;
  323. float grlColorsWidth = grl_colorMode_visibility_colorsWidth_useColors.z;
  324. float grlUseColors = grl_colorMode_visibility_colorsWidth_useColors.w;
  325. float grlUseDash = grl_dashOptions.x;
  326. float grlDashArray = grl_dashOptions.y;
  327. float grlDashOffset = grl_dashOptions.z;
  328. float grlDashRatio = grl_dashOptions.w;
  329. gl_FragColor.a *= step(grlCounters, grlVisibility);
  330. if( gl_FragColor.a == 0. ) discard;
  331. if(grlUseDash == 1.){
  332. gl_FragColor.a *= ceil(mod(grlCounters + grlDashOffset, grlDashArray) - (grlDashArray * grlDashRatio));
  333. if (gl_FragColor.a == 0.) discard;
  334. }
  335. #ifdef GREASED_LINE_HAS_COLOR
  336. if (grlColorMode == ${GreasedLineMeshColorMode.COLOR_MODE_SET}.) {
  337. gl_FragColor.rgb = grl_singleColor;
  338. } else if (grlColorMode == ${GreasedLineMeshColorMode.COLOR_MODE_ADD}.) {
  339. gl_FragColor.rgb += grl_singleColor;
  340. } else if (grlColorMode == ${GreasedLineMeshColorMode.COLOR_MODE_MULTIPLY}.) {
  341. gl_FragColor.rgb *= grl_singleColor;
  342. }
  343. #else
  344. if (grlUseColors == 1.) {
  345. #ifdef GREASED_LINE_COLOR_DISTRIBUTION_TYPE_LINE
  346. vec4 grlColor = texture2D(grl_colors, vec2(grlCounters, 0.), 0.);
  347. #else
  348. vec4 grlColor = texture2D(grl_colors, vec2(grlColorPointer/grlColorsWidth, 0.), 0.);
  349. #endif
  350. if (grlColorMode == ${GreasedLineMeshColorMode.COLOR_MODE_SET}.) {
  351. gl_FragColor = grlColor;
  352. } else if (grlColorMode == ${GreasedLineMeshColorMode.COLOR_MODE_ADD}.) {
  353. gl_FragColor += grlColor;
  354. } else if (grlColorMode == ${GreasedLineMeshColorMode.COLOR_MODE_MULTIPLY}.) {
  355. gl_FragColor *= grlColor;
  356. }
  357. }
  358. #endif
  359. `,
  360. };
  361. }
  362. return null;
  363. }
  364. /**
  365. * Disposes the plugin material.
  366. */
  367. dispose() {
  368. this.colorsTexture?.dispose();
  369. super.dispose();
  370. }
  371. /**
  372. * Returns the colors used to colorize the line
  373. */
  374. get colors() {
  375. return this._colors;
  376. }
  377. /**
  378. * Sets the colors used to colorize the line
  379. */
  380. set colors(value) {
  381. this.setColors(value);
  382. }
  383. /**
  384. * Creates or updates the colors texture
  385. * @param colors color table RGBA
  386. * @param lazy if lazy, the colors are not updated
  387. * @param forceNewTexture force creation of a new texture
  388. */
  389. setColors(colors, lazy = false, forceNewTexture = false) {
  390. const origColorsCount = this._colors?.length ?? 0;
  391. this._colors = colors;
  392. if (colors === null || colors.length === 0) {
  393. this.colorsTexture?.dispose();
  394. return;
  395. }
  396. if (lazy && !forceNewTexture) {
  397. return;
  398. }
  399. if (this.colorsTexture && origColorsCount === colors.length && !forceNewTexture) {
  400. const colorArray = GreasedLineTools.Color3toRGBAUint8(colors);
  401. this.colorsTexture.update(colorArray);
  402. }
  403. else {
  404. this.colorsTexture?.dispose();
  405. this.colorsTexture = GreasedLineTools.CreateColorsTexture(`${this._material.name}-colors-texture`, colors, this.colorsSampling, this._scene);
  406. }
  407. }
  408. /**
  409. * Updates the material. Use when material created in lazy mode.
  410. */
  411. updateLazy() {
  412. if (this._colors) {
  413. this.setColors(this._colors, false, true);
  414. }
  415. }
  416. /**
  417. * Gets the number of dashes in the line
  418. */
  419. get dashCount() {
  420. return this._dashCount;
  421. }
  422. /**
  423. * Sets the number of dashes in the line
  424. * @param value dash
  425. */
  426. set dashCount(value) {
  427. this._dashCount = value;
  428. this._dashArray = 1 / value;
  429. }
  430. /**
  431. * If set to true the line will be rendered always with the same width regardless how far it is located from the camera.
  432. * Not supported for non camera facing lines.
  433. */
  434. get sizeAttenuation() {
  435. return this._sizeAttenuation;
  436. }
  437. /**
  438. * Turn on/off size attenuation of the width option and widths array.
  439. * Not supported for non camera facing lines.
  440. * @param value If set to true the line will be rendered always with the same width regardless how far it is located from the camera.
  441. */
  442. set sizeAttenuation(value) {
  443. this._sizeAttenuation = value;
  444. this.markAllDefinesAsDirty();
  445. }
  446. /**
  447. * Gets the color of the line
  448. */
  449. get color() {
  450. return this._color;
  451. }
  452. /**
  453. * Sets the color of the line
  454. * @param value Color3 or null to clear the color. You need to clear the color if you use colors and useColors = true
  455. */
  456. set color(value) {
  457. this.setColor(value);
  458. }
  459. /**
  460. * Sets the color of the line. If set the whole line will be mixed with this color according to the colorMode option.
  461. * @param value color
  462. * @param doNotMarkDirty if true, the material will not be marked as dirty
  463. */
  464. setColor(value, doNotMarkDirty = false) {
  465. if ((this._color === null && value !== null) || (this._color !== null && value === null)) {
  466. this._color = value;
  467. !doNotMarkDirty && this.markAllDefinesAsDirty();
  468. }
  469. else {
  470. this._color = value;
  471. }
  472. }
  473. /**
  474. * Gets the color distributiopn type
  475. */
  476. get colorsDistributionType() {
  477. return this._colorsDistributionType;
  478. }
  479. /**
  480. * Sets the color distribution type
  481. * @see GreasedLineMeshColorDistributionType
  482. * @param value color distribution type
  483. */
  484. set colorsDistributionType(value) {
  485. this._colorsDistributionType = value;
  486. this.markAllDefinesAsDirty();
  487. }
  488. /**
  489. * Gets the resolution
  490. */
  491. get resolution() {
  492. return this._resolution;
  493. }
  494. /**
  495. * Sets the resolution
  496. * @param value resolution of the screen for GreasedLine
  497. */
  498. set resolution(value) {
  499. this._aspect = value.x / value.y;
  500. this._resolution = value;
  501. }
  502. /**
  503. * Serializes this plugin material
  504. * @returns serializationObjec
  505. */
  506. serialize() {
  507. const serializationObject = super.serialize();
  508. const greasedLineMaterialOptions = {
  509. colorDistributionType: this._colorsDistributionType,
  510. colorsSampling: this.colorsSampling,
  511. colorMode: this.colorMode,
  512. dashCount: this._dashCount,
  513. dashOffset: this.dashOffset,
  514. dashRatio: this.dashRatio,
  515. resolution: this._resolution,
  516. sizeAttenuation: this._sizeAttenuation,
  517. useColors: this.useColors,
  518. useDash: this.useDash,
  519. visibility: this.visibility,
  520. width: this.width,
  521. };
  522. this._colors && (greasedLineMaterialOptions.colors = this._colors);
  523. this._color && (greasedLineMaterialOptions.color = this._color);
  524. serializationObject.greasedLineMaterialOptions = greasedLineMaterialOptions;
  525. return serializationObject;
  526. }
  527. /**
  528. * Parses a serialized objects
  529. * @param source serialized object
  530. * @param scene scene
  531. * @param rootUrl root url for textures
  532. */
  533. parse(source, scene, rootUrl) {
  534. super.parse(source, scene, rootUrl);
  535. const greasedLineMaterialOptions = source.greasedLineMaterialOptions;
  536. this.colorsTexture?.dispose();
  537. greasedLineMaterialOptions.color && this.setColor(greasedLineMaterialOptions.color, true);
  538. greasedLineMaterialOptions.colorDistributionType && (this.colorsDistributionType = greasedLineMaterialOptions.colorDistributionType);
  539. greasedLineMaterialOptions.colors && (this.colors = greasedLineMaterialOptions.colors);
  540. greasedLineMaterialOptions.colorsSampling && (this.colorsSampling = greasedLineMaterialOptions.colorsSampling);
  541. greasedLineMaterialOptions.colorMode && (this.colorMode = greasedLineMaterialOptions.colorMode);
  542. greasedLineMaterialOptions.useColors && (this.useColors = greasedLineMaterialOptions.useColors);
  543. greasedLineMaterialOptions.visibility && (this.visibility = greasedLineMaterialOptions.visibility);
  544. greasedLineMaterialOptions.useDash && (this.useDash = greasedLineMaterialOptions.useDash);
  545. greasedLineMaterialOptions.dashCount && (this.dashCount = greasedLineMaterialOptions.dashCount);
  546. greasedLineMaterialOptions.dashRatio && (this.dashRatio = greasedLineMaterialOptions.dashRatio);
  547. greasedLineMaterialOptions.dashOffset && (this.dashOffset = greasedLineMaterialOptions.dashOffset);
  548. greasedLineMaterialOptions.width && (this.width = greasedLineMaterialOptions.width);
  549. greasedLineMaterialOptions.sizeAttenuation && (this.sizeAttenuation = greasedLineMaterialOptions.sizeAttenuation);
  550. greasedLineMaterialOptions.resolution && (this.resolution = greasedLineMaterialOptions.resolution);
  551. if (this.colors) {
  552. this.colorsTexture = GreasedLineTools.CreateColorsTexture(`${this._material.name}-colors-texture`, this.colors, this.colorsSampling, scene);
  553. }
  554. else {
  555. GreasedLineTools.PrepareEmptyColorsTexture(scene);
  556. }
  557. this.markAllDefinesAsDirty();
  558. }
  559. /**
  560. * Makes a duplicate of the current configuration into another one.
  561. * @param plugin define the config where to copy the info
  562. */
  563. copyTo(plugin) {
  564. const dest = plugin;
  565. dest.colorsTexture?.dispose();
  566. if (this._colors) {
  567. dest.colorsTexture = GreasedLineTools.CreateColorsTexture(`${dest._material.name}-colors-texture`, this._colors, dest.colorsSampling, this._scene);
  568. }
  569. dest.setColor(this.color, true);
  570. dest.colorsDistributionType = this.colorsDistributionType;
  571. dest.colorsSampling = this.colorsSampling;
  572. dest.colorMode = this.colorMode;
  573. dest.useColors = this.useColors;
  574. dest.visibility = this.visibility;
  575. dest.useDash = this.useDash;
  576. dest.dashCount = this.dashCount;
  577. dest.dashRatio = this.dashRatio;
  578. dest.dashOffset = this.dashOffset;
  579. dest.width = this.width;
  580. dest.sizeAttenuation = this.sizeAttenuation;
  581. dest.resolution = this.resolution;
  582. dest.markAllDefinesAsDirty();
  583. }
  584. }
  585. /**
  586. * Plugin name
  587. */
  588. GreasedLinePluginMaterial.GREASED_LINE_MATERIAL_NAME = "GreasedLinePluginMaterial";
  589. RegisterClass(`BABYLON.${GreasedLinePluginMaterial.GREASED_LINE_MATERIAL_NAME}`, GreasedLinePluginMaterial);
  590. //# sourceMappingURL=greasedLinePluginMaterial.js.map