@@ -5,10 +5,20 @@ import type { ValidateUniformSchema } from 'typegpu';
55
66interface UniformValue < TSchema , TValue extends d . Infer < TSchema > > {
77 schema : TSchema ;
8- value : TValue | undefined ;
8+ value : TValue ;
99 readonly $ : d . InferGPU < TSchema > ;
1010}
1111
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+
1222export function useUniformValue <
1323 TSchema extends d . AnyWgslData ,
1424 TValue extends d . Infer < TSchema > ,
@@ -40,17 +50,15 @@ export function useUniformValue<
4050
4151 // biome-ignore lint/correctness/useExhaustiveDependencies: This value needs to be stable
4252 const uniformValue = useMemo ( ( ) => {
43- let currentValue = initialValue ;
53+ let currentValue = initialValue ?? initialValueFromSchema ( schema ) as TValue ;
4454 return {
4555 schema,
4656 get value ( ) {
4757 return currentValue ;
4858 } ,
49- set value ( newValue : TValue | undefined ) {
59+ set value ( newValue : TValue ) {
5060 currentValue = newValue ;
51- if ( newValue !== undefined ) {
52- uniformBuffer . write ( newValue ) ;
53- }
61+ uniformBuffer . write ( newValue ) ;
5462 } ,
5563 get $ ( ) {
5664 return uniformBuffer . $ ;
0 commit comments