Home Reference Source

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

  1. import { Logger } from "cables-shared-client";
  2. import { Port } from "../core_port.js";
  3.  
  4. class CgUniform
  5. {
  6. constructor(__shader, __type, __name, _value, _port2, _port3, _port4, _structUniformName, _structName, _propertyName)
  7. {
  8. this._log = new Logger("cg_uniform");
  9. this._type = __type;
  10. this._name = __name;
  11. this._shader = __shader;
  12. this._value = 0.00001;
  13. this._oldValue = null;
  14. this._port = null;
  15. this._structName = _structName;
  16. this._structUniformName = _structUniformName;
  17. this._propertyName = _propertyName;
  18.  
  19. this._shader._addUniform(this);
  20. this.needsUpdate = true;
  21. this.shaderType = null;
  22. this.comment = null;
  23.  
  24. if (__type == "f")
  25. {
  26. this.set = this.setValue = this.setValueF.bind(this);
  27. this.updateValue = this.updateValueF.bind(this);
  28. }
  29. else if (__type == "f[]")
  30. {
  31. this.set = this.setValue = this.setValueArrayF.bind(this);
  32. this.updateValue = this.updateValueArrayF.bind(this);
  33. }
  34. else if (__type == "2f[]")
  35. {
  36. this.set = this.setValue = this.setValueArray2F.bind(this);
  37. this.updateValue = this.updateValueArray2F.bind(this);
  38. }
  39. else if (__type == "3f[]")
  40. {
  41. this.set = this.setValue = this.setValueArray3F.bind(this);
  42. this.updateValue = this.updateValueArray3F.bind(this);
  43. }
  44. else if (__type == "4f[]")
  45. {
  46. this.set = this.setValue = this.setValueArray4F.bind(this);
  47. this.updateValue = this.updateValueArray4F.bind(this);
  48. }
  49. else if (__type == "i")
  50. {
  51. this.set = this.setValue = this.setValueI.bind(this);
  52. this.updateValue = this.updateValueI.bind(this);
  53. }
  54. else if (__type == "2i")
  55. {
  56. this.set = this.setValue = this.setValue2I.bind(this);
  57. this.updateValue = this.updateValue2I.bind(this);
  58. }
  59. else if (__type == "3i")
  60. {
  61. this.set = this.setValue = this.setValue3I.bind(this);
  62. this.updateValue = this.updateValue3I.bind(this);
  63. }
  64. else if (__type == "4i")
  65. {
  66. this.set = this.setValue = this.setValue4I.bind(this);
  67. this.updateValue = this.updateValue4I.bind(this);
  68. }
  69. else if (__type == "b")
  70. {
  71. this.set = this.setValue = this.setValueBool.bind(this);
  72. this.updateValue = this.updateValueBool.bind(this);
  73. }
  74. else if (__type == "4f")
  75. {
  76. this.set = this.setValue = this.setValue4F.bind(this);
  77. this.updateValue = this.updateValue4F.bind(this);
  78. }
  79. else if (__type == "3f")
  80. {
  81. this.set = this.setValue = this.setValue3F.bind(this);
  82. this.updateValue = this.updateValue3F.bind(this);
  83. }
  84. else if (__type == "2f")
  85. {
  86. this.set = this.setValue = this.setValue2F.bind(this);
  87. this.updateValue = this.updateValue2F.bind(this);
  88. }
  89. else if (__type == "t")
  90. {
  91. this.set = this.setValue = this.setValueT.bind(this);
  92. this.updateValue = this.updateValueT.bind(this);
  93. }
  94. else if (__type == "sampler")
  95. {
  96. if (this.setValueAny)
  97. {
  98. this.set = this.setValue = this.setValueAny.bind(this);
  99. this.updateValue = this.updateValueAny.bind(this);
  100. }
  101. }
  102. else if (__type == "tc")
  103. {
  104. this.set = this.setValue = this.setValueT.bind(this);
  105. this.updateValue = this.updateValueT.bind(this);
  106. }
  107. else if (__type == "t[]")
  108. {
  109. this.set = this.setValue = this.setValueArrayT.bind(this);
  110. this.updateValue = this.updateValueArrayT.bind(this);
  111. }
  112. else if (__type == "m4" || __type == "m4[]")
  113. {
  114. this.set = this.setValue = this.setValueM4.bind(this);
  115. this.updateValue = this.updateValueM4.bind(this);
  116. }
  117. else
  118. {
  119. // console.error("unknown");
  120. this._log.error("Unknown uniform type " + __type);
  121. }
  122.  
  123. if (typeof _value == "object" && _value instanceof Port)
  124. {
  125. this._port = _value;
  126. this._value = this._port.get();
  127.  
  128.  
  129. if (_port2 && _port3 && _port4)
  130. {
  131. if (!(_port2 instanceof Port) || !(_port3 instanceof Port) || !(_port4 instanceof Port))
  132. {
  133. this._log.error("[cgl_uniform] mixed port/value parameter for vec4 ", this._name);
  134. }
  135.  
  136. this._value = [0, 0, 0, 0];
  137. this._port2 = _port2;
  138. this._port3 = _port3;
  139. this._port4 = _port4;
  140.  
  141. this._port.on("change", this.updateFromPort4f.bind(this));
  142. this._port2.on("change", this.updateFromPort4f.bind(this));
  143. this._port3.on("change", this.updateFromPort4f.bind(this));
  144. this._port4.on("change", this.updateFromPort4f.bind(this));
  145.  
  146. // this._port.onChange = this._port2.onChange = this._port3.onChange = this._port4.onChange = this.updateFromPort4f.bind(this);
  147. this.updateFromPort4f();
  148. }
  149. else if (_port2 && _port3)
  150. {
  151. if (!(_port2 instanceof Port) || !(_port3 instanceof Port))
  152. {
  153. this._log.error("[cgl_uniform] mixed port/value parameter for vec4 ", this._name);
  154. }
  155.  
  156. this._value = [0, 0, 0];
  157. this._port2 = _port2;
  158. this._port3 = _port3;
  159. // this._port.onChange = this._port2.onChange = this._port3.onChange = this.updateFromPort3f.bind(this);
  160. this._port.on("change", this.updateFromPort3f.bind(this));
  161. this._port2.on("change", this.updateFromPort3f.bind(this));
  162. this._port3.on("change", this.updateFromPort3f.bind(this));
  163.  
  164. this.updateFromPort3f();
  165. }
  166. else if (_port2)
  167. {
  168. if (!(_port2 instanceof Port))
  169. {
  170. this._log.error("[cgl_uniform] mixed port/value parameter for vec4 ", this._name);
  171. }
  172.  
  173. this._value = [0, 0];
  174. this._port2 = _port2;
  175. // this._port.onChange = this._port2.onChange = this.updateFromPort2f.bind(this);
  176. this._port.on("change", this.updateFromPort2f.bind(this));
  177. this._port2.on("change", this.updateFromPort2f.bind(this));
  178.  
  179. this.updateFromPort2f();
  180. }
  181. else
  182. {
  183. // this._port.on = this.updateFromPort.bind(this);
  184. this._port.on("change", this.updateFromPort.bind(this));
  185. }
  186. }
  187. else this._value = _value;
  188.  
  189.  
  190. if (this._value == undefined)
  191. {
  192. console.log("value undefined", this);
  193. this._value = 0;
  194. }
  195.  
  196. this.setValue(this._value);
  197.  
  198. this.needsUpdate = true;
  199. }
  200.  
  201. getType()
  202. {
  203. return this._type;
  204. }
  205.  
  206. get type()
  207. {
  208. return this._type;
  209. }
  210.  
  211. get name()
  212. {
  213. return this._name;
  214. }
  215.  
  216. getName()
  217. {
  218. return this._name;
  219. }
  220.  
  221. getValue()
  222. {
  223. return this._value;
  224. }
  225.  
  226. getShaderType()
  227. {
  228. return this.shaderType;
  229. }
  230.  
  231. isStructMember()
  232. {
  233. return !!this._structName;
  234. }
  235.  
  236.  
  237. updateFromPort4f()
  238. {
  239. this._value[0] = this._port.get();
  240. this._value[1] = this._port2.get();
  241. this._value[2] = this._port3.get();
  242. this._value[3] = this._port4.get();
  243. this.setValue(this._value);
  244. }
  245.  
  246. updateFromPort3f()
  247. {
  248. this._value[0] = this._port.get();
  249. this._value[1] = this._port2.get();
  250. this._value[2] = this._port3.get();
  251. this.setValue(this._value);
  252. }
  253.  
  254. updateFromPort2f()
  255. {
  256. this._value[0] = this._port.get();
  257. this._value[1] = this._port2.get();
  258. this.setValue(this._value);
  259. }
  260.  
  261. updateFromPort()
  262. {
  263. this.setValue(this._port.get());
  264. }
  265. }
  266.  
  267. export default CgUniform;