File tree 4 files changed +34
-4
lines changed
4 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 1
1
import {
2
+ FloatType ,
3
+ HalfFloatType ,
2
4
LinearFilter ,
3
5
Loader ,
4
6
type Data3DTexture ,
5
- type DataTexture
7
+ type DataTexture ,
8
+ type WebGLRenderer
6
9
} from 'three'
7
10
import { EXRLoader } from 'three-stdlib'
8
11
import join from 'url-join'
@@ -11,6 +14,7 @@ import {
11
14
createData3DTextureLoader ,
12
15
createDataTextureLoader ,
13
16
EXR3DLoader ,
17
+ Float16Array ,
14
18
parseFloat16Array
15
19
} from '@takram/three-geospatial'
16
20
@@ -37,6 +41,15 @@ export interface PrecomputedTextures {
37
41
38
42
export class PrecomputedTexturesLoader extends Loader < PrecomputedTextures > {
39
43
format : 'binary' | 'exr' = 'exr'
44
+ type : typeof FloatType | typeof HalfFloatType = HalfFloatType
45
+
46
+ setTypeFromRenderer ( renderer : WebGLRenderer ) : this {
47
+ this . type =
48
+ renderer . getContext ( ) . getExtension ( 'OES_texture_float_linear' ) == null
49
+ ? HalfFloatType
50
+ : FloatType
51
+ return this
52
+ }
40
53
41
54
override load (
42
55
url : string ,
@@ -57,6 +70,16 @@ export class PrecomputedTexturesLoader extends Loader<PrecomputedTextures> {
57
70
texture => {
58
71
texture . minFilter = LinearFilter
59
72
texture . magFilter = LinearFilter
73
+
74
+ // Using a half-float buffer introduces artifacts seemingly due to
75
+ // insufficient precision in linear interpolation.
76
+ texture . type = this . type
77
+ if ( this . type === FloatType ) {
78
+ texture . image . data = new Float32Array (
79
+ new Float16Array ( texture . image . data . buffer )
80
+ )
81
+ }
82
+
60
83
result [ `${ name } Texture` ] = texture
61
84
if (
62
85
result . irradianceTexture != null &&
Original file line number Diff line number Diff line change
1
+ import { useThree } from '@react-three/fiber'
1
2
import {
2
3
createContext ,
3
4
forwardRef ,
@@ -92,12 +93,14 @@ export const Atmosphere = /*#__PURE__*/ forwardRef<
92
93
shadowLength : null
93
94
} )
94
95
96
+ const gl = useThree ( ( { gl } ) => gl )
95
97
const [ textures , setTextures ] = useState (
96
98
typeof texturesProp !== 'string' ? texturesProp : undefined
97
99
)
98
100
useEffect ( ( ) => {
99
101
if ( typeof texturesProp === 'string' ) {
100
102
const loader = new PrecomputedTexturesLoader ( )
103
+ loader . setTypeFromRenderer ( gl )
101
104
; ( async ( ) => {
102
105
setTextures ( await loader . loadAsync ( texturesProp ) )
103
106
} ) ( ) . catch ( error => {
@@ -106,7 +109,7 @@ export const Atmosphere = /*#__PURE__*/ forwardRef<
106
109
} else {
107
110
setTextures ( texturesProp )
108
111
}
109
- } , [ texturesProp ] )
112
+ } , [ texturesProp , gl ] )
110
113
111
114
const context = useMemo (
112
115
( ) => ( {
Original file line number Diff line number Diff line change @@ -161,7 +161,9 @@ function init(): void {
161
161
)
162
162
163
163
// Load precomputed textures.
164
- new PrecomputedTexturesLoader ( ) . load ( 'atmosphere' , onPrecomputedTexturesLoad )
164
+ new PrecomputedTexturesLoader ( )
165
+ . setTypeFromRenderer ( renderer )
166
+ . load ( 'atmosphere' , onPrecomputedTexturesLoad )
165
167
166
168
container . appendChild ( renderer . domElement )
167
169
window . addEventListener ( 'resize' , onWindowResize )
Original file line number Diff line number Diff line change @@ -150,7 +150,9 @@ function init(): void {
150
150
)
151
151
152
152
// Load precomputed textures.
153
- new PrecomputedTexturesLoader ( ) . load ( 'atmosphere' , onPrecomputedTexturesLoad )
153
+ new PrecomputedTexturesLoader ( )
154
+ . setTypeFromRenderer ( renderer )
155
+ . load ( 'atmosphere' , onPrecomputedTexturesLoad )
154
156
155
157
// Load textures for the clouds.
156
158
new TextureLoader ( ) . load ( 'clouds/local_weather.png' , onLocalWeatherLoad )
You can’t perform that action at this time.
0 commit comments