@@ -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
0 commit comments