@@ -26,34 +26,49 @@ export function applyProps(instance: NgtInstanceNode, props: NgtAnyRecord): NgtI
26
26
const changes = diffProps ( instance , props ) ;
27
27
28
28
for ( let i = 0 ; i < changes . length ; i ++ ) {
29
- const key = changes [ i ] [ 0 ] ;
29
+ let key = changes [ i ] [ 0 ] ;
30
30
const currentInstance = instance ;
31
31
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
+ }
33
47
34
48
// special treatmen for objects with support for set/copy, and layers
35
49
if ( targetProp && targetProp [ 'set' ] && ( targetProp [ 'copy' ] || targetProp instanceof THREE . Layers ) ) {
36
50
const isColor = targetProp instanceof THREE . Color ;
37
51
// if value is an array
38
52
if ( Array . isArray ( value ) ) {
39
- if ( targetProp [ 'fromArray' ] ) targetProp [ 'fromArray' ] ( value ) ;
53
+ if ( ( targetProp as NgtAnyRecord ) [ 'fromArray' ] ) ( targetProp as NgtAnyRecord ) [ 'fromArray' ] ( value ) ;
40
54
else targetProp [ 'set' ] ( ...value ) ;
41
55
}
42
56
// test again target.copy
43
57
else if (
44
- targetProp [ 'copy' ] &&
58
+ ( targetProp as NgtAnyRecord ) [ 'copy' ] &&
45
59
value &&
46
60
value . constructor &&
47
61
targetProp . constructor . name === value . constructor . name
48
62
) {
49
- targetProp [ 'copy' ] ( value ) ;
63
+ ( targetProp as NgtAnyRecord ) [ 'copy' ] ( value ) ;
50
64
if ( ! THREE . ColorManagement && ! rootState . linear && isColor ) targetProp [ 'convertSRGBToLinear' ] ( ) ;
51
65
}
52
66
// if nothing else fits, just set the single value, ignore undefined
53
67
else if ( value !== undefined ) {
54
68
const isColor = targetProp instanceof THREE . Color ;
55
69
// 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 ) ;
57
72
// layers have no copy function, copy the mask
58
73
else if ( targetProp instanceof THREE . Layers && value instanceof THREE . Layers )
59
74
targetProp . mask = value . mask ;
@@ -69,12 +84,14 @@ export function applyProps(instance: NgtInstanceNode, props: NgtAnyRecord): NgtI
69
84
currentInstance [ key ] = value ;
70
85
// auto-convert srgb textures
71
86
if (
72
- ! rootState ?. linear &&
73
87
currentInstance [ key ] instanceof THREE . Texture &&
74
88
currentInstance [ key ] . format === THREE . RGBAFormat &&
75
89
currentInstance [ key ] . type === THREE . UnsignedByteType
76
90
) {
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 ;
78
95
}
79
96
}
80
97
0 commit comments