@monogrid/gainmap-js 2.0.6 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.5
2
+ * @monogrid/gainmap-js v2.0.7
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -48,23 +48,13 @@
48
48
  * @param type
49
49
  * @param renderer
50
50
  * @param camera
51
+ * @param renderTargetOptions
51
52
  * @returns
52
53
  */
53
- const canReadPixels = (type, renderer, camera) => {
54
+ const canReadPixels = (type, renderer, camera, renderTargetOptions) => {
54
55
  if (_canReadPixelsResult !== undefined)
55
56
  return _canReadPixelsResult;
56
- const testRT = new three.WebGLRenderTarget(1, 1, {
57
- type,
58
- colorSpace: three.LinearSRGBColorSpace,
59
- format: three.RGBAFormat,
60
- magFilter: three.LinearFilter,
61
- minFilter: three.LinearFilter,
62
- wrapS: three.RepeatWrapping,
63
- wrapT: three.RepeatWrapping,
64
- depthBuffer: false,
65
- stencilBuffer: false,
66
- generateMipmaps: true
67
- });
57
+ const testRT = new three.WebGLRenderTarget(1, 1, renderTargetOptions);
68
58
  renderer.setRenderTarget(testRT);
69
59
  const mesh = new three.Mesh(new three.PlaneGeometry(), new three.MeshBasicMaterial({ color: 0xffffff }));
70
60
  renderer.render(mesh, camera);
@@ -89,7 +79,8 @@
89
79
  * @param sourceTexture
90
80
  * @param renderer
91
81
  */
92
- constructor(width, height, type, colorSpace, material, renderer) {
82
+ constructor(options) {
83
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
93
84
  this._rendererIsDisposable = false;
94
85
  this._supportsReadPixels = true;
95
86
  /**
@@ -106,13 +97,29 @@
106
97
  }
107
98
  this._renderer.setRenderTarget(null);
108
99
  };
109
- this._width = width;
110
- this._height = height;
111
- this._type = type;
112
- this._colorSpace = colorSpace;
113
- this._material = material;
114
- if (renderer) {
115
- this._renderer = renderer;
100
+ this._width = options.width;
101
+ this._height = options.height;
102
+ this._type = options.type;
103
+ this._colorSpace = options.colorSpace;
104
+ const rtOptions = {
105
+ // fixed options
106
+ format: three.RGBAFormat,
107
+ depthBuffer: false,
108
+ stencilBuffer: false,
109
+ // user options
110
+ type: this._type,
111
+ colorSpace: this._colorSpace,
112
+ anisotropy: ((_a = options.renderTargetOptions) === null || _a === void 0 ? void 0 : _a.anisotropy) !== undefined ? (_b = options.renderTargetOptions) === null || _b === void 0 ? void 0 : _b.anisotropy : 1,
113
+ generateMipmaps: ((_c = options.renderTargetOptions) === null || _c === void 0 ? void 0 : _c.generateMipmaps) !== undefined ? (_d = options.renderTargetOptions) === null || _d === void 0 ? void 0 : _d.generateMipmaps : false,
114
+ magFilter: ((_e = options.renderTargetOptions) === null || _e === void 0 ? void 0 : _e.magFilter) !== undefined ? (_f = options.renderTargetOptions) === null || _f === void 0 ? void 0 : _f.magFilter : three.LinearFilter,
115
+ minFilter: ((_g = options.renderTargetOptions) === null || _g === void 0 ? void 0 : _g.minFilter) !== undefined ? (_h = options.renderTargetOptions) === null || _h === void 0 ? void 0 : _h.minFilter : three.LinearFilter,
116
+ samples: ((_j = options.renderTargetOptions) === null || _j === void 0 ? void 0 : _j.samples) !== undefined ? (_k = options.renderTargetOptions) === null || _k === void 0 ? void 0 : _k.samples : undefined,
117
+ wrapS: ((_l = options.renderTargetOptions) === null || _l === void 0 ? void 0 : _l.wrapS) !== undefined ? (_m = options.renderTargetOptions) === null || _m === void 0 ? void 0 : _m.wrapS : three.ClampToEdgeWrapping,
118
+ wrapT: ((_o = options.renderTargetOptions) === null || _o === void 0 ? void 0 : _o.wrapT) !== undefined ? (_p = options.renderTargetOptions) === null || _p === void 0 ? void 0 : _p.wrapT : three.ClampToEdgeWrapping
119
+ };
120
+ this._material = options.material;
121
+ if (options.renderer) {
122
+ this._renderer = options.renderer;
116
123
  }
117
124
  else {
118
125
  this._renderer = QuadRenderer.instantiateRenderer();
@@ -126,7 +133,7 @@
126
133
  this._camera.top = 0.5;
127
134
  this._camera.bottom = -0.5;
128
135
  this._camera.updateProjectionMatrix();
129
- if (!canReadPixels(this._type, this._renderer, this._camera)) {
136
+ if (!canReadPixels(this._type, this._renderer, this._camera, rtOptions)) {
130
137
  let alternativeType;
131
138
  switch (this._type) {
132
139
  case three.HalfFloatType:
@@ -145,18 +152,8 @@
145
152
  this._quad = new three.Mesh(new three.PlaneGeometry(), this._material);
146
153
  this._quad.geometry.computeBoundingBox();
147
154
  this._scene.add(this._quad);
148
- this._renderTarget = new three.WebGLRenderTarget(width, height, {
149
- type: this._type,
150
- colorSpace,
151
- format: three.RGBAFormat,
152
- magFilter: three.LinearFilter,
153
- minFilter: three.LinearMipMapLinearFilter,
154
- wrapS: three.RepeatWrapping,
155
- wrapT: three.RepeatWrapping,
156
- depthBuffer: false,
157
- stencilBuffer: false,
158
- generateMipmaps: true
159
- });
155
+ this._renderTarget = new three.WebGLRenderTarget(this.width, this.height, rtOptions);
156
+ this._renderTarget.texture.mapping = ((_q = options.renderTargetOptions) === null || _q === void 0 ? void 0 : _q.mapping) !== undefined ? (_r = options.renderTargetOptions) === null || _r === void 0 ? void 0 : _r.mapping : three.UVMapping;
160
157
  }
161
158
  /**
162
159
  * Instantiates a temporary renderer
@@ -185,19 +182,85 @@
185
182
  this._renderer.readRenderTargetPixels(this._renderTarget, 0, 0, this._width, this._height, out);
186
183
  return out;
187
184
  }
188
- toDataTexture() {
189
- return new three.DataTexture(this.toArray(), this.width, this.height, three.RGBAFormat, this._type, three.UVMapping, three.RepeatWrapping, three.RepeatWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, 1, three.LinearSRGBColorSpace);
185
+ /**
186
+ * Performs a readPixel operation in the renderTarget
187
+ * and returns a DataTexture containing the read data
188
+ *
189
+ * @params options
190
+ * @returns
191
+ */
192
+ toDataTexture(options) {
193
+ const returnValue = new three.DataTexture(
194
+ // fixed values
195
+ this.toArray(), this.width, this.height, three.RGBAFormat, this._type,
196
+ // user values
197
+ (options === null || options === void 0 ? void 0 : options.mapping) || three.UVMapping, (options === null || options === void 0 ? void 0 : options.wrapS) || three.ClampToEdgeWrapping, (options === null || options === void 0 ? void 0 : options.wrapT) || three.ClampToEdgeWrapping, (options === null || options === void 0 ? void 0 : options.magFilter) || three.LinearFilter, (options === null || options === void 0 ? void 0 : options.minFilter) || three.LinearFilter, (options === null || options === void 0 ? void 0 : options.anisotropy) || 1,
198
+ // fixed value
199
+ three.LinearSRGBColorSpace);
200
+ // set this afterwards, we can't set it in constructor
201
+ returnValue.generateMipmaps = (options === null || options === void 0 ? void 0 : options.generateMipmaps) !== undefined ? options === null || options === void 0 ? void 0 : options.generateMipmaps : false;
202
+ return returnValue;
190
203
  }
191
204
  /**
192
205
  * If using a disposable renderer, it will dispose it.
193
206
  */
194
- dispose() {
207
+ disposeOnDemandRenderer() {
195
208
  this._renderer.setRenderTarget(null);
196
209
  if (this._rendererIsDisposable) {
197
210
  this._renderer.dispose();
198
211
  this._renderer.forceContextLoss();
199
212
  }
200
213
  }
214
+ /**
215
+ * Will dispose of **all** assets used by this renderer.
216
+ *
217
+ *
218
+ * @param disposeRenderTarget will dispose of the renderTarget which will not be usable later
219
+ * set this to true if you passed the `renderTarget.texture` to a `PMREMGenerator`
220
+ * or are otherwise done with it.
221
+ *
222
+ * @example
223
+ * ```js
224
+ * const loader = new HDRJPGLoader(renderer)
225
+ * const result = await loader.loadAsync('gainmap.jpeg')
226
+ * const mesh = new Mesh(geometry, new MeshBasicMaterial({ map: result.renderTarget.texture }) )
227
+ * // DO NOT dispose the renderTarget here,
228
+ * // it is used directly in the material
229
+ * result.dispose()
230
+ * ```
231
+ *
232
+ * @example
233
+ * ```js
234
+ * const loader = new HDRJPGLoader(renderer)
235
+ * const pmremGenerator = new PMREMGenerator( renderer );
236
+ * const result = await loader.loadAsync('gainmap.jpeg')
237
+ * const envMap = pmremGenerator.fromEquirectangular(result.renderTarget.texture)
238
+ * const mesh = new Mesh(geometry, new MeshStandardMaterial({ envMap }) )
239
+ * // renderTarget can be disposed here
240
+ * // because it was used to generate a PMREM texture
241
+ * result.dispose(true)
242
+ * ```
243
+ */
244
+ dispose(disposeRenderTarget) {
245
+ this.disposeOnDemandRenderer();
246
+ if (disposeRenderTarget) {
247
+ this.renderTarget.dispose();
248
+ }
249
+ // dispose shader material texture uniforms
250
+ if (this.material instanceof three.ShaderMaterial) {
251
+ Object.values(this.material.uniforms).forEach(v => {
252
+ if (v.value instanceof three.Texture)
253
+ v.value.dispose();
254
+ });
255
+ }
256
+ // dispose other material properties
257
+ Object.values(this.material).forEach(value => {
258
+ if (value instanceof three.Texture)
259
+ value.dispose();
260
+ });
261
+ this.material.dispose();
262
+ this._quad.geometry.dispose();
263
+ }
201
264
  /**
202
265
  * Width of the texture
203
266
  */
@@ -429,6 +492,9 @@ void main() {
429
492
  * scene.add(mesh)
430
493
  * renderer.render(scene, new PerspectiveCamera())
431
494
  *
495
+ * // result must be manually disposed
496
+ * // when you are done using it
497
+ * result.dispose()
432
498
  *
433
499
  * @param params
434
500
  * @returns
@@ -451,19 +517,35 @@ void main() {
451
517
  sdr,
452
518
  gainMap
453
519
  });
454
- // TODO: three types are generic, eslint complains here, see how we can solve
455
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
456
- const quadRenderer = new QuadRenderer(sdr.image.width, sdr.image.height, three.HalfFloatType, three.LinearSRGBColorSpace, material, renderer);
520
+ const quadRenderer = new QuadRenderer({
521
+ // TODO: three types are generic, eslint complains here, see how we can solve
522
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
523
+ width: sdr.image.width,
524
+ // TODO: three types are generic, eslint complains here, see how we can solve
525
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
526
+ height: sdr.image.height,
527
+ type: three.HalfFloatType,
528
+ colorSpace: three.LinearSRGBColorSpace,
529
+ material,
530
+ renderer,
531
+ renderTargetOptions: params.renderTargetOptions
532
+ });
457
533
  try {
458
534
  quadRenderer.render();
459
535
  }
460
536
  catch (e) {
461
- quadRenderer.dispose();
537
+ quadRenderer.disposeOnDemandRenderer();
462
538
  throw e;
463
539
  }
464
540
  return quadRenderer;
465
541
  };
466
542
 
543
+ class GainMapNotFoundError extends Error {
544
+ }
545
+
546
+ class XMPMetadataNotFoundError extends Error {
547
+ }
548
+
467
549
  const getAttribute = (description, name, defaultValue) => {
468
550
  var _a;
469
551
  let returnValue;
@@ -499,7 +581,7 @@ void main() {
499
581
  const extractXMP = (input) => {
500
582
  var _a, _b;
501
583
  let str;
502
- // support node test evnvironment
584
+ // support node test environment
503
585
  if (typeof TextDecoder !== 'undefined')
504
586
  str = new TextDecoder().decode(input);
505
587
  else
@@ -726,11 +808,11 @@ void main() {
726
808
  const extractGainmapFromJPEG = async (jpegFile) => {
727
809
  const metadata = extractXMP(jpegFile);
728
810
  if (!metadata)
729
- throw new Error('Gain map XMP metadata not found');
811
+ throw new XMPMetadataNotFoundError('Gain map XMP metadata not found');
730
812
  const mpfExtractor = new MPFExtractor({ extractFII: true, extractNonFII: true });
731
813
  const images = await mpfExtractor.extract(jpegFile);
732
814
  if (images.length !== 2)
733
- throw new Error('Gain map recovery image not found');
815
+ throw new GainMapNotFoundError('Gain map recovery image not found');
734
816
  return {
735
817
  sdr: new Uint8Array(await images[0].arrayBuffer()),
736
818
  gainMap: new Uint8Array(await images[1].arrayBuffer()),
@@ -744,7 +826,7 @@ void main() {
744
826
  * @param blob
745
827
  * @returns
746
828
  */
747
- const getImage = (blob) => {
829
+ const getHTMLImageFromBlob = (blob) => {
748
830
  return new Promise((resolve, reject) => {
749
831
  const img = document.createElement('img');
750
832
  img.onload = () => { resolve(img); };
@@ -752,6 +834,7 @@ void main() {
752
834
  img.src = URL.createObjectURL(blob);
753
835
  });
754
836
  };
837
+
755
838
  class LoaderBase extends three.Loader {
756
839
  /**
757
840
  *
@@ -763,6 +846,16 @@ void main() {
763
846
  this._renderer = renderer;
764
847
  this._internalLoadingManager = new three.LoadingManager();
765
848
  }
849
+ /**
850
+ * Specify the renderTarget options to use when rendering the gain map
851
+ *
852
+ * @param options
853
+ * @returns
854
+ */
855
+ setRenderTargetOptions(options) {
856
+ this._renderTargetOptions = options;
857
+ return this;
858
+ }
766
859
  /**
767
860
  * @private
768
861
  * @returns
@@ -781,26 +874,34 @@ void main() {
781
874
  gainMap: new three.Texture(),
782
875
  sdr: new three.Texture()
783
876
  });
784
- return new QuadRenderer(16, 16, three.HalfFloatType, three.LinearSRGBColorSpace, material, this._renderer);
877
+ return new QuadRenderer({
878
+ width: 16,
879
+ height: 16,
880
+ type: three.HalfFloatType,
881
+ colorSpace: three.LinearSRGBColorSpace,
882
+ material,
883
+ renderer: this._renderer,
884
+ renderTargetOptions: this._renderTargetOptions
885
+ });
785
886
  }
786
887
  /**
787
- *
788
- * @private
789
- * @param quadRenderer
790
- * @param gainMapBuffer
791
- * @param sdrBuffer
792
- * @param metadata
793
- */
794
- async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
795
- const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
888
+ * @private
889
+ * @param quadRenderer
890
+ * @param metadata
891
+ * @param sdrBuffer
892
+ * @param gainMapBuffer
893
+ */
894
+ async render(quadRenderer, metadata, sdrBuffer, gainMapBuffer) {
895
+ // this is optional, will render a black gain-map if not present
896
+ const gainMapBlob = gainMapBuffer ? new Blob([gainMapBuffer], { type: 'image/jpeg' }) : undefined;
796
897
  const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
797
898
  let sdrImage;
798
899
  let gainMapImage;
799
900
  let needsFlip = false;
800
901
  if (typeof createImageBitmap === 'undefined') {
801
902
  const res = await Promise.all([
802
- getImage(gainMapBlob),
803
- getImage(sdrBlob)
903
+ gainMapBlob ? getHTMLImageFromBlob(gainMapBlob) : Promise.resolve(undefined),
904
+ getHTMLImageFromBlob(sdrBlob)
804
905
  ]);
805
906
  gainMapImage = res[0];
806
907
  sdrImage = res[1];
@@ -808,13 +909,13 @@ void main() {
808
909
  }
809
910
  else {
810
911
  const res = await Promise.all([
811
- createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
912
+ gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }) : Promise.resolve(undefined),
812
913
  createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
813
914
  ]);
814
915
  gainMapImage = res[0];
815
916
  sdrImage = res[1];
816
917
  }
817
- const gainMap = new three.Texture(gainMapImage, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.LinearSRGBColorSpace);
918
+ const gainMap = new three.Texture(gainMapImage || new ImageData(2, 2), three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.LinearSRGBColorSpace);
818
919
  gainMap.flipY = needsFlip;
819
920
  gainMap.needsUpdate = true;
820
921
  const sdr = new three.Texture(sdrImage, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
@@ -860,7 +961,7 @@ void main() {
860
961
  *
861
962
  * const loader = new GainMapLoader(renderer)
862
963
  *
863
- * const result = loader.load(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
964
+ * const result = await loader.loadAsync(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
864
965
  * // `result` can be used to populate a Texture
865
966
  *
866
967
  * const scene = new Scene()
@@ -880,6 +981,10 @@ void main() {
880
981
  * scene.background.mapping = EquirectangularReflectionMapping
881
982
  * scene.background.minFilter = LinearFilter
882
983
  *
984
+ * // result must be manually disposed
985
+ * // when you are done using it
986
+ * result.dispose()
987
+ *
883
988
  */
884
989
  class GainMapLoader extends LoaderBase {
885
990
  /**
@@ -903,13 +1008,25 @@ void main() {
903
1008
  let metadata;
904
1009
  const loadCheck = async () => {
905
1010
  if (sdr && gainMap && metadata) {
906
- await this.render(quadRenderer, gainMap, sdr, metadata);
1011
+ // solves #16
1012
+ try {
1013
+ await this.render(quadRenderer, metadata, sdr, gainMap);
1014
+ }
1015
+ catch (error) {
1016
+ this.manager.itemError(sdrUrl);
1017
+ this.manager.itemError(gainMapUrl);
1018
+ this.manager.itemError(metadataUrl);
1019
+ if (typeof onError === 'function')
1020
+ onError(error);
1021
+ quadRenderer.disposeOnDemandRenderer();
1022
+ return;
1023
+ }
907
1024
  if (typeof onLoad === 'function')
908
1025
  onLoad(quadRenderer);
909
1026
  this.manager.itemEnd(sdrUrl);
910
1027
  this.manager.itemEnd(gainMapUrl);
911
1028
  this.manager.itemEnd(metadataUrl);
912
- quadRenderer.dispose();
1029
+ quadRenderer.disposeOnDemandRenderer();
913
1030
  }
914
1031
  };
915
1032
  let sdrLengthComputable = true;
@@ -1005,7 +1122,7 @@ void main() {
1005
1122
  * @group Loaders
1006
1123
  *
1007
1124
  * @example
1008
- * import { JPEGRLoader } from '@monogrid/gainmap-js'
1125
+ * import { HDRJPGLoader } from '@monogrid/gainmap-js'
1009
1126
  * import {
1010
1127
  * EquirectangularReflectionMapping,
1011
1128
  * LinearFilter,
@@ -1019,9 +1136,9 @@ void main() {
1019
1136
  *
1020
1137
  * const renderer = new WebGLRenderer()
1021
1138
  *
1022
- * const loader = new JPEGRLoader(renderer)
1139
+ * const loader = new HDRJPGLoader(renderer)
1023
1140
  *
1024
- * const result = loader.load('gainmap.jpeg')
1141
+ * const result = await loader.loadAsync('gainmap.jpeg')
1025
1142
  * // `result` can be used to populate a Texture
1026
1143
  *
1027
1144
  * const scene = new Scene()
@@ -1040,12 +1157,16 @@ void main() {
1040
1157
  * scene.background = result.toDataTexture()
1041
1158
  * scene.background.mapping = EquirectangularReflectionMapping
1042
1159
  * scene.background.minFilter = LinearFilter
1043
-
1160
+ *
1161
+ * // result must be manually disposed
1162
+ * // when you are done using it
1163
+ * result.dispose()
1044
1164
  *
1045
1165
  */
1046
1166
  class HDRJPGLoader extends LoaderBase {
1047
1167
  /**
1048
- * Loads a JPEGR Image
1168
+ * Loads a JPEG containing gain map metadata
1169
+ * Renders a normal SDR image if gainmap data is not found
1049
1170
  *
1050
1171
  * @param url An array in the form of [sdr.jpg, gainmap.jpg, metadata.json]
1051
1172
  * @param onLoad Load complete callback, will receive the result
@@ -1063,13 +1184,52 @@ void main() {
1063
1184
  this.manager.itemStart(url);
1064
1185
  loader.load(url, async (jpeg) => {
1065
1186
  if (typeof jpeg === 'string')
1066
- throw new Error('Invalid buffer');
1067
- const { gainMap: gainMapJPEG, sdr: sdrJPEG, metadata } = await extractGainmapFromJPEG(new Uint8Array(jpeg));
1068
- await this.render(quadRenderer, gainMapJPEG, sdrJPEG, metadata);
1187
+ throw new Error('Invalid buffer, received [string], was expecting [ArrayBuffer]');
1188
+ const jpegBuffer = new Uint8Array(jpeg);
1189
+ let sdrJPEG;
1190
+ let gainMapJPEG;
1191
+ let metadata;
1192
+ try {
1193
+ const extractionResult = await extractGainmapFromJPEG(jpegBuffer);
1194
+ // gain map is successfully reconstructed
1195
+ sdrJPEG = extractionResult.sdr;
1196
+ gainMapJPEG = extractionResult.gainMap;
1197
+ metadata = extractionResult.metadata;
1198
+ }
1199
+ catch (e) {
1200
+ // render the SDR version if this is not a gainmap
1201
+ if (e instanceof XMPMetadataNotFoundError || e instanceof GainMapNotFoundError) {
1202
+ console.warn(`Failure to reconstruct an HDR image from ${url}: Gain map metadata not found in the file, HDRJPGLoader will render the SDR jpeg`);
1203
+ metadata = {
1204
+ gainMapMin: [0, 0, 0],
1205
+ gainMapMax: [1, 1, 1],
1206
+ gamma: [1, 1, 1],
1207
+ hdrCapacityMin: 0,
1208
+ hdrCapacityMax: 1,
1209
+ offsetHdr: [0, 0, 0],
1210
+ offsetSdr: [0, 0, 0]
1211
+ };
1212
+ sdrJPEG = jpegBuffer;
1213
+ }
1214
+ else {
1215
+ throw e;
1216
+ }
1217
+ }
1218
+ // solves #16
1219
+ try {
1220
+ await this.render(quadRenderer, metadata, sdrJPEG, gainMapJPEG);
1221
+ }
1222
+ catch (error) {
1223
+ this.manager.itemError(url);
1224
+ if (typeof onError === 'function')
1225
+ onError(error);
1226
+ quadRenderer.disposeOnDemandRenderer();
1227
+ return;
1228
+ }
1069
1229
  if (typeof onLoad === 'function')
1070
1230
  onLoad(quadRenderer);
1071
1231
  this.manager.itemEnd(url);
1072
- quadRenderer.dispose();
1232
+ quadRenderer.disposeOnDemandRenderer();
1073
1233
  }, onProgress, (error) => {
1074
1234
  this.manager.itemError(url);
1075
1235
  if (typeof onError === 'function')
@@ -43,7 +43,10 @@ import { EncodingParametersBase } from './types';
43
43
  * encodingResult.gainMap.material.gamma = [1.1, 1.1, 1.1]
44
44
  * encodingResult.gainMap.render()
45
45
  *
46
- * // must be manually disposed
46
+ * // do something with encodingResult.gainMap.toArray()
47
+ * // and encodingResult.sdr.toArray()
48
+ *
49
+ * // renderers must be manually disposed
47
50
  * encodingResult.sdr.dispose()
48
51
  * encodingResult.gainMap.dispose()
49
52
  *
@@ -1,5 +1,6 @@
1
1
  import { DataTexture, ToneMapping, WebGLRenderer } from 'three';
2
2
  import { QuadRenderer } from '../core/QuadRenderer';
3
+ import { QuadRendererTextureOptions } from '../decode';
3
4
  import { SDRMaterial } from './materials/SDRMaterial';
4
5
  /**
5
6
  * Renders an SDR Representation of an HDR Image
@@ -10,6 +11,7 @@ import { SDRMaterial } from './materials/SDRMaterial';
10
11
  * @param hdrTexture The HDR image to be rendered
11
12
  * @param renderer (optional) WebGLRenderer to use during the rendering, a disposable renderer will be create and destroyed if this is not provided.
12
13
  * @param toneMapping (optional) Tone mapping to be applied to the SDR Rendition
14
+ * @param renderTargetOptions (optional) Options to use when creating the output renderTarget
13
15
  * @throws {Error} if the WebGLRenderer fails to render the SDR image
14
16
  */
15
- export declare const getSDRRendition: (hdrTexture: DataTexture, renderer?: WebGLRenderer, toneMapping?: ToneMapping) => QuadRenderer<1009, SDRMaterial>;
17
+ export declare const getSDRRendition: (hdrTexture: DataTexture, renderer?: WebGLRenderer, toneMapping?: ToneMapping, renderTargetOptions?: QuadRendererTextureOptions) => QuadRenderer<1009, SDRMaterial>;
@@ -2,6 +2,7 @@ import { type DataTexture, ToneMapping, WebGLRenderer } from 'three';
2
2
  import { type EXR } from 'three/examples/jsm/loaders/EXRLoader';
3
3
  import { type LogLuv } from 'three/examples/jsm/loaders/LogLuvLoader';
4
4
  import { type RGBE } from 'three/examples/jsm/loaders/RGBELoader';
5
+ import { QuadRendererTextureOptions } from '../decode';
5
6
  import { WorkerInterfaceImplementation } from '../worker-types';
6
7
  /**
7
8
  * Parameters used by content Creators in order to create a GainMap
@@ -82,6 +83,10 @@ export type EncodingParametersBase = GainmapEncodingParameters & {
82
83
  * @defaultValue `ACESFilmicToneMapping`
83
84
  */
84
85
  toneMapping?: ToneMapping;
86
+ /**
87
+ * Options to use when creating the output renderTarget
88
+ */
89
+ renderTargetOptions?: QuadRendererTextureOptions;
85
90
  };
86
91
  /**
87
92
  * This library can provide gainmap compressed in these mimeTypes
package/dist/encode.js CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.5
2
+ * @monogrid/gainmap-js v2.0.7
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
- import { c as compress } from './compress-_4jEQonS.js';
6
+ import { c as compress } from './compress-AzvRyeg9.js';
7
7
  import { DataTexture, RGBAFormat, UVMapping, RepeatWrapping, LinearFilter, LinearSRGBColorSpace, ShaderMaterial, Vector3, NoBlending, UnsignedByteType, ACESFilmicToneMapping, LinearToneMapping, CineonToneMapping, ReinhardToneMapping, SRGBColorSpace, Vector2, WebGLRenderTarget, NearestFilter, ClampToEdgeWrapping, FloatType, DataUtils } from 'three';
8
- import { Q as QuadRenderer } from './QuadRenderer-gqnZPNP-.js';
8
+ import { Q as QuadRenderer } from './QuadRenderer-LVKXL-q4.js';
9
9
 
10
10
  /**
11
11
  * Utility function to obtain a `DataTexture` from various input formats
@@ -195,12 +195,20 @@ const getGainMap = (params) => {
195
195
  sdr: sdr.renderTarget.texture,
196
196
  hdr: dataTexture
197
197
  });
198
- const quadRenderer = new QuadRenderer(dataTexture.image.width, dataTexture.image.height, UnsignedByteType, LinearSRGBColorSpace, material, renderer);
198
+ const quadRenderer = new QuadRenderer({
199
+ width: dataTexture.image.width,
200
+ height: dataTexture.image.height,
201
+ type: UnsignedByteType,
202
+ colorSpace: LinearSRGBColorSpace,
203
+ material,
204
+ renderer,
205
+ renderTargetOptions: params.renderTargetOptions
206
+ });
199
207
  try {
200
208
  quadRenderer.render();
201
209
  }
202
210
  catch (e) {
203
- quadRenderer.dispose();
211
+ quadRenderer.disposeOnDemandRenderer();
204
212
  throw e;
205
213
  }
206
214
  return quadRenderer;
@@ -418,16 +426,25 @@ class SDRMaterial extends ShaderMaterial {
418
426
  * @param hdrTexture The HDR image to be rendered
419
427
  * @param renderer (optional) WebGLRenderer to use during the rendering, a disposable renderer will be create and destroyed if this is not provided.
420
428
  * @param toneMapping (optional) Tone mapping to be applied to the SDR Rendition
429
+ * @param renderTargetOptions (optional) Options to use when creating the output renderTarget
421
430
  * @throws {Error} if the WebGLRenderer fails to render the SDR image
422
431
  */
423
- const getSDRRendition = (hdrTexture, renderer, toneMapping) => {
432
+ const getSDRRendition = (hdrTexture, renderer, toneMapping, renderTargetOptions) => {
424
433
  hdrTexture.needsUpdate = true;
425
- const quadRenderer = new QuadRenderer(hdrTexture.image.width, hdrTexture.image.height, UnsignedByteType, SRGBColorSpace, new SDRMaterial({ map: hdrTexture, toneMapping }), renderer);
434
+ const quadRenderer = new QuadRenderer({
435
+ width: hdrTexture.image.width,
436
+ height: hdrTexture.image.height,
437
+ type: UnsignedByteType,
438
+ colorSpace: SRGBColorSpace,
439
+ material: new SDRMaterial({ map: hdrTexture, toneMapping }),
440
+ renderer,
441
+ renderTargetOptions
442
+ });
426
443
  try {
427
444
  quadRenderer.render();
428
445
  }
429
446
  catch (e) {
430
- quadRenderer.dispose();
447
+ quadRenderer.disposeOnDemandRenderer();
431
448
  throw e;
432
449
  }
433
450
  return quadRenderer;
@@ -476,7 +493,10 @@ const getSDRRendition = (hdrTexture, renderer, toneMapping) => {
476
493
  * encodingResult.gainMap.material.gamma = [1.1, 1.1, 1.1]
477
494
  * encodingResult.gainMap.render()
478
495
  *
479
- * // must be manually disposed
496
+ * // do something with encodingResult.gainMap.toArray()
497
+ * // and encodingResult.sdr.toArray()
498
+ *
499
+ * // renderers must be manually disposed
480
500
  * encodingResult.sdr.dispose()
481
501
  * encodingResult.gainMap.dispose()
482
502
  *
@@ -486,7 +506,7 @@ const getSDRRendition = (hdrTexture, renderer, toneMapping) => {
486
506
  const encode = (params) => {
487
507
  const { image, renderer } = params;
488
508
  const dataTexture = getDataTexture(image);
489
- const sdr = getSDRRendition(dataTexture, renderer, params.toneMapping);
509
+ const sdr = getSDRRendition(dataTexture, renderer, params.toneMapping, params.renderTargetOptions);
490
510
  const gainMapRenderer = getGainMap({
491
511
  ...params,
492
512
  image: dataTexture,
@@ -686,7 +706,14 @@ const findTextureMinMax = (image, mode = 'max', renderer) => {
686
706
  mat.needsUpdate = true;
687
707
  let w = srcTex.image.width;
688
708
  let h = srcTex.image.height;
689
- const quadRenderer = new QuadRenderer(w, h, srcTex.type, srcTex.colorSpace, mat, renderer);
709
+ const quadRenderer = new QuadRenderer({
710
+ width: w,
711
+ height: h,
712
+ type: srcTex.type,
713
+ colorSpace: srcTex.colorSpace,
714
+ material: mat,
715
+ renderer
716
+ });
690
717
  const frameBuffers = [];
691
718
  while (w > 1 || h > 1) {
692
719
  w = Math.max(1, (w + cellSize - 1) / cellSize | 0);