createUniform.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Cartesian4 from "../Core/Cartesian4.js";
  4. import Color from "../Core/Color.js";
  5. import defined from "../Core/defined.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import Matrix2 from "../Core/Matrix2.js";
  8. import Matrix3 from "../Core/Matrix3.js";
  9. import Matrix4 from "../Core/Matrix4.js";
  10. import RuntimeError from "../Core/RuntimeError.js";
  11. /**
  12. * @private
  13. * @constructor
  14. */
  15. function createUniform(gl, activeUniform, uniformName, location) {
  16. switch (activeUniform.type) {
  17. case gl.FLOAT:
  18. return new UniformFloat(gl, activeUniform, uniformName, location);
  19. case gl.FLOAT_VEC2:
  20. return new UniformFloatVec2(gl, activeUniform, uniformName, location);
  21. case gl.FLOAT_VEC3:
  22. return new UniformFloatVec3(gl, activeUniform, uniformName, location);
  23. case gl.FLOAT_VEC4:
  24. return new UniformFloatVec4(gl, activeUniform, uniformName, location);
  25. case gl.SAMPLER_2D:
  26. case gl.SAMPLER_CUBE:
  27. return new UniformSampler(gl, activeUniform, uniformName, location);
  28. case gl.INT:
  29. case gl.BOOL:
  30. return new UniformInt(gl, activeUniform, uniformName, location);
  31. case gl.INT_VEC2:
  32. case gl.BOOL_VEC2:
  33. return new UniformIntVec2(gl, activeUniform, uniformName, location);
  34. case gl.INT_VEC3:
  35. case gl.BOOL_VEC3:
  36. return new UniformIntVec3(gl, activeUniform, uniformName, location);
  37. case gl.INT_VEC4:
  38. case gl.BOOL_VEC4:
  39. return new UniformIntVec4(gl, activeUniform, uniformName, location);
  40. case gl.FLOAT_MAT2:
  41. return new UniformMat2(gl, activeUniform, uniformName, location);
  42. case gl.FLOAT_MAT3:
  43. return new UniformMat3(gl, activeUniform, uniformName, location);
  44. case gl.FLOAT_MAT4:
  45. return new UniformMat4(gl, activeUniform, uniformName, location);
  46. default:
  47. throw new RuntimeError(
  48. `Unrecognized uniform type: ${activeUniform.type} for uniform "${uniformName}".`
  49. );
  50. }
  51. }
  52. /**
  53. * @private
  54. * @constructor
  55. */
  56. function UniformFloat(gl, activeUniform, uniformName, location) {
  57. /**
  58. * @type {String}
  59. * @readonly
  60. */
  61. this.name = uniformName;
  62. this.value = undefined;
  63. this._value = 0.0;
  64. this._gl = gl;
  65. this._location = location;
  66. }
  67. UniformFloat.prototype.set = function () {
  68. if (this.value !== this._value) {
  69. this._value = this.value;
  70. this._gl.uniform1f(this._location, this.value);
  71. }
  72. };
  73. ///////////////////////////////////////////////////////////////////////////
  74. /**
  75. * @private
  76. * @constructor
  77. */
  78. function UniformFloatVec2(gl, activeUniform, uniformName, location) {
  79. /**
  80. * @type {String}
  81. * @readonly
  82. */
  83. this.name = uniformName;
  84. this.value = undefined;
  85. this._value = new Cartesian2();
  86. this._gl = gl;
  87. this._location = location;
  88. }
  89. UniformFloatVec2.prototype.set = function () {
  90. const v = this.value;
  91. if (!Cartesian2.equals(v, this._value)) {
  92. Cartesian2.clone(v, this._value);
  93. this._gl.uniform2f(this._location, v.x, v.y);
  94. }
  95. };
  96. ///////////////////////////////////////////////////////////////////////////
  97. /**
  98. * @private
  99. * @constructor
  100. */
  101. function UniformFloatVec3(gl, activeUniform, uniformName, location) {
  102. /**
  103. * @type {String}
  104. * @readonly
  105. */
  106. this.name = uniformName;
  107. this.value = undefined;
  108. this._value = undefined;
  109. this._gl = gl;
  110. this._location = location;
  111. }
  112. UniformFloatVec3.prototype.set = function () {
  113. const v = this.value;
  114. if (defined(v.red)) {
  115. if (!Color.equals(v, this._value)) {
  116. this._value = Color.clone(v, this._value);
  117. this._gl.uniform3f(this._location, v.red, v.green, v.blue);
  118. }
  119. } else if (defined(v.x)) {
  120. if (!Cartesian3.equals(v, this._value)) {
  121. this._value = Cartesian3.clone(v, this._value);
  122. this._gl.uniform3f(this._location, v.x, v.y, v.z);
  123. }
  124. } else {
  125. //>>includeStart('debug', pragmas.debug);
  126. throw new DeveloperError(`Invalid vec3 value for uniform "${this.name}".`);
  127. //>>includeEnd('debug');
  128. }
  129. };
  130. ///////////////////////////////////////////////////////////////////////////
  131. /**
  132. * @private
  133. * @constructor
  134. */
  135. function UniformFloatVec4(gl, activeUniform, uniformName, location) {
  136. /**
  137. * @type {String}
  138. * @readonly
  139. */
  140. this.name = uniformName;
  141. this.value = undefined;
  142. this._value = undefined;
  143. this._gl = gl;
  144. this._location = location;
  145. }
  146. UniformFloatVec4.prototype.set = function () {
  147. const v = this.value;
  148. if (defined(v.red)) {
  149. if (!Color.equals(v, this._value)) {
  150. this._value = Color.clone(v, this._value);
  151. this._gl.uniform4f(this._location, v.red, v.green, v.blue, v.alpha);
  152. }
  153. } else if (defined(v.x)) {
  154. if (!Cartesian4.equals(v, this._value)) {
  155. this._value = Cartesian4.clone(v, this._value);
  156. this._gl.uniform4f(this._location, v.x, v.y, v.z, v.w);
  157. }
  158. } else {
  159. //>>includeStart('debug', pragmas.debug);
  160. throw new DeveloperError(`Invalid vec4 value for uniform "${this.name}".`);
  161. //>>includeEnd('debug');
  162. }
  163. };
  164. ///////////////////////////////////////////////////////////////////////////
  165. /**
  166. * @private
  167. * @constructor
  168. */
  169. function UniformSampler(gl, activeUniform, uniformName, location) {
  170. /**
  171. * @type {String}
  172. * @readonly
  173. */
  174. this.name = uniformName;
  175. this.value = undefined;
  176. this._gl = gl;
  177. this._location = location;
  178. this.textureUnitIndex = undefined;
  179. }
  180. UniformSampler.prototype.set = function () {
  181. const gl = this._gl;
  182. gl.activeTexture(gl.TEXTURE0 + this.textureUnitIndex);
  183. const v = this.value;
  184. gl.bindTexture(v._target, v._texture);
  185. };
  186. UniformSampler.prototype._setSampler = function (textureUnitIndex) {
  187. this.textureUnitIndex = textureUnitIndex;
  188. this._gl.uniform1i(this._location, textureUnitIndex);
  189. return textureUnitIndex + 1;
  190. };
  191. ///////////////////////////////////////////////////////////////////////////
  192. /**
  193. * @private
  194. * @constructor
  195. */
  196. function UniformInt(gl, activeUniform, uniformName, location) {
  197. /**
  198. * @type {String}
  199. * @readonly
  200. */
  201. this.name = uniformName;
  202. this.value = undefined;
  203. this._value = 0.0;
  204. this._gl = gl;
  205. this._location = location;
  206. }
  207. UniformInt.prototype.set = function () {
  208. if (this.value !== this._value) {
  209. this._value = this.value;
  210. this._gl.uniform1i(this._location, this.value);
  211. }
  212. };
  213. ///////////////////////////////////////////////////////////////////////////
  214. /**
  215. * @private
  216. * @constructor
  217. */
  218. function UniformIntVec2(gl, activeUniform, uniformName, location) {
  219. /**
  220. * @type {String}
  221. * @readonly
  222. */
  223. this.name = uniformName;
  224. this.value = undefined;
  225. this._value = new Cartesian2();
  226. this._gl = gl;
  227. this._location = location;
  228. }
  229. UniformIntVec2.prototype.set = function () {
  230. const v = this.value;
  231. if (!Cartesian2.equals(v, this._value)) {
  232. Cartesian2.clone(v, this._value);
  233. this._gl.uniform2i(this._location, v.x, v.y);
  234. }
  235. };
  236. ///////////////////////////////////////////////////////////////////////////
  237. /**
  238. * @private
  239. * @constructor
  240. */
  241. function UniformIntVec3(gl, activeUniform, uniformName, location) {
  242. /**
  243. * @type {String}
  244. * @readonly
  245. */
  246. this.name = uniformName;
  247. this.value = undefined;
  248. this._value = new Cartesian3();
  249. this._gl = gl;
  250. this._location = location;
  251. }
  252. UniformIntVec3.prototype.set = function () {
  253. const v = this.value;
  254. if (!Cartesian3.equals(v, this._value)) {
  255. Cartesian3.clone(v, this._value);
  256. this._gl.uniform3i(this._location, v.x, v.y, v.z);
  257. }
  258. };
  259. ///////////////////////////////////////////////////////////////////////////
  260. /**
  261. * @private
  262. * @constructor
  263. */
  264. function UniformIntVec4(gl, activeUniform, uniformName, location) {
  265. /**
  266. * @type {String}
  267. * @readonly
  268. */
  269. this.name = uniformName;
  270. this.value = undefined;
  271. this._value = new Cartesian4();
  272. this._gl = gl;
  273. this._location = location;
  274. }
  275. UniformIntVec4.prototype.set = function () {
  276. const v = this.value;
  277. if (!Cartesian4.equals(v, this._value)) {
  278. Cartesian4.clone(v, this._value);
  279. this._gl.uniform4i(this._location, v.x, v.y, v.z, v.w);
  280. }
  281. };
  282. ///////////////////////////////////////////////////////////////////////////
  283. const scratchUniformArray = new Float32Array(4);
  284. /**
  285. * @private
  286. * @constructor
  287. */
  288. function UniformMat2(gl, activeUniform, uniformName, location) {
  289. /**
  290. * @type {String}
  291. * @readonly
  292. */
  293. this.name = uniformName;
  294. this.value = undefined;
  295. this._value = new Matrix2();
  296. this._gl = gl;
  297. this._location = location;
  298. }
  299. UniformMat2.prototype.set = function () {
  300. if (!Matrix2.equalsArray(this.value, this._value, 0)) {
  301. Matrix2.clone(this.value, this._value);
  302. const array = Matrix2.toArray(this.value, scratchUniformArray);
  303. this._gl.uniformMatrix2fv(this._location, false, array);
  304. }
  305. };
  306. ///////////////////////////////////////////////////////////////////////////
  307. const scratchMat3Array = new Float32Array(9);
  308. /**
  309. * @private
  310. * @constructor
  311. */
  312. function UniformMat3(gl, activeUniform, uniformName, location) {
  313. /**
  314. * @type {String}
  315. * @readonly
  316. */
  317. this.name = uniformName;
  318. this.value = undefined;
  319. this._value = new Matrix3();
  320. this._gl = gl;
  321. this._location = location;
  322. }
  323. UniformMat3.prototype.set = function () {
  324. if (!Matrix3.equalsArray(this.value, this._value, 0)) {
  325. Matrix3.clone(this.value, this._value);
  326. const array = Matrix3.toArray(this.value, scratchMat3Array);
  327. this._gl.uniformMatrix3fv(this._location, false, array);
  328. }
  329. };
  330. ///////////////////////////////////////////////////////////////////////////
  331. const scratchMat4Array = new Float32Array(16);
  332. /**
  333. * @private
  334. * @constructor
  335. */
  336. function UniformMat4(gl, activeUniform, uniformName, location) {
  337. /**
  338. * @type {String}
  339. * @readonly
  340. */
  341. this.name = uniformName;
  342. this.value = undefined;
  343. this._value = new Matrix4();
  344. this._gl = gl;
  345. this._location = location;
  346. }
  347. UniformMat4.prototype.set = function () {
  348. if (!Matrix4.equalsArray(this.value, this._value, 0)) {
  349. Matrix4.clone(this.value, this._value);
  350. const array = Matrix4.toArray(this.value, scratchMat4Array);
  351. this._gl.uniformMatrix4fv(this._location, false, array);
  352. }
  353. };
  354. export default createUniform;