babylon.colorGradingTexture.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. /**
  9. * This represents a color grading texture. This acts as a lookup table LUT, useful during post process
  10. * It can help converting any input color in a desired output one. This can then be used to create effects
  11. * from sepia, black and white to sixties or futuristic rendering...
  12. *
  13. * The only supported format is currently 3dl.
  14. * More information on LUT: https://en.wikipedia.org/wiki/3D_lookup_table/
  15. */
  16. var ColorGradingTexture = (function (_super) {
  17. __extends(ColorGradingTexture, _super);
  18. /**
  19. * Instantiates a ColorGradingTexture from the following parameters.
  20. *
  21. * @param url The location of the color gradind data (currently only supporting 3dl)
  22. * @param scene The scene the texture will be used in
  23. */
  24. function ColorGradingTexture(url, scene) {
  25. _super.call(this, scene);
  26. if (!url) {
  27. return;
  28. }
  29. this._textureMatrix = BABYLON.Matrix.Identity();
  30. this.name = url;
  31. this.url = url;
  32. this.hasAlpha = false;
  33. this.isCube = false;
  34. this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  35. this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  36. this.anisotropicFilteringLevel = 1;
  37. this._texture = this._getFromCache(url, true);
  38. if (!this._texture) {
  39. if (!scene.useDelayedTextureLoading) {
  40. this.loadTexture();
  41. }
  42. else {
  43. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  44. }
  45. }
  46. }
  47. /**
  48. * Returns the texture matrix used in most of the material.
  49. * This is not used in color grading but keep for troubleshooting purpose (easily swap diffuse by colorgrading to look in).
  50. */
  51. ColorGradingTexture.prototype.getTextureMatrix = function () {
  52. return this._textureMatrix;
  53. };
  54. /**
  55. * Occurs when the file being loaded is a .3dl LUT file.
  56. */
  57. ColorGradingTexture.prototype.load3dlTexture = function () {
  58. var _this = this;
  59. var mipLevels = 0;
  60. var floatArrayView = null;
  61. var texture = this.getScene().getEngine().createRawTexture(null, 1, 1, BABYLON.Engine.TEXTUREFORMAT_RGBA, false, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  62. this._texture = texture;
  63. var callback = function (text) {
  64. var data;
  65. var tempData;
  66. var line;
  67. var lines = text.split('\n');
  68. var size = 0, pixelIndexW = 0, pixelIndexH = 0, pixelIndexSlice = 0;
  69. var maxColor = 0;
  70. for (var i = 0; i < lines.length; i++) {
  71. line = lines[i];
  72. if (!ColorGradingTexture._noneEmptyLineRegex.test(line))
  73. continue;
  74. if (line.indexOf('#') === 0)
  75. continue;
  76. var words = line.split(" ");
  77. if (size === 0) {
  78. // Number of space + one
  79. size = words.length;
  80. data = new Uint8Array(size * size * size * 4); // volume texture of side size and rgb 8
  81. tempData = new Float32Array(size * size * size * 4);
  82. continue;
  83. }
  84. if (size != 0) {
  85. var r = Math.max(parseInt(words[0]), 0);
  86. var g = Math.max(parseInt(words[1]), 0);
  87. var b = Math.max(parseInt(words[2]), 0);
  88. maxColor = Math.max(r, maxColor);
  89. maxColor = Math.max(g, maxColor);
  90. maxColor = Math.max(b, maxColor);
  91. var pixelStorageIndex = (pixelIndexW + pixelIndexSlice * size + pixelIndexH * size * size) * 4;
  92. tempData[pixelStorageIndex + 0] = r;
  93. tempData[pixelStorageIndex + 1] = g;
  94. tempData[pixelStorageIndex + 2] = b;
  95. tempData[pixelStorageIndex + 3] = 0;
  96. pixelIndexSlice++;
  97. if (pixelIndexSlice % size == 0) {
  98. pixelIndexH++;
  99. pixelIndexSlice = 0;
  100. if (pixelIndexH % size == 0) {
  101. pixelIndexW++;
  102. pixelIndexH = 0;
  103. }
  104. }
  105. }
  106. }
  107. for (var i = 0; i < tempData.length; i++) {
  108. var value = tempData[i];
  109. data[i] = (value / maxColor * 255);
  110. }
  111. _this.getScene().getEngine().updateTextureSize(texture, size * size, size);
  112. _this.getScene().getEngine().updateRawTexture(texture, data, BABYLON.Engine.TEXTUREFORMAT_RGBA, false);
  113. };
  114. BABYLON.Tools.LoadFile(this.url, callback);
  115. return this._texture;
  116. };
  117. /**
  118. * Starts the loading process of the texture.
  119. */
  120. ColorGradingTexture.prototype.loadTexture = function () {
  121. if (this.url && this.url.toLocaleLowerCase().indexOf(".3dl") == (this.url.length - 4)) {
  122. this.load3dlTexture();
  123. }
  124. };
  125. /**
  126. * Clones the color gradind texture.
  127. */
  128. ColorGradingTexture.prototype.clone = function () {
  129. var newTexture = new ColorGradingTexture(this.url, this.getScene());
  130. // Base texture
  131. newTexture.level = this.level;
  132. return newTexture;
  133. };
  134. /**
  135. * Called during delayed load for textures.
  136. */
  137. ColorGradingTexture.prototype.delayLoad = function () {
  138. if (this.delayLoadState !== BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  139. return;
  140. }
  141. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  142. this._texture = this._getFromCache(this.url, true);
  143. if (!this._texture) {
  144. this.loadTexture();
  145. }
  146. };
  147. /**
  148. * Binds the color grading to the shader.
  149. * @param colorGrading The texture to bind
  150. * @param effect The effect to bind to
  151. */
  152. ColorGradingTexture.Bind = function (colorGrading, effect) {
  153. effect.setTexture("cameraColorGrading2DSampler", colorGrading);
  154. var x = colorGrading.level; // Texture Level
  155. var y = colorGrading.getSize().height; // Texture Size example with 8
  156. var z = y - 1.0; // SizeMinusOne 8 - 1
  157. var w = 1 / y; // Space of 1 slice 1 / 8
  158. effect.setFloat4("vCameraColorGradingInfos", x, y, z, w);
  159. var slicePixelSizeU = w / y; // Space of 1 pixel in U direction, e.g. 1/64
  160. var slicePixelSizeV = w; // Space of 1 pixel in V direction, e.g. 1/8 // Space of 1 pixel in V direction, e.g. 1/8
  161. var x2 = z * slicePixelSizeU; // Extent of lookup range in U for a single slice so that range corresponds to (size-1) texels, for example 7/64
  162. var y2 = z / y; // Extent of lookup range in V for a single slice so that range corresponds to (size-1) texels, for example 7/8
  163. var z2 = 0.5 * slicePixelSizeU; // Offset of lookup range in U to align sample position with texel centre, for example 0.5/64
  164. var w2 = 0.5 * slicePixelSizeV; // Offset of lookup range in V to align sample position with texel centre, for example 0.5/8
  165. effect.setFloat4("vCameraColorGradingScaleOffset", x2, y2, z2, w2);
  166. };
  167. /**
  168. * Prepare the list of uniforms associated with the ColorGrading effects.
  169. * @param uniformsList The list of uniforms used in the effect
  170. * @param samplersList The list of samplers used in the effect
  171. */
  172. ColorGradingTexture.PrepareUniformsAndSamplers = function (uniformsList, samplersList) {
  173. uniformsList.push("vCameraColorGradingInfos", "vCameraColorGradingScaleOffset");
  174. samplersList.push("cameraColorGrading2DSampler");
  175. };
  176. /**
  177. * Parses a color grading texture serialized by Babylon.
  178. * @param parsedTexture The texture information being parsedTexture
  179. * @param scene The scene to load the texture in
  180. * @param rootUrl The root url of the data assets to load
  181. * @return A color gradind texture
  182. */
  183. ColorGradingTexture.Parse = function (parsedTexture, scene, rootUrl) {
  184. var texture = null;
  185. if (parsedTexture.name && !parsedTexture.isRenderTarget) {
  186. texture = new BABYLON.ColorGradingTexture(parsedTexture.name, scene);
  187. texture.name = parsedTexture.name;
  188. texture.level = parsedTexture.level;
  189. }
  190. return texture;
  191. };
  192. /**
  193. * Serializes the LUT texture to json format.
  194. */
  195. ColorGradingTexture.prototype.serialize = function () {
  196. if (!this.name) {
  197. return null;
  198. }
  199. var serializationObject = {};
  200. serializationObject.name = this.name;
  201. serializationObject.level = this.level;
  202. serializationObject.customType = "BABYLON.ColorGradingTexture";
  203. return serializationObject;
  204. };
  205. /**
  206. * Empty line regex stored for GC.
  207. */
  208. ColorGradingTexture._noneEmptyLineRegex = /\S+/;
  209. return ColorGradingTexture;
  210. }(BABYLON.BaseTexture));
  211. BABYLON.ColorGradingTexture = ColorGradingTexture;
  212. })(BABYLON || (BABYLON = {}));