createUniform.js 10.0 KB

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