Skip to content

Commit a770a58

Browse files
fix: set r159 update ranges (#1744)
1 parent b436ad7 commit a770a58

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/core/Cloud.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { MaterialNode, extend, applyProps, useFrame, ReactThreeFiber } from '@react-three/fiber'
1717
import { useTexture } from './useTexture'
1818
import { v4 } from 'uuid'
19+
import { setUpdateRange } from '../helpers/deprecated'
1920

2021
declare global {
2122
namespace JSX {
@@ -175,11 +176,13 @@ export const Clouds = /* @__PURE__ */ React.forwardRef<Group, CloudsProps>(
175176
})
176177

177178
React.useLayoutEffect(() => {
178-
const updateRange = Math.min(limit, range !== undefined ? range : limit, clouds.current.length)
179-
instance.current.count = updateRange
180-
instance.current.instanceMatrix.updateRange.count = updateRange * 16
181-
if (instance.current.instanceColor) instance.current.instanceColor.updateRange.count = updateRange * 3
182-
;(instance.current.geometry.attributes.opacity as BufferAttribute).updateRange.count = updateRange
179+
const count = Math.min(limit, range !== undefined ? range : limit, clouds.current.length)
180+
instance.current.count = count
181+
setUpdateRange(instance.current.instanceMatrix, { offset: 0, count: count * 16 })
182+
if (instance.current.instanceColor) {
183+
setUpdateRange(instance.current.instanceColor, { offset: 0, count: count * 3 })
184+
}
185+
setUpdateRange(instance.current.geometry.attributes.opacity as BufferAttribute, { offset: 0, count: count })
183186
})
184187

185188
let imageBounds = [cloudTexture!.image.width ?? 1, cloudTexture!.image.height ?? 1]

src/core/Instances.tsx

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ReactThreeFiber, extend, useFrame } from '@react-three/fiber'
44
import mergeRefs from 'react-merge-refs'
55
import Composer from 'react-composer'
66
import { ForwardRefComponent } from '../helpers/ts-utils'
7+
import { setUpdateRange } from '../helpers/deprecated'
78

89
declare global {
910
namespace JSX {
@@ -129,18 +130,18 @@ export const Instances: ForwardRefComponent<InstancesProps, InstancedMesh> = /*
129130
parentRef.current.instanceMatrix.needsUpdate = true
130131
})
131132

133+
let iterations = 0
132134
let count = 0
133-
let updateRange = 0
134135
useFrame(() => {
135-
if (frames === Infinity || count < frames) {
136+
if (frames === Infinity || iterations < frames) {
136137
parentRef.current.updateMatrix()
137138
parentRef.current.updateMatrixWorld()
138139
parentMatrix.copy(parentRef.current.matrixWorld).invert()
139140

140-
updateRange = Math.min(limit, range !== undefined ? range : limit, instances.length)
141-
parentRef.current.count = updateRange
142-
parentRef.current.instanceMatrix.updateRange.count = updateRange * 16
143-
parentRef.current.instanceColor.updateRange.count = updateRange * 3
141+
count = Math.min(limit, range !== undefined ? range : limit, instances.length)
142+
parentRef.current.count = count
143+
setUpdateRange(parentRef.current.instanceMatrix, { offset: 0, count: count * 16 })
144+
setUpdateRange(parentRef.current.instanceColor, { offset: 0, count: count * 3 })
144145

145146
for (let i = 0; i < instances.length; i++) {
146147
const instance = instances[i].current
@@ -153,7 +154,7 @@ export const Instances: ForwardRefComponent<InstancesProps, InstancedMesh> = /*
153154
instance.color.toArray(colors, i * 3)
154155
parentRef.current.instanceColor.needsUpdate = true
155156
}
156-
count++
157+
iterations++
157158
}
158159
})
159160

src/helpers/deprecated.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Sets `BufferAttribute.updateRange` since r159.
3+
*/
4+
export const setUpdateRange = (
5+
attribute: THREE.BufferAttribute,
6+
updateRange: { offset: number; count: number }
7+
): void => {
8+
if ('updateRanges' in attribute) {
9+
// r159
10+
// @ts-ignore
11+
attribute.updateRanges[0] = updateRange
12+
} else {
13+
attribute.updateRange = updateRange
14+
}
15+
}

0 commit comments

Comments
 (0)