@@ -5,10 +5,20 @@ import type { ValidateUniformSchema } from 'typegpu';
5
5
6
6
interface UniformValue < TSchema , TValue extends d . Infer < TSchema > > {
7
7
schema : TSchema ;
8
- value : TValue | undefined ;
8
+ value : TValue ;
9
9
readonly $ : d . InferGPU < TSchema > ;
10
10
}
11
11
12
+ function initialValueFromSchema < T extends d . AnyWgslData > (
13
+ schema : ValidateUniformSchema < T > ,
14
+ ) : d . Infer < T > {
15
+ if ( typeof schema !== 'function' ) {
16
+ throw new Error ( 'Cannot use a non-callable schema with `useUniformValue`' ) ;
17
+ }
18
+
19
+ return schema ( ) as d . Infer < T > ;
20
+ }
21
+
12
22
export function useUniformValue <
13
23
TSchema extends d . AnyWgslData ,
14
24
TValue extends d . Infer < TSchema > ,
@@ -40,17 +50,15 @@ export function useUniformValue<
40
50
41
51
// biome-ignore lint/correctness/useExhaustiveDependencies: This value needs to be stable
42
52
const uniformValue = useMemo ( ( ) => {
43
- let currentValue = initialValue ;
53
+ let currentValue = initialValue ?? initialValueFromSchema ( schema ) as TValue ;
44
54
return {
45
55
schema,
46
56
get value ( ) {
47
57
return currentValue ;
48
58
} ,
49
- set value ( newValue : TValue | undefined ) {
59
+ set value ( newValue : TValue ) {
50
60
currentValue = newValue ;
51
- if ( newValue !== undefined ) {
52
- uniformBuffer . write ( newValue ) ;
53
- }
61
+ uniformBuffer . write ( newValue ) ;
54
62
} ,
55
63
get $ ( ) {
56
64
return uniformBuffer . $ ;
0 commit comments