Skip to content

Commit 6de857c

Browse files
committed
Merge branch 'issue/atmosphere-internal-precision'
2 parents 2b4c769 + 4b58e52 commit 6de857c

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

packages/atmosphere/src/PrecomputedTexturesLoader.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {
2+
FloatType,
3+
HalfFloatType,
24
LinearFilter,
35
Loader,
46
type Data3DTexture,
5-
type DataTexture
7+
type DataTexture,
8+
type WebGLRenderer
69
} from 'three'
710
import { EXRLoader } from 'three-stdlib'
811
import join from 'url-join'
@@ -11,6 +14,7 @@ import {
1114
createData3DTextureLoader,
1215
createDataTextureLoader,
1316
EXR3DLoader,
17+
Float16Array,
1418
parseFloat16Array
1519
} from '@takram/three-geospatial'
1620

@@ -37,6 +41,15 @@ export interface PrecomputedTextures {
3741

3842
export class PrecomputedTexturesLoader extends Loader<PrecomputedTextures> {
3943
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+
}
4053

4154
override load(
4255
url: string,
@@ -57,6 +70,16 @@ export class PrecomputedTexturesLoader extends Loader<PrecomputedTextures> {
5770
texture => {
5871
texture.minFilter = LinearFilter
5972
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+
6083
result[`${name}Texture`] = texture
6184
if (
6285
result.irradianceTexture != null &&

packages/atmosphere/src/r3f/Atmosphere.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useThree } from '@react-three/fiber'
12
import {
23
createContext,
34
forwardRef,
@@ -92,12 +93,14 @@ export const Atmosphere = /*#__PURE__*/ forwardRef<
9293
shadowLength: null
9394
})
9495

96+
const gl = useThree(({ gl }) => gl)
9597
const [textures, setTextures] = useState(
9698
typeof texturesProp !== 'string' ? texturesProp : undefined
9799
)
98100
useEffect(() => {
99101
if (typeof texturesProp === 'string') {
100102
const loader = new PrecomputedTexturesLoader()
103+
loader.setTypeFromRenderer(gl)
101104
;(async () => {
102105
setTextures(await loader.loadAsync(texturesProp))
103106
})().catch(error => {
@@ -106,7 +109,7 @@ export const Atmosphere = /*#__PURE__*/ forwardRef<
106109
} else {
107110
setTextures(texturesProp)
108111
}
109-
}, [texturesProp])
112+
}, [texturesProp, gl])
110113

111114
const context = useMemo(
112115
() => ({

storybook/src/atmosphere/Atmosphere-Vanilla.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ function init(): void {
161161
)
162162

163163
// Load precomputed textures.
164-
new PrecomputedTexturesLoader().load('atmosphere', onPrecomputedTexturesLoad)
164+
new PrecomputedTexturesLoader()
165+
.setTypeFromRenderer(renderer)
166+
.load('atmosphere', onPrecomputedTexturesLoad)
165167

166168
container.appendChild(renderer.domElement)
167169
window.addEventListener('resize', onWindowResize)

storybook/src/clouds/Clouds-Vanilla.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ function init(): void {
150150
)
151151

152152
// Load precomputed textures.
153-
new PrecomputedTexturesLoader().load('atmosphere', onPrecomputedTexturesLoad)
153+
new PrecomputedTexturesLoader()
154+
.setTypeFromRenderer(renderer)
155+
.load('atmosphere', onPrecomputedTexturesLoad)
154156

155157
// Load textures for the clouds.
156158
new TextureLoader().load('clouds/local_weather.png', onLocalWeatherLoad)

0 commit comments

Comments
 (0)