Skip to content

Commit

Permalink
fix: add swizzle and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pavophilip committed Dec 24, 2023
1 parent a5be10e commit 6c34351
Show file tree
Hide file tree
Showing 21 changed files with 1,241 additions and 282 deletions.
1,101 changes: 993 additions & 108 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@fontsource/inter": "^5.0.16",
"@mui/joy": "^5.0.0-beta.19",
"@react-shader-fiber/renderer": "^1.0.12",
"@react-shader-fiber/stdlib": "^1.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-shader-fiber": "^1.0.10"
"react-shader-fiber": "^1.1.0"
},
"devDependencies": {
"@types/react": "^18.2.43",
Expand Down
42 changes: 0 additions & 42 deletions packages/demo/src/App.css

This file was deleted.

52 changes: 13 additions & 39 deletions packages/demo/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
import { useRef } from "react";
import "./App.css";
import circle from "../lygia/draw/circle.glsl";
import { FragmentShader } from "react-shader-fiber";
import {
Assign,
Call,
Float,
Output,
Input,
Main,
Vec4,
} from "@react-shader-fiber/stdlib";
import Circle from "./examples/Circle.jsx";
import { Box, Sheet } from "@mui/joy";

function App() {
const fragColorRef = useRef();
const vCoordsRef = useRef();

return (
<div>
<FragmentShader prelude={[circle]}>
<Output type={"vec4"} id={"fragColor"} ref={fragColorRef} />

<Input ref={vCoordsRef} type={"vec2"} id={"vCoords"} />

<Main>
<Assign to={fragColorRef}>
<Vec4
x={0}
y={0}
z={
<Call fn={"circle"} type={"float"}>
{vCoordsRef}
<Float value={0.5} />
</Call>
}
w={1}
/>
</Assign>
</Main>
</FragmentShader>
</div>
<Box
display={"grid"}
sx={{
gridTemplateColumns: "1fr 1fr 1fr",
}}
>
<Circle />
<Circle />
<Circle />
<Circle />
</Box>
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/demo/src/assets/react.svg

This file was deleted.

43 changes: 43 additions & 0 deletions packages/demo/src/examples/Circle.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FragmentShader } from "react-shader-fiber";
import circle from "../../lygia/draw/circle.glsl";
import {
Assign,
Call,
Float,
Input,
Main,
Output,
Vec4,
} from "@react-shader-fiber/stdlib";
import { useRef } from "react";

const Circle = () => {
const fragColorRef = useRef();
const vCoordsRef = useRef();

return (
<FragmentShader prelude={[circle]}>
<Output type={"vec4"} id={"fragColor"} ref={fragColorRef} />

<Input ref={vCoordsRef} type={"vec2"} id={"vCoords"} />

<Main>
<Assign to={fragColorRef}>
<Vec4
x={0}
y={0}
z={
<Call fn={"circle"} type={"float"}>
{vCoordsRef}
<Float value={0.5} />
</Call>
}
w={1}
/>
</Assign>
</Main>
</FragmentShader>
);
};

export default Circle;
25 changes: 25 additions & 0 deletions packages/demo/src/examples/Gradient.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FragmentShader } from "react-shader-fiber";
import circle from "../../lygia/draw/circle.glsl";
import { Assign, Input, Main, Output, Vec4 } from "@react-shader-fiber/stdlib";
import { useRef } from "react";

const Gradient = () => {
const fragColorRef = useRef();
const vCoordsRef = useRef();

return (
<FragmentShader prelude={[circle]}>
<Output type={"vec4"} id={"fragColor"} ref={fragColorRef} />

<Input ref={vCoordsRef} type={"vec2"} id={"vCoords"} />

<Main>
<Assign to={fragColorRef}>
<Vec4 x={0} y={0} z={0} w={1} />
</Assign>
</Main>
</FragmentShader>
);
};

export default Gradient;
68 changes: 0 additions & 68 deletions packages/demo/src/index.css

This file was deleted.

18 changes: 11 additions & 7 deletions packages/demo/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "@fontsource/inter";
import { CssBaseline, CssVarsProvider } from "@mui/joy";

ReactDOM.createRoot(document.getElementById('root')).render(
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<App />
<CssVarsProvider>
<CssBaseline />
<App />
</CssVarsProvider>
</React.StrictMode>,
)
);
14 changes: 0 additions & 14 deletions packages/stdlib/src/components/glsl/X.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions packages/stdlib/src/components/glsl/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import FragColor from "./FragColor.tsx";
import VCoords from "./VCoords.tsx";
import X from "./X.tsx";

export { FragColor, VCoords, X };
export { FragColor, VCoords };
1 change: 1 addition & 0 deletions packages/stdlib/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./builtin/math/index.ts";
export * from "./sym/index.ts";
export * from "./lit/index.ts";
export * from "./function/index.ts";
export * from "./swizzle/index.ts";
13 changes: 13 additions & 0 deletions packages/stdlib/src/components/swizzle/W.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Base from "../glsl/Base.tsx";
import { FC, forwardRef, PropsWithChildren, ReactElement } from "react";
import { $w } from "@thi.ng/shader-ast";
import { Vec } from "../lit/Vec.tsx";

export default forwardRef<
FC,
PropsWithChildren<{
of: Vec | ReactElement[] | ReactElement;
}>
>(function W({ of }, ref) {
return <Base ref={ref} gen={$w} args={[of]} />;
});
14 changes: 14 additions & 0 deletions packages/stdlib/src/components/swizzle/X.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Base from "../glsl/Base.tsx";
import { FC, forwardRef, PropsWithChildren, ReactElement } from "react";
import { $x } from "@thi.ng/shader-ast";
import { Vec } from "../lit/Vec.tsx";
import normalizeVectorValue from "../../utils/normalizeVectorValue.ts";

export default forwardRef<
FC,
PropsWithChildren<{
of: Vec | ReactElement[] | ReactElement;
}>
>(function X({ of }, ref) {
return <Base ref={ref} gen={$x} args={[normalizeVectorValue(of)]} />;
});
15 changes: 15 additions & 0 deletions packages/stdlib/src/components/swizzle/XY.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Base from "../glsl/Base.tsx";
import { FC, forwardRef, PropsWithChildren, ReactElement } from "react";
import { $xy } from "@thi.ng/shader-ast";
import { Vec2 } from "../lit/Vec2.tsx";
import { Vec3 } from "../lit/Vec3.tsx";
import { Vec4 } from "../lit/Vec4.tsx";

export default forwardRef<
FC,
PropsWithChildren<{
of: Vec2 | Vec3 | Vec4 | ReactElement[] | ReactElement;
}>
>(function XY({ of }, ref) {
return <Base ref={ref} gen={$xy} args={[of]} />;
});
14 changes: 14 additions & 0 deletions packages/stdlib/src/components/swizzle/XYZ.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Base from "../glsl/Base.tsx";
import { FC, forwardRef, PropsWithChildren, ReactElement } from "react";
import { $xyz } from "@thi.ng/shader-ast";
import { Vec3 } from "../lit/Vec3.tsx";
import { Vec4 } from "../lit/Vec4.tsx";

export default forwardRef<
FC,
PropsWithChildren<{
of: Vec3 | Vec4 | ReactElement[] | ReactElement;
}>
>(function XYZ({ of }, ref) {
return <Base ref={ref} gen={$xyz} args={[of]} />;
});
13 changes: 13 additions & 0 deletions packages/stdlib/src/components/swizzle/Y.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Base from "../glsl/Base.tsx";
import { FC, forwardRef, PropsWithChildren, ReactElement } from "react";
import { $y } from "@thi.ng/shader-ast";
import { Vec } from "../lit/Vec.tsx";

export default forwardRef<
FC,
PropsWithChildren<{
of: Vec | ReactElement[] | ReactElement;
}>
>(function Y({ of }, ref) {
return <Base ref={ref} gen={$y} args={[of]} />;
});
14 changes: 14 additions & 0 deletions packages/stdlib/src/components/swizzle/Z.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Base from "../glsl/Base.tsx";
import { FC, forwardRef, PropsWithChildren, ReactElement } from "react";
import { $z } from "@thi.ng/shader-ast";
import { Vec4 } from "../lit/Vec4.tsx";
import { Vec3 } from "../lit/Vec3.tsx";

export default forwardRef<
FC,
PropsWithChildren<{
of: Vec3 | Vec4 | ReactElement | ReactElement[];
}>
>(function Z({ of }, ref) {
return <Base ref={ref} gen={$z} args={[of]} />;
});
Loading

0 comments on commit 6c34351

Please sign in to comment.