Skip to content

Commit 75a234e

Browse files
authored
Merge branch 'main' into fix/raw-code-snippet-invalid-gpu-value
2 parents a26488b + bcde57c commit 75a234e

File tree

1 file changed

+15
-6
lines changed
  • apps/typegpu-docs/src/content/docs/fundamentals

1 file changed

+15
-6
lines changed

apps/typegpu-docs/src/content/docs/fundamentals/utils.mdx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,26 @@ This can be used to precompute and inject a value into the final shader code.
137137
import tgpu from 'typegpu';
138138
import * as d from 'typegpu/data';
139139

140-
const hsvToRgb = tgpu.fn([d.f32, d.f32, d.f32], d.vec3f)``;
141-
142140
// ---cut---
143-
const injectRand01 = tgpu['~unstable']
144-
.comptime(() => Math.random());
141+
const color = tgpu['~unstable']
142+
.comptime((int: number) => {
143+
const r = (int >> 16) & 0xff;
144+
const g = (int >> 8) & 0xff;
145+
const b = int & 0xff;
146+
return d.vec3f(r / 255, g / 255, b / 255);
147+
});
145148

146-
const getColor = tgpu.fn([d.vec3f])((diffuse) => {
149+
const material = tgpu.fn([d.vec3f], d.vec3f)((diffuse) => {
147150
'use gpu';
148-
const albedo = hsvToRgb(injectRand01(), 1, 0.5);
151+
const albedo = color(0xff00ff);
149152
return albedo.mul(diffuse);
150153
});
154+
155+
const shader = tgpu.resolve([material]);
156+
// fn material(diffuse: vec3f) -> vec3f {
157+
// var albedo = vec3f(1, 0, 1);
158+
// return (albedo * diffuse);
159+
// }
151160
```
152161

153162
Note how the function passed into `comptime` doesn't have to be marked with

0 commit comments

Comments
 (0)