Skip to content

Commit

Permalink
Reuse the texture2D result for color and alpha (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans authored Nov 19, 2022
1 parent 1d27e73 commit f8785d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-clocks-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shader-composer": patch
---

**Fixed:** `Texture2D().color` and `Texture2D().alpha` now reuse the sample `texture2D` call, instead of re-sampling the texture.
22 changes: 13 additions & 9 deletions packages/shader-composer/src/stdlib/textures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { $ } from "../expressions"
import { Input, Unit } from "../units"
import { Float, Vec3, Vec4 } from "./values"

export const Texture2D = (sampler2D: Unit<"sampler2D">, xy: Input<"vec2">) => ({
...Vec4($`texture2D(${sampler2D}, ${xy})`, {
name: "Texture2D"
}),
export const Texture2D = (sampler2D: Unit<"sampler2D">, xy: Input<"vec2">) => {
const texture2D = Vec4($`texture2D(${sampler2D}, ${xy})`, {
name: "Texture2D"
})

/** The color value sampled from the texture. */
color: Vec3($`texture2D(${sampler2D}, ${xy}).rgb`, { name: "Texture2D Color" }),
return {
...texture2D,

/** The alpha value sampled from the texture. */
alpha: Float($`texture2D(${sampler2D}, ${xy}).a`, { name: "Texture2D Alpha" })
})
/** The color value sampled from the texture. */
color: Vec3($`${texture2D}.rgb`, { name: "Texture2D Color" }),

/** The alpha value sampled from the texture. */
alpha: Float($`${texture2D}.a`, { name: "Texture2D Alpha" })
}
}

0 comments on commit f8785d4

Please sign in to comment.