Skip to content

Commit 47f0734

Browse files
committed
Example updates v2
1 parent 0219c65 commit 47f0734

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

apps/typegpu-docs/src/examples/react/shifting-gradient/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { d, common, std } from 'typegpu';
22
import { useConfigureContext, useFrame, useRoot, useUniformValue } from '@typegpu/react';
3-
import { hexToRgb, oklabToRgb, hexToOklab, rgbToOklab } from '@typegpu/color';
3+
import { oklabToRgb, hexToOklab } from '@typegpu/color';
44

55
function App() {
66
const root = useRoot();
@@ -13,11 +13,11 @@ function App() {
1313
fragment: ({ uv }) => {
1414
'use gpu';
1515
const fromStart = hexToOklab('#ff0000');
16-
const fromEnd = rgbToOklab(hexToRgb('#0000ff'));
16+
const fromEnd = hexToOklab('#0000ff');
1717
const from = std.mix(fromStart, fromEnd, std.sin(time.$) * 0.5 + 0.5);
1818

19-
const toStart = rgbToOklab(hexToRgb('#00ff00'));
20-
const toEnd = rgbToOklab(hexToRgb('#ff00ff'));
19+
const toStart = hexToOklab('#00ff00');
20+
const toEnd = hexToOklab('#ff00ff');
2121
const to = std.mix(toStart, toEnd, std.cos(time.$ * 1.5) * 0.5 + 0.5);
2222

2323
const mixed = std.mix(from, to, (uv.x * 2 - 1) * 0.5 + std.sin(time.$ + uv.y * 3) * 0.5);
-38.1 KB
Loading

apps/typegpu-docs/src/examples/react/spinning-triangle/index.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { d, std } from 'typegpu';
1+
import tgpu, { d, std } from 'typegpu';
2+
import { hexToOklab, oklabToRgb } from '@typegpu/color';
23
import { useConfigureContext, useFrame, useRoot, useUniformValue } from '@typegpu/react';
34
import { useMemo } from 'react';
45

@@ -12,6 +13,20 @@ function rotate(v: d.v2f, angle: number): d.v2f {
1213
return pos;
1314
}
1415

16+
const purple = hexToOklab('#c463ff');
17+
const blue = hexToOklab('#22ffff');
18+
19+
function getGradientColor(ratio: number) {
20+
'use gpu';
21+
return oklabToRgb(std.mix(purple, blue, ratio));
22+
}
23+
24+
const positions = tgpu.const(d.arrayOf(d.vec2f, 3), [
25+
d.vec2f(0, 1),
26+
rotate(d.vec2f(0, 1), (Math.PI * 2) / 3),
27+
rotate(d.vec2f(0, 1), (Math.PI * 4) / 3),
28+
]);
29+
1530
function App() {
1631
const root = useRoot();
1732
const time = useUniformValue(d.f32, 0);
@@ -21,13 +36,20 @@ function App() {
2136
root.createRenderPipeline({
2237
vertex: ({ $vertexIndex: vid }) => {
2338
'use gpu';
24-
const positions = [d.vec2f(0, 1.1), d.vec2f(-1, -0.7), d.vec2f(1, -0.7)];
25-
const rotated = rotate(positions[vid], time.$);
26-
return { $position: d.vec4f(rotated * 0.7, 0, 1) };
39+
const local = positions.$[vid];
40+
const rotated = rotate(local, time.$ * 0.1);
41+
return {
42+
$position: d.vec4f(rotated * 0.7, 0, 1),
43+
dist0: std.length(local - positions.$[0]),
44+
dist1: std.length(local - positions.$[1]),
45+
dist2: std.length(local - positions.$[2]),
46+
};
2747
},
28-
fragment: () => {
48+
fragment: ({ dist0, dist1, dist2 }) => {
2949
'use gpu';
30-
return d.vec4f(1, 0, 0, 1);
50+
const dist = 1 / (1.4 - std.min(dist0, dist1, dist2));
51+
const albedo = getGradientColor(std.fract(dist * 2 - time.$) * 2 - 1 + std.cos(time.$));
52+
return d.vec4f(albedo, 1);
3153
},
3254
}),
3355
[root, time],
230 KB
Loading

0 commit comments

Comments
 (0)