-
-
Notifications
You must be signed in to change notification settings - Fork 22
feat(@typegpu/react): Implement useUniformValue hook #1694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
20391bd
abe8946
4264d36
ddd5e91
470e02e
10099cd
b0ad047
99ef5b9
42ed770
5f43bf2
012e609
29a2149
b806954
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<div id='example-app'></div> | ||
<div id="example-app"></div> |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,62 @@ | ||||||||||||||||||||||||||||||||||
import type * as d from 'typegpu/data'; | ||||||||||||||||||||||||||||||||||
import { useRoot } from './root-context.tsx'; | ||||||||||||||||||||||||||||||||||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||||||||||||||||||||||||||||||||||
import type { ValidateUniformSchema } from 'typegpu'; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
interface UniformValue<TSchema, TValue extends d.Infer<TSchema>> { | ||||||||||||||||||||||||||||||||||
schema: TSchema; | ||||||||||||||||||||||||||||||||||
value: TValue; | ||||||||||||||||||||||||||||||||||
value: TValue | undefined; | ||||||||||||||||||||||||||||||||||
readonly $: d.InferGPU<TSchema>; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
export function useUniformValue<TSchema, TValue extends d.Infer<TSchema>>( | ||||||||||||||||||||||||||||||||||
schema: d.AnyWgslData, | ||||||||||||||||||||||||||||||||||
export function useUniformValue< | ||||||||||||||||||||||||||||||||||
TSchema extends d.AnyWgslData, | ||||||||||||||||||||||||||||||||||
TValue extends d.Infer<TSchema>, | ||||||||||||||||||||||||||||||||||
>( | ||||||||||||||||||||||||||||||||||
schema: ValidateUniformSchema<TSchema>, | ||||||||||||||||||||||||||||||||||
initialValue?: TValue | undefined, | ||||||||||||||||||||||||||||||||||
): UniformValue<TSchema, TValue> { | ||||||||||||||||||||||||||||||||||
// TODO: Implement | ||||||||||||||||||||||||||||||||||
const root = useRoot(); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const [uniformBuffer] = useState(() => { | ||||||||||||||||||||||||||||||||||
return root.createUniform( | ||||||||||||||||||||||||||||||||||
schema, | ||||||||||||||||||||||||||||||||||
initialValue, | ||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const cleanupRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||
if (cleanupRef.current) { | ||||||||||||||||||||||||||||||||||
clearTimeout(cleanupRef.current); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return () => { | ||||||||||||||||||||||||||||||||||
cleanupRef.current = setTimeout(() => { | ||||||||||||||||||||||||||||||||||
uniformBuffer.buffer.destroy(); | ||||||||||||||||||||||||||||||||||
}, 200); | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
}, [uniformBuffer]); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// biome-ignore lint/correctness/useExhaustiveDependencies: This value needs to be stable | ||||||||||||||||||||||||||||||||||
const uniformValue = useMemo(() => { | ||||||||||||||||||||||||||||||||||
let currentValue = initialValue; | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Schemas should generally be callable, in which case they return an initial value that matches that schema.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing that out! However, with current implementation: export function useUniformValue<
TSchema extends d.AnyWgslData,
TValue extends d.Infer<TSchema>,
>(
schema: ValidateUniformSchema<TSchema>,
initialValue?: TValue | undefined,
): UniformValue<TSchema, TValue> { schema is not callable:
I'm wondering how can I alter this to simplify the logic. |
||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||
schema, | ||||||||||||||||||||||||||||||||||
get value() { | ||||||||||||||||||||||||||||||||||
return currentValue; | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
set value(newValue: TValue | undefined) { | ||||||||||||||||||||||||||||||||||
currentValue = newValue; | ||||||||||||||||||||||||||||||||||
if (newValue !== undefined) { | ||||||||||||||||||||||||||||||||||
uniformBuffer.write(newValue); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
Comment on lines
+46
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the previous comment, we don't have to worry about undefined.
Suggested change
|
||||||||||||||||||||||||||||||||||
get $() { | ||||||||||||||||||||||||||||||||||
return uniformBuffer.$; | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
}, []); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return uniformValue as UniformValue<TSchema, TValue>; | ||||||||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.