Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sdk/r3f/src/input/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export interface Part {
}

const hex = (c: THREE.Color) => "#" + c.getHexString();
/** three draws an opacity outside [0, 1] clamped (an easing that overshoots gives 1.03); raea's materials refuse it */
const unit = (v: number) => Math.min(1, Math.max(0, v));
const side = (s: THREE.Side) => (s === THREE.BackSide ? "back" : s === THREE.DoubleSide ? "double" : undefined);

/** the props every surface shares: colour, opacity and blending, side, fog */
function drawn(m: THREE.Material & { color?: THREE.Color; map?: THREE.Texture | null }, p: Props) {
if (m.color) p.color = hex(m.color);
if (m.opacity < 1 || m.transparent) {
p.opacity = m.opacity;
p.opacity = unit(m.opacity);
p.transparent = true;
}
if (m.blending === THREE.AdditiveBlending) p.alphaMode = "add";
Expand Down Expand Up @@ -177,7 +179,7 @@ export function materialOf(m: THREE.Material, vertexColors: boolean, at?: NodeAt
return { type: "MeshBasicMaterial", props: { color: "#808080" } };
case "ShadowMaterial": {
const s = m as THREE.ShadowMaterial;
return { type: "MeshBasicMaterial", props: { color: hex(s.color), opacity: s.opacity, transparent: true }, catchShadow: true } as Part;
return { type: "MeshBasicMaterial", props: { color: hex(s.color), opacity: unit(s.opacity), transparent: true }, catchShadow: true } as Part;
}
default: {
// a TSL node material (three/webgpu): one with no node of its own is its classic material; one with a graph is
Expand Down
9 changes: 9 additions & 0 deletions sdk/r3f/test/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ describe("the scene graph", () => {
expect(meshes.map((m) => (m.children![0].props.indices as number[]).length)).toEqual([6, 6, 6, 6, 6, 6]);
end(r.key);
});
test("an opacity past 1 or below 0 (an overshooting easing) is clamped, as three draws it", () => {
const r = partsOf(() => <>
<mesh><boxGeometry /><meshBasicMaterial transparent opacity={1.03} /></mesh>
<mesh><boxGeometry /><meshStandardMaterial transparent opacity={-0.02} /></mesh>
</>);
const ops = flat(r.parts).filter((p) => p.type === "Mesh").map((m) => m.children!.find((c) => "opacity" in c.props)!.props.opacity);
expect(ops).toEqual([1, 0]);
end(r.key);
});
});

describe("textures", () => {
Expand Down
Loading