Home Reference Source

cables_dev/cables/src/core/cg/cg_state.js

  1. import { Events } from "cables-shared-client";
  2. import { CgCanvas } from "./cg_canvas.js";
  3. import { MatrixStack } from "./cg_matrixstack.js";
  4.  
  5. class CGState extends Events
  6. {
  7. constructor(_patch)
  8. {
  9. super();
  10. this.tempData = this.frameStore = this.frameStore || {};
  11. // this.canvas = null;
  12.  
  13. this.fpsCounter = new CABLES.CG.FpsCounter();
  14. this._identView = vec3.create();
  15. this._ident = vec3.create();
  16. vec3.set(this._identView, 0, 0, -2);
  17. vec3.set(this._ident, 0, 0, 0);
  18.  
  19. this.patch = _patch;
  20. this.autoReSize = true;
  21.  
  22. this.DEPTH_COMPARE_FUNC_NEVER = 0;
  23. this.DEPTH_COMPARE_FUNC_LESS = 1;
  24. this.DEPTH_COMPARE_FUNC_EQUAL = 2;
  25. this.DEPTH_COMPARE_FUNC_LESSEQUAL = 3;
  26. this.DEPTH_COMPARE_FUNC_GREATER = 4;
  27. this.DEPTH_COMPARE_FUNC_NOTEQUAL = 5;
  28. this.DEPTH_COMPARE_FUNC_GREATEREQUAL = 6;
  29. this.DEPTH_COMPARE_FUNC_ALWAYS = 7;
  30.  
  31.  
  32. /**
  33. * Current projection matrix
  34. * @memberof Context
  35. * @instance
  36. * @type {mat4}
  37. */
  38. this.pMatrix = mat4.create();
  39.  
  40. /**
  41. * Current model matrix
  42. * @memberof Context
  43. * @instance
  44. * @type {mat4}
  45. */
  46. this.mMatrix = mat4.create();
  47.  
  48. /**
  49. * Current view matrix
  50. * @memberof Context
  51. * @instance
  52. * @type {mat4}
  53. */
  54. this.vMatrix = mat4.create();
  55. this._textureslots = [];
  56.  
  57. this._pMatrixStack = new MatrixStack();
  58. this._mMatrixStack = new MatrixStack();
  59. this._vMatrixStack = new MatrixStack();
  60.  
  61. this.canvasScale = 1;
  62.  
  63. mat4.identity(this.mMatrix);
  64. mat4.identity(this.vMatrix);
  65.  
  66.  
  67. window.matchMedia("screen and (min-resolution: 2dppx)")
  68. .addEventListener("change", (e) =>
  69. {
  70. this.emitEvent("resize");
  71. });
  72. }
  73.  
  74. get canvasWidth()
  75. {
  76. return this.cgCanvas.canvasWidth;
  77. }
  78.  
  79. get canvasHeight()
  80. {
  81. return this.cgCanvas.canvasHeight;
  82. }
  83.  
  84. set pixelDensity(p)
  85. {
  86. if (this.cgCanvas.pixelDensity != p)
  87. {
  88. this.cgCanvas.pixelDensity = p;
  89. this.cgCanvas.updateSize();
  90. this.emitEvent("resize");
  91. }
  92. }
  93.  
  94. get pixelDensity()
  95. {
  96. return this.cgCanvas.pixelDensity;
  97. }
  98.  
  99.  
  100. getGApiName()
  101. {
  102. return ["WebGL", "WebGPU"][this.gApi];
  103. }
  104.  
  105. get canvas()
  106. {
  107. return this.cgCanvas.canvasEle;
  108. }
  109.  
  110.  
  111.  
  112. setCanvas(canvEle)
  113. {
  114. if (this.cgCanvas && canvEle == this.cgCanvas.canvasEle) return;
  115. if (typeof canvEle === "string") canvEle = document.getElementById(canvEle);
  116.  
  117. this.cgCanvas = new CgCanvas({ "canvasEle": canvEle, "cg": this });
  118.  
  119. canvEle.parentElement.classList.add("cablesContainer");
  120. if (this._setCanvas) this._setCanvas(canvEle);
  121.  
  122. this.updateSize();
  123. }
  124.  
  125. updateSize()
  126. {
  127. this.cgCanvas.updateSize();
  128. }
  129.  
  130. setSize(w, h, ignorestyle)
  131. {
  132. this.cgCanvas.setSize(w, h, ignorestyle);
  133. }
  134.  
  135. _resizeToWindowSize()
  136. {
  137. if (this.autoReSize)
  138. {
  139. this.setSize(window.innerWidth, window.innerHeight);
  140. this.updateSize();
  141. }
  142. }
  143.  
  144. _resizeToParentSize()
  145. {
  146. if (this.autoReSize)
  147. {
  148. const p = this.canvas.parentElement;
  149. if (!p)
  150. {
  151. this._log.error("cables: can not resize to container element");
  152. return;
  153. }
  154.  
  155. this.setSize(p.clientWidth, p.clientHeight);
  156. this.updateSize();
  157. }
  158. }
  159.  
  160. setAutoResize(parent)
  161. {
  162. window.removeEventListener("resize", this._resizeToWindowSize.bind(this));
  163. window.removeEventListener("resize", this._resizeToParentSize.bind(this));
  164.  
  165. if (parent == "window")
  166. {
  167. window.addEventListener("resize", this._resizeToWindowSize.bind(this));
  168. window.addEventListener("orientationchange", this._resizeToWindowSize.bind(this));
  169. this._resizeToWindowSize();
  170. }
  171. if (parent == "parent")
  172. {
  173. window.addEventListener("resize", this._resizeToParentSize.bind(this));
  174. this._resizeToParentSize();
  175. }
  176. }
  177.  
  178. /**
  179. * push a matrix to the projection matrix stack
  180. * @function pushPMatrix
  181. * @memberof Context
  182. * @instance
  183. */
  184. pushPMatrix()
  185. {
  186. this.pMatrix = this._pMatrixStack.push(this.pMatrix);
  187. }
  188.  
  189. /**
  190. * pop projection matrix stack
  191. * @function popPMatrix
  192. * @memberof Context
  193. * @instance
  194. * @returns {mat4} current projectionmatrix
  195. */
  196. popPMatrix()
  197. {
  198. this.pMatrix = this._pMatrixStack.pop();
  199. return this.pMatrix;
  200. }
  201.  
  202. getProjectionMatrixStateCount()
  203. {
  204. return this._pMatrixStack.stateCounter;
  205. }
  206.  
  207. /**
  208. * push a matrix to the model matrix stack
  209. * @function pushModelMatrix
  210. * @memberof Context
  211. * @instance
  212. * @example
  213. * // see source code of translate op:
  214. * cgl.pushModelMatrix();
  215. * mat4.translate(cgl.mMatrix,cgl.mMatrix, vec);
  216. * trigger.trigger();
  217. * cgl.popModelMatrix();
  218. */
  219. pushModelMatrix()
  220. {
  221. this.mMatrix = this._mMatrixStack.push(this.mMatrix);
  222. }
  223.  
  224. /**
  225. * pop model matrix stack
  226. * @function popModelMatrix
  227. * @memberof Context
  228. * @instance
  229. * @returns {mat4} current modelmatrix
  230. */
  231. popModelMatrix()
  232. {
  233. // todo: DEPRECATE
  234. // if (this._mMatrixStack.length === 0) throw "Invalid modelview popMatrix!";
  235. this.mMatrix = this._mMatrixStack.pop();
  236. return this.mMatrix;
  237. }
  238.  
  239. /**
  240. * get model matrix
  241. * @function modelMatrix
  242. * @memberof Context
  243. * @instance
  244. * @returns {mat4} current modelmatrix
  245. */
  246. modelMatrix()
  247. {
  248. return this.mMatrix;
  249. }
  250.  
  251.  
  252. /**
  253. * push a matrix to the view matrix stack
  254. * @function pushviewMatrix
  255. * @memberof Context
  256. * @instance
  257. */
  258. pushViewMatrix()
  259. {
  260. this.vMatrix = this._vMatrixStack.push(this.vMatrix);
  261. }
  262.  
  263. /**
  264. * pop view matrix stack
  265. * @function popViewMatrix
  266. * @memberof Context
  267. * @instance
  268. * @returns {mat4} current viewmatrix
  269. * @function
  270. */
  271. popViewMatrix()
  272. {
  273. this.vMatrix = this._vMatrixStack.pop();
  274. }
  275.  
  276. getViewMatrixStateCount()
  277. {
  278. return this._vMatrixStack.stateCounter;
  279. }
  280.  
  281. _startMatrixStacks(identTranslate, identTranslateView)
  282. {
  283. identTranslate = identTranslate || this._ident;
  284. identTranslateView = identTranslateView || this._identView;
  285.  
  286. mat4.perspective(this.pMatrix, 45, this.canvasWidth / this.canvasHeight, 0.1, 1000.0);
  287.  
  288. mat4.identity(this.mMatrix);
  289. mat4.identity(this.vMatrix);
  290. mat4.translate(this.mMatrix, this.mMatrix, identTranslate);
  291. mat4.translate(this.vMatrix, this.vMatrix, identTranslateView);
  292.  
  293. this.pushPMatrix();
  294. this.pushModelMatrix();
  295. this.pushViewMatrix();
  296. }
  297.  
  298. _endMatrixStacks()
  299. {
  300. this.popViewMatrix();
  301. this.popModelMatrix();
  302. this.popPMatrix();
  303. }
  304.  
  305. dispose()
  306. {
  307. this.aborted = true;
  308. if (this.cgCanvas) this.cgCanvas.dispose();
  309. if (this._dispose) this._dispose();
  310. }
  311.  
  312. shouldDrawHelpers()
  313. {
  314. return false;
  315. }
  316. }
  317.  
  318. export { CGState };
  319.  
  320.