Source: lib/chart/gridvisuals.js

  1. import * as ECS from '../../packages/ecs-js/index'
  2. import XSys from '../sys/xsys'
  3. import {x} from '../xapp/xworld'
  4. import {XError} from '../xutils/xcommon'
  5. import {vec3} from '../xmath/vec';
  6. import {CoordsGrid} from '../xmath/chartgrid'
  7. import {GridElem} from '../component/ext/chart';
  8. import {Obj3Type} from '../component/obj3';
  9. import {AssetType, ShaderFlag} from '../component/visual';
  10. /**
  11. * Subsystem manage 3d chart's auxillaries, including xyz plane, xyz value line,
  12. * xyz value labels.<br>
  13. * @class GridVisuals
  14. */
  15. export default class GridVisuals extends XSys {
  16. /**
  17. * create chart world
  18. * @param {ECS} ecs
  19. * @param {object} options
  20. * options.chart: json chart section
  21. * @param {array} json visuas configuration.
  22. * @constructor GridVisuals */
  23. constructor(ecs, options, json) {
  24. super(ecs);
  25. this.logged = false;
  26. this.ecs = ecs;
  27. this.cmd = [];
  28. ecs.registerComponent('GridElem', GridElem);
  29. if (!options.chart)
  30. throw new XError('GridVisuals can only been created synchronously with json data (options.chart) for initializing');
  31. /** @property {CoordsGrid} grid - grid space manager
  32. * @member GridVisuals#grid
  33. */
  34. if (!x.chart || !x.chart.grid) {
  35. this.grid = new CoordsGrid(options.chart, json);
  36. x.chart = Object.assign(x.chart || new Object(), {grid: this.grid});
  37. }
  38. else this.grid = x.chart.grid;
  39. if (ecs) {
  40. /** @property {object} elems - {xyzLine, xyzPlane, axisPlane}, where<br>
  41. * xyzLine is a THREE.Object3D, with children of 3 vertical lines;<br>
  42. * xyzPlane is a THREE.Object3D, with children of 3 vertical plane,
  43. * can be used for highlight value grid;<br>
  44. * axisPlane, {yz, zx, xy} is an array of 3 THREE.Object3D, with children of 3 vertical plane at axes plane;<br>
  45. * @member GridVisuals#elems
  46. */
  47. this.elems = this.visuals(ecs, options, json);
  48. }
  49. this.movePlane = true; // xyzPlane movable
  50. }
  51. /**
  52. * @param {int} tick
  53. * @param {array<Entity>} entites
  54. * @member GridVisuals#update
  55. * @function
  56. */
  57. update(tick, entities) {
  58. if (x.xview.flag > 0 && x.xview.picked && x.xview.picked.GridValue) {
  59. var gridval = x.xview.picked.GridValue;
  60. var y = this.grid.barHeight(gridval.val);
  61. this.strechLines(gridval.gridx, [0, y, 0]);
  62. if (this.movePlane) {
  63. }
  64. }
  65. }
  66. /** Generate chart visual elements.
  67. * @param {ECS} ecs
  68. * @param {object} options
  69. * @param {object} json
  70. * @member GridVisuals#visuals
  71. * @function
  72. */
  73. visuals(ecs, options, json) {
  74. // var s = grid.space(vec3.add(options.chart.grid, 1));
  75. var s = this.grid.space([1, 1, 1]);
  76. // x, y, z line with value label
  77. var idxline = options.lines;
  78. var xyzLine = ecs.createEntity({
  79. id: 'xyz-line',
  80. Obj3: { geom: Obj3Type.PointSects,
  81. box: [] },
  82. Visual:{vtype: AssetType.DynaSects,
  83. paras: {
  84. // position updated with strechLines(), see update()
  85. sects:[[[0, 0, 0], [0, 0, 0]],
  86. [[0, 0, 0], [0, 0, 0]],
  87. [[0, 0, 0], [0, 0, 0]]],
  88. origin: [0, 0, 0],
  89. scale: s,
  90. color: idxline && idxline.color || 0xcc00ff } },
  91. GridElem: {}
  92. });
  93. var bounds = this.grid.spaceBound();
  94. var xPlane = ecs.createEntity({
  95. id: 'x-plane',
  96. Obj3: { geom: Obj3Type.PLANE,
  97. transform: [{rotate: {deg: 90, axis: [0, 1, 0]}},
  98. {translate: [0, bounds[1]/2, bounds[2]/2]}],
  99. uniforms: {opacity: 0.5},
  100. box: [bounds[2], bounds[1]] },
  101. Visual:{vtype: AssetType.mesh_basic,
  102. asset: options.planes.tex,
  103. paras: {
  104. blending: THREE.AdditiveBlending,
  105. color: 0x770000 } },
  106. GridElem: {}
  107. });
  108. var yPlane = ecs.createEntity({
  109. id: 'y-plane',
  110. Obj3: { geom: Obj3Type.PLANE,
  111. transform: [{rotate: {deg: -90, axis: [1, 0, 0]}},
  112. {translate: [bounds[0]/2, 0, bounds[2]/2]}],
  113. uniforms: {opacity: 0.5},
  114. box: [bounds[0], bounds[2]] },
  115. Visual:{vtype: AssetType.mesh_basic,
  116. asset: options.planes.tex,
  117. paras: {
  118. blending: THREE.AdditiveBlending,
  119. color: 0x007700 } },
  120. GridElem: {}
  121. });
  122. var zPlane = ecs.createEntity({
  123. id: 'z-plane',
  124. Obj3: { geom: Obj3Type.PLANE,
  125. transform: [{translate: [bounds[0]/2, bounds[1]/2, 0]}],
  126. uniforms: {opacity: 0.5},
  127. box: bounds },
  128. Visual:{vtype: AssetType.mesh_basic,
  129. asset: options.planes.tex,
  130. paras: {
  131. blending: THREE.AdditiveBlending,
  132. color: 0x000077 } },
  133. GridElem: {}
  134. });
  135. return {xyzLine,
  136. xyzPlane: {yz: xPlane, xz: yPlane, xy: zPlane} };
  137. }
  138. /**Set value indicating lines to grid, with offset in value range (befor scale
  139. * to world).
  140. *
  141. * This method modifying the lines' vertices position buffer directly.
  142. * @param {array<int>} gridIx
  143. * @param {array<number>} [offset = [0, 0, 0]]
  144. * @return {GridVisuals} this
  145. * @member GridVisuals#strechLines
  146. * @function
  147. */
  148. strechLines (gridIx, offset = [0, 0, 0]) {
  149. var p = this.grid.worldPos(this.xyzBuf, gridIx);
  150. return this.strechLinesWorld(p, offset);
  151. }
  152. /**Set value indicating lines to position in world, with offset in world.
  153. *
  154. * This method modifying the lines' vertices position buffer directly.
  155. * @param {array<int>} gridIx
  156. * @param {array<number>} offset
  157. * @return {GridVisuals} this
  158. * @member GridVisuals#strechLines
  159. * @function
  160. */
  161. strechLinesWorld (p, offset) {
  162. var s = this.elems.xyzLine.Obj3.mesh.geometry.attributes.position.array;
  163. var x = 0;
  164. s[x + 0] = p[0] + offset[0];
  165. s[x + 1] = p[1] + offset[1];
  166. s[x + 2] = p[2] + offset[2];
  167. s[x + 4] = p[1] + offset[1];
  168. s[x + 5] = p[2] + offset[2];
  169. //
  170. var y = 6;
  171. s[y + 0] = p[0] + offset[0];
  172. s[y + 1] = p[1] + offset[1];
  173. s[y + 2] = p[2] + offset[2];
  174. s[y + 3] = p[0] + offset[0];
  175. s[y + 5] = p[2] + offset[2];
  176. //
  177. var z = 12;
  178. s[z + 0] = p[0] + offset[0];
  179. s[z + 1] = p[1] + offset[1];
  180. s[z + 2] = p[2] + offset[2];
  181. s[z + 3] = p[0] + offset[0];
  182. s[z + 4] = p[1] + offset[1];
  183. this.elems.xyzLine.Obj3.mesh.geometry.attributes.position.needsUpdate = true;
  184. this.xyzBuf = p;
  185. return this;
  186. }
  187. setPlanePos (gridIx) {
  188. }
  189. }
  190. GridVisuals.query = {any: ['GridValue']};