Skip to content

Commit

Permalink
Restructure Shader Composer (#363)
Browse files Browse the repository at this point in the history
- Restructure into indivdual scoped packages
- Remove Three.js dependencies
- QoL improvements
  • Loading branch information
hmans authored Nov 30, 2022
1 parent 2d1ca19 commit d504e35
Show file tree
Hide file tree
Showing 137 changed files with 1,181 additions and 506 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"fixed": [],
"linked": [
["vfx-composer", "vfx-composer-r3f"],
["shader-composer", "shader-composer-r3f"],
["shader-composer", "shader-composer-*", "@shader-composer/*"],
["material-composer", "material-composer-r3f"]
],
"access": "public",
Expand Down
14 changes: 14 additions & 0 deletions .changeset/curvy-llamas-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"shader-composer": minor
"@shader-composer/core": minor
---

**Breaking:** The casting helpers (eg. `$vec3()`, `$mat3()` etc.) have been renamed to `vec3()`, `mat3()` etc., and have had their signature changed. Where the old implementations were able to take any number of arguments, the new helpers will only ever take _a single argument_. (If you want to cast multiple arguments, you can pass an array.)

```js
/* Before: */
$vec3(1, 2, 3)

/* After: */
vec3([1, 2, 3])
```
23 changes: 23 additions & 0 deletions .changeset/olive-coats-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"shader-composer": minor
"@shader-composer/core": minor
"@shader-composer/noise": minor
"@shader-composer/postprocessing": minor
"@shader-composer/r3f": minor
"@shader-composer/three": minor
"shader-composer-toybox": minor
---

**Breaking:** Shader Composer has received a new package structure! Its code is now spread across multiple smaller packages scoped within the `@shader-composer/*` organization, with the main `shader-composer` package acting as an umbrella package.

The user can now choose between either picking the scoped packages they need, or just using the umbrella package, which provides additional entrypoints for specific frameworks and libraries.

Example for working with individual scoped packages:

```js
import { compileShader } from "@shader-composer/core"
import { setupThree } from "@shader-composer/three"
import { PostProcessingEffectMaster } from "@shader-composer/postprocessing"
```

TODO: finalize examples
10 changes: 10 additions & 0 deletions .changeset/spotty-students-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"material-composer": minor
"@material-composer/patch-material": minor
"material-composer-r3f": minor
"render-composer": minor
"vfx-composer": minor
"vfx-composer-r3f": minor
---

Upgraded to the new version of Shader Composer.
14 changes: 14 additions & 0 deletions .changeset/stale-moles-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"shader-composer": minor
"@shader-composer/core": minor
"@shader-composer/r3f": minor
"@shader-composer/three": minor
---

**Breaking:** The core Shader Composer package `@shader-composer/core` no longer requires Three.js as a peer dependency, and is now ready to be used together with other frameworks (or directly with WebGL.)

Since some of the units provided within the standard library require framework-specific code to operate (like `CameraFar`, `CameraNear`, `Resolution` etc.), glue code needs to be created to use Shader Composer with other frameworks.

The Three.js glue code lives in the `@shader-composer/three` package.

TODO: example for usage
22 changes: 22 additions & 0 deletions .changeset/two-jokes-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"shader-composer": minor
"@shader-composer/core": minor
---

**New:** Arrays of specific lengths are now automatically casts to vectors and matrices; an array with 2 elements will be rendered as a `vec2`, an array with 3 elements will be rendered as a `vec3`, and so on.

```js
/* Before, and this still works: */
ScaleAndOffset(value, Vec2([0.5, 0.5]), Vec2([0.5, 0.5]))

/* But now this is also possible: */
ScaleAndOffset(value, [0.5, 0.5], [0.5, 0.5])
```

If you want to cast an array to a specific type, you can use the `vec2()`, `vec3()` etc. helpers:

```js
ScaleAndOffset(value, vec2([0.5, 0.5]), vec2([0.5, 0.5]))
```

Or wrap them in full units, like above.
5 changes: 5 additions & 0 deletions .changeset/yellow-pens-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shader-composer": minor
---

Removed Three-specific bits from `UpdateCallback`
1 change: 0 additions & 1 deletion apps/material-composer-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"react-dom": "^18.2.0",
"react-merge-refs": "^2.0.1",
"shader-composer": "0.4.9",
"shader-composer-r3f": "^0.4.5",
"shader-composer-toybox": "^0.1.3",
"three": "^0.145.0",
"timeline-composer": "^0.1.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useControls } from "leva"
import { composable, Layer, LayerProps, modules } from "material-composer-r3f"
import { useUniformUnit } from "shader-composer-r3f"
import { useUniformUnit } from "shader-composer/r3f"
import { Color } from "three"

export const ColorLayer = (props: LayerProps) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useControls } from "leva"
import { composable, Layer, modules } from "material-composer-r3f"
import { Description } from "r3f-stage"
import { useUniformUnit } from "shader-composer-r3f"
import { useUniformUnit } from "shader-composer/r3f"

export default function HelloWorld() {
const controls = useControls({ mix: { value: 0.5, min: 0, max: 1 } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Time,
VertexPosition
} from "shader-composer"
import { useUniformUnit } from "shader-composer-r3f"
import { useUniformUnit } from "shader-composer/r3f"
import { DoubleSide } from "three"
import { Lava } from "./Fireball"
import { Plasma } from "./PlasmaBall"
Expand Down
2 changes: 1 addition & 1 deletion apps/material-composer-examples/src/examples/Textures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useControls } from "leva"
import { composable, Layer, modules } from "material-composer-r3f"
import { Description } from "r3f-stage"
import { Mul, Time, UV } from "shader-composer"
import { useUniformUnit } from "shader-composer-r3f"
import { useUniformUnit } from "shader-composer/r3f"
import textureUrl from "./textures/hexgrid.jpeg"

export default function Textures() {
Expand Down
2 changes: 1 addition & 1 deletion apps/material-composer-examples/src/examples/Vanilla.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const vanillaCode = (

/* Create mesh and add it to the scene. */
const stopLoop = loop((dt) => {
shaderMeta.update(dt, camera, scene, renderer)
shaderMeta.update(dt, { camera, scene, renderer })
})

return () => {
Expand Down
3 changes: 1 addition & 2 deletions apps/shader-composer-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"r3f-stage": "^0.3.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"shader-composer": "^0.4.9",
"shader-composer-r3f": "^0.4.5",
"shader-composer": "workspace:^0.4.9",
"shader-composer-toybox": "^0.1.3",
"three": "^0.145.0"
},
Expand Down
6 changes: 3 additions & 3 deletions apps/shader-composer-examples/src/examples/Billboarding.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Description, FlatStage } from "r3f-stage"
import { ShaderMaterialMaster, VertexPosition } from "shader-composer"
import { useShader } from "shader-composer-r3f"
import { useShader } from "shader-composer/r3f"
import { Billboard } from "shader-composer-toybox"
import { Color } from "three"

Expand All @@ -21,8 +21,8 @@ export default function BillboardingExample() {
</mesh>

<Description>
Use the <strong>Billboard</strong> unit to turn a mesh (like this plane) into a
billboard that is always facing the camera.
Use the <strong>Billboard</strong> unit to turn a mesh (like this plane)
into a billboard that is always facing the camera.
</Description>
</FlatStage>
)
Expand Down
4 changes: 2 additions & 2 deletions apps/shader-composer-examples/src/examples/DiscoCube.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
Sub,
VertexPosition
} from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer-r3f"
import { PSRDNoise3D } from "shader-composer-toybox"
import { Shader, ShaderMaster, useShader } from "shader-composer/r3f"
import { PSRDNoise3D } from "shader-composer"
import { Color, Mesh } from "three"

export default function DiscoCube() {
Expand Down
2 changes: 1 addition & 1 deletion apps/shader-composer-examples/src/examples/Dissolve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ShaderMaster,
useShader,
useUniformUnit
} from "shader-composer-r3f"
} from "shader-composer/r3f"
import { Dissolve } from "shader-composer-toybox"
import { Color, DoubleSide } from "three"

Expand Down
4 changes: 2 additions & 2 deletions apps/shader-composer-examples/src/examples/Fireball.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
ShaderMaster,
useShader,
useUniformUnit
} from "shader-composer-r3f"
import { PSRDNoise3D, Turbulence3D } from "shader-composer-toybox"
} from "shader-composer/r3f"
import { PSRDNoise3D, Turbulence3D } from "shader-composer"
import textureUrl from "./textures/explosion.png"

export default function Fireball() {
Expand Down
2 changes: 1 addition & 1 deletion apps/shader-composer-examples/src/examples/Flag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Vec3,
VertexPosition
} from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer-r3f"
import { Shader, ShaderMaster, useShader } from "shader-composer/r3f"
import { DoubleSide } from "three"
import textureUrl from "./textures/shader-composer-logo.jpg"

Expand Down
5 changes: 3 additions & 2 deletions apps/shader-composer-examples/src/examples/FloatingIsland.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import {
Vec2,
Vec3
} from "shader-composer"
import { Displacement } from "shader-composer-toybox"
import { PSRDNoise2D } from "shader-composer"
import {
Shader,
ShaderMaster,
useShader,
useUniformUnit
} from "shader-composer-r3f"
import { Displacement, PSRDNoise2D } from "shader-composer-toybox"
} from "shader-composer/r3f"
import { Color, RGBADepthPacking, Vector2 } from "three"

export default function FloatingIslandExample() {
Expand Down
2 changes: 1 addition & 1 deletion apps/shader-composer-examples/src/examples/ForceField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ShaderMaster,
useShader,
useUniformUnit
} from "shader-composer-r3f"
} from "shader-composer/r3f"
import { Color } from "three"
import { useRepeatingTexture } from "./helpers"

Expand Down
2 changes: 1 addition & 1 deletion apps/shader-composer-examples/src/examples/HelloWorld.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Sin,
VertexPosition
} from "shader-composer"
import { useShader, useUniformUnit } from "shader-composer-r3f"
import { useShader, useUniformUnit } from "shader-composer/r3f"
import { Color } from "three"

export default function HelloWorld() {
Expand Down
4 changes: 2 additions & 2 deletions apps/shader-composer-examples/src/examples/Planet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
Vec3,
VertexPosition
} from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer-r3f"
import { Simplex3DNoise } from "shader-composer-toybox"
import { Simplex3DNoise } from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer/r3f"
import { Color } from "three"

const getNoise = (
Expand Down
2 changes: 1 addition & 1 deletion apps/shader-composer-examples/src/examples/Rotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
VertexNormal,
VertexPosition
} from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer-r3f"
import { Shader, ShaderMaster, useShader } from "shader-composer/r3f"
import { Vector3 } from "three"

export default function MatrixTransformations() {
Expand Down
4 changes: 2 additions & 2 deletions apps/shader-composer-examples/src/examples/StylizedWater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
ShaderMaster,
useShader,
useUniformUnit
} from "shader-composer-r3f"
import { PSRDNoise2D, PSRDNoise3D } from "shader-composer-toybox"
} from "shader-composer/r3f"
import { PSRDNoise2D, PSRDNoise3D } from "shader-composer"
import { Color } from "three"

export default function StylizedWater() {
Expand Down
7 changes: 3 additions & 4 deletions apps/shader-composer-examples/src/examples/Textures.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Shader, ShaderMaster, useShader } from "shader-composer/r3f"
import {
$,
$vec2,
Add,
Fresnel,
Mul,
Expand All @@ -10,21 +10,20 @@ import {
UniformUnit,
UV
} from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer-r3f"
import { Color } from "three"
import { useRepeatingTexture } from "./helpers"

export default function Textures() {
const texture = useRepeatingTexture("/textures/hexgrid.jpg")

const shader = useShader(() => {
const offset = $vec2(Mul(Time(), 0.05), 0)
const offset = [Mul(Time(), 0.05), 0]

/* Create a texture sampler */
const sampler2D = UniformUnit("sampler2D", texture)

/* Get the texture information for the current fragment */
const tex2d = Texture2D(sampler2D, ScaleAndOffset(UV, $vec2(2, 1), offset))
const tex2d = Texture2D(sampler2D, ScaleAndOffset(UV, [2, 1], offset))

/* Define a color to tint the texture with */
const color = new Color("hotpink")
Expand Down
20 changes: 11 additions & 9 deletions apps/shader-composer-examples/src/examples/Water.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Environment } from "@react-three/drei"
import { pipe } from "fp-ts/function"
import { GlobalTime, Input, Int, Mul, Remap, Vec2 } from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer-r3f"
import { Displacement, FBMNoise, GerstnerWave } from "shader-composer-toybox"
import { add } from "shader-composer/fun"
import { Add, GlobalTime, Input, Int, Mul, Remap, Vec2 } from "shader-composer"
import { Displacement } from "shader-composer-toybox"
import { FBMNoise, GerstnerWave } from "shader-composer"
import { Shader, ShaderMaster, useShader } from "shader-composer/r3f"
import { Color, DoubleSide } from "three"

const NormalizeNoise = (v: Input<"float">) => Remap(v, -1, 1, 0, 1)
Expand Down Expand Up @@ -38,11 +38,13 @@ function Water() {

return pipe(
v,
add(Mul(GerstnerWave(xy, Vec2([1, 1]), 0.5, 20.0, time), 0.8)),
add(Mul(GerstnerWave(xy, Vec2([0.2, 1]), 0.2, 10, time), 0.8)),
add(Mul(GerstnerWave(xy, Vec2([0, -1]), 0.2, 5, time), 0.5)),
add(Mul(GerstnerWave(xy, Vec2([1, 1]), 0.2, 8, time), 0.3))
// add(Mul(vec3(0, 0.005, 0), fbm))
(v) =>
Add(v, Mul(GerstnerWave(xy, Vec2([1, 1]), 0.5, 20.0, time), 0.8)),
(v) =>
Add(v, Mul(GerstnerWave(xy, Vec2([0.2, 1]), 0.2, 10, time), 0.8)),
(v) => Add(v, Mul(GerstnerWave(xy, Vec2([0, -1]), 0.2, 5, time), 0.5)),
(v) => Add(v, Mul(GerstnerWave(xy, Vec2([1, 1]), 0.2, 8, time), 0.3))
// (v) => Add(v, Mul(vec3(0, 0.005, 0), fbm))
)
})

Expand Down
15 changes: 11 additions & 4 deletions apps/shader-composer-examples/src/examples/useRenderPass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useFrame, useThree } from "@react-three/fiber"
import { useLayoutEffect, useMemo, useRef } from "react"
import { DepthTexture, WebGLRenderTarget } from "three"
import { useUniformUnit } from "shader-composer-r3f"
import { useUniformUnit } from "shader-composer/r3f"

export type RenderPassOptions = {
resolution?: number
Expand All @@ -20,14 +20,21 @@ export const useRenderPass = ({

const renderTargets = useMemo(
() => [
new WebGLRenderTarget(256, 256, { depthTexture: new DepthTexture(256, 256) }),
new WebGLRenderTarget(256, 256, { depthTexture: new DepthTexture(256, 256) })
new WebGLRenderTarget(256, 256, {
depthTexture: new DepthTexture(256, 256)
}),
new WebGLRenderTarget(256, 256, {
depthTexture: new DepthTexture(256, 256)
})
],
[]
)

const texture = useUniformUnit("sampler2D", renderTargets[0].texture)
const depthTexture = useUniformUnit("sampler2D", renderTargets[0].depthTexture)
const depthTexture = useUniformUnit(
"sampler2D",
renderTargets[0].depthTexture
)
const cursor = useRef(0)

useLayoutEffect(() => {
Expand Down
Loading

0 comments on commit d504e35

Please sign in to comment.