|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { useEffect, useRef } from "react"; |
| 3 | + |
| 4 | +import type { SurfaceFnDef } from "../../src/helpers/surface-equations"; |
| 5 | +import { |
| 6 | + CONCAVE, |
| 7 | + CONVEX, |
| 8 | + CONVEX_CIRCLE, |
| 9 | + LIP, |
| 10 | +} from "../../src/helpers/surface-equations"; |
| 11 | +import { |
| 12 | + calculateDisplacementMap, |
| 13 | + calculateDisplacementMapRadius, |
| 14 | +} from "../../src/maps/displacement-map"; |
| 15 | + |
| 16 | +type Props = { |
| 17 | + radius: number; |
| 18 | + glassThickness: number; |
| 19 | + bezelWidth: number; |
| 20 | + refractiveIndex: number; |
| 21 | + bezelHeightFn: SurfaceFnDef; |
| 22 | + pixelRatio: number; |
| 23 | +}; |
| 24 | + |
| 25 | +const DisplacementMapVis = ({ |
| 26 | + radius, |
| 27 | + glassThickness, |
| 28 | + bezelWidth, |
| 29 | + refractiveIndex, |
| 30 | + bezelHeightFn, |
| 31 | + pixelRatio, |
| 32 | +}: Props) => { |
| 33 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + const canvas = canvasRef.current; |
| 37 | + if (!canvas) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const cornerWidth = Math.max(radius, bezelWidth); |
| 42 | + const imageSide = cornerWidth * 2 + 1; |
| 43 | + |
| 44 | + const map = calculateDisplacementMapRadius( |
| 45 | + glassThickness, |
| 46 | + bezelWidth, |
| 47 | + bezelHeightFn, |
| 48 | + refractiveIndex, |
| 49 | + ); |
| 50 | + const maximumDisplacement = Math.max(...map.map(Math.abs)); |
| 51 | + |
| 52 | + const imageData = calculateDisplacementMap({ |
| 53 | + width: imageSide, |
| 54 | + height: imageSide, |
| 55 | + radius, |
| 56 | + bezelWidth, |
| 57 | + precomputedDisplacementMap: map, |
| 58 | + maximumDisplacement, |
| 59 | + pixelRatio, |
| 60 | + }); |
| 61 | + |
| 62 | + canvas.width = imageData.width; |
| 63 | + canvas.height = imageData.height; |
| 64 | + const ctx = canvas.getContext("2d")!; |
| 65 | + ctx.putImageData(imageData, 0, 0); |
| 66 | + }, [ |
| 67 | + radius, |
| 68 | + glassThickness, |
| 69 | + bezelWidth, |
| 70 | + refractiveIndex, |
| 71 | + bezelHeightFn, |
| 72 | + pixelRatio, |
| 73 | + ]); |
| 74 | + |
| 75 | + return ( |
| 76 | + <div style={{ padding: 32, background: "#1a1a2e" }}> |
| 77 | + <p style={{ color: "#ccc", marginBottom: 8, fontFamily: "monospace" }}> |
| 78 | + Red = X displacement, Green = Y displacement (128 = neutral) |
| 79 | + </p> |
| 80 | + <canvas |
| 81 | + ref={canvasRef} |
| 82 | + style={{ |
| 83 | + imageRendering: "pixelated", |
| 84 | + width: 500, |
| 85 | + height: 500, |
| 86 | + border: "1px solid #444", |
| 87 | + }} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const meta = { |
| 94 | + title: "Internals/Displacement Map", |
| 95 | + component: DisplacementMapVis, |
| 96 | + argTypes: { |
| 97 | + radius: { control: { type: "range", min: 0, max: 100, step: 1 } }, |
| 98 | + glassThickness: { control: { type: "range", min: 0, max: 300, step: 1 } }, |
| 99 | + bezelWidth: { control: { type: "range", min: 0, max: 100, step: 1 } }, |
| 100 | + refractiveIndex: { |
| 101 | + control: { type: "range", min: 1, max: 3, step: 0.01 }, |
| 102 | + }, |
| 103 | + pixelRatio: { control: { type: "range", min: 1, max: 12, step: 1 } }, |
| 104 | + bezelHeightFn: { table: { disable: true } }, |
| 105 | + }, |
| 106 | + args: { |
| 107 | + radius: 20, |
| 108 | + glassThickness: 70, |
| 109 | + bezelWidth: 30, |
| 110 | + refractiveIndex: 1.5, |
| 111 | + pixelRatio: 6, |
| 112 | + bezelHeightFn: CONVEX, |
| 113 | + }, |
| 114 | +} satisfies Meta<typeof DisplacementMapVis>; |
| 115 | + |
| 116 | +export default meta; |
| 117 | + |
| 118 | +type Story = StoryObj<typeof meta>; |
| 119 | + |
| 120 | +export const Convex: Story = { args: { bezelHeightFn: CONVEX } }; |
| 121 | +export const ConvexCircle: Story = { args: { bezelHeightFn: CONVEX_CIRCLE } }; |
| 122 | +export const Concave: Story = { args: { bezelHeightFn: CONCAVE } }; |
| 123 | +export const Lip: Story = { args: { bezelHeightFn: LIP } }; |
0 commit comments