Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f77a7d

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
feat: update setting color space and encoding with r152
1 parent 8ffedb3 commit 3f77a7d

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

libs/angular-three/src/lib/stores/store.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,14 @@ export class NgtStore extends NgtRxStore<NgtState> {
335335
if ('enabled' in ColorManagement) ColorManagement['enabled'] = !legacy ?? false;
336336
else if ('legacyMode' in ColorManagement) ColorManagement['legacyMode'] = legacy ?? true;
337337
}
338-
const outputEncoding = linear ? THREE.LinearEncoding : THREE.sRGBEncoding;
339-
const toneMapping = flat ? THREE.NoToneMapping : THREE.ACESFilmicToneMapping;
340338

341-
if (gl.outputEncoding !== outputEncoding) gl.outputEncoding = outputEncoding;
342-
if (gl.toneMapping !== toneMapping) gl.toneMapping = toneMapping;
339+
// set color space and tonemapping preferences
340+
const LinearEncoding = 3000;
341+
const sRGBEncoding = 3001;
342+
applyProps(gl, {
343+
outputEncoding: linear ? LinearEncoding : sRGBEncoding,
344+
toneMapping: flat ? THREE.NoToneMapping : THREE.ACESFilmicToneMapping,
345+
});
343346

344347
// Update color management state
345348
if (state.legacy !== legacy) stateToUpdate.legacy = legacy;

libs/angular-three/src/lib/utils/apply-props.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,49 @@ export function applyProps(instance: NgtInstanceNode, props: NgtAnyRecord): NgtI
2626
const changes = diffProps(instance, props);
2727

2828
for (let i = 0; i < changes.length; i++) {
29-
const key = changes[i][0];
29+
let key = changes[i][0];
3030
const currentInstance = instance;
3131
const targetProp = currentInstance[key] as NgtAnyRecord;
32-
const value = changes[i][1];
32+
let value = changes[i][1];
33+
34+
if (is.colorSpaceExist(currentInstance)) {
35+
const sRGBEncoding = 3001;
36+
const SRGBColorSpace = 'srgb';
37+
const LinearSRGBColorSpace = 'srgb-linear';
38+
39+
if (key === 'encoding') {
40+
key = 'colorSpace';
41+
value = value === sRGBEncoding ? SRGBColorSpace : LinearSRGBColorSpace;
42+
} else if (key === 'outputEncoding') {
43+
key = 'outputColorSpace';
44+
value = value === sRGBEncoding ? SRGBColorSpace : LinearSRGBColorSpace;
45+
}
46+
}
3347

3448
// special treatmen for objects with support for set/copy, and layers
3549
if (targetProp && targetProp['set'] && (targetProp['copy'] || targetProp instanceof THREE.Layers)) {
3650
const isColor = targetProp instanceof THREE.Color;
3751
// if value is an array
3852
if (Array.isArray(value)) {
39-
if (targetProp['fromArray']) targetProp['fromArray'](value);
53+
if ((targetProp as NgtAnyRecord)['fromArray']) (targetProp as NgtAnyRecord)['fromArray'](value);
4054
else targetProp['set'](...value);
4155
}
4256
// test again target.copy
4357
else if (
44-
targetProp['copy'] &&
58+
(targetProp as NgtAnyRecord)['copy'] &&
4559
value &&
4660
value.constructor &&
4761
targetProp.constructor.name === value.constructor.name
4862
) {
49-
targetProp['copy'](value);
63+
(targetProp as NgtAnyRecord)['copy'](value);
5064
if (!THREE.ColorManagement && !rootState.linear && isColor) targetProp['convertSRGBToLinear']();
5165
}
5266
// if nothing else fits, just set the single value, ignore undefined
5367
else if (value !== undefined) {
5468
const isColor = targetProp instanceof THREE.Color;
5569
// allow setting array scalars
56-
if (!isColor && targetProp['setScalar']) targetProp['setScalar'](value);
70+
if (!isColor && (targetProp as NgtAnyRecord)['setScalar'])
71+
(targetProp as NgtAnyRecord)['setScalar'](value);
5772
// layers have no copy function, copy the mask
5873
else if (targetProp instanceof THREE.Layers && value instanceof THREE.Layers)
5974
targetProp.mask = value.mask;
@@ -69,12 +84,14 @@ export function applyProps(instance: NgtInstanceNode, props: NgtAnyRecord): NgtI
6984
currentInstance[key] = value;
7085
// auto-convert srgb textures
7186
if (
72-
!rootState?.linear &&
7387
currentInstance[key] instanceof THREE.Texture &&
7488
currentInstance[key].format === THREE.RGBAFormat &&
7589
currentInstance[key].type === THREE.UnsignedByteType
7690
) {
77-
currentInstance[key]['encoding'] = THREE.sRGBEncoding;
91+
const texture = currentInstance[key] as THREE.Texture;
92+
if (is.colorSpaceExist(texture) && is.colorSpaceExist(rootState.gl))
93+
texture.colorSpace = rootState.gl.outputColorSpace;
94+
else texture.encoding = rootState.gl.outputEncoding;
7895
}
7996
}
8097

libs/angular-three/src/lib/utils/is.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ElementRef } from '@angular/core';
22
import * as THREE from 'three';
3-
import type { NgtAnyRecord, NgtEquConfig, NgtInstanceNode } from '../types';
3+
import type { NgtAnyRecord, NgtEquConfig, NgtGLRenderer, NgtInstanceNode } from '../types';
44

55
export const is = {
66
obj: (a: unknown): a is object => a === Object(a) && !Array.isArray(a) && typeof a !== 'function',
@@ -16,6 +16,12 @@ export const is = {
1616
object3D: (a: unknown): a is THREE.Object3D => !!a && (a as THREE.Object3D).isObject3D,
1717
instance: (a: unknown): a is NgtInstanceNode => !!a && !!(a as NgtAnyRecord)['__ngt__'],
1818
ref: (a: unknown): a is ElementRef => a instanceof ElementRef,
19+
colorSpaceExist: <
20+
T extends NgtGLRenderer | THREE.Texture | object,
21+
P = T extends NgtGLRenderer ? { outputColorSpace: string } : { colorSpace: string }
22+
>(
23+
object: T
24+
): object is T & P => 'colorSpace' in object || 'outputColorSpace' in object,
1925
equ(a: any, b: any, { arrays = 'shallow', objects = 'reference', strict = true }: NgtEquConfig = {}) {
2026
// Wrong type or one of the two undefined, doesn't match
2127
if (typeof a !== typeof b || !!a !== !!b) return false;

0 commit comments

Comments
 (0)