Skip to content

Commit

Permalink
chore: enhance naming (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciNyan committed Apr 29, 2024
1 parent 3093ee0 commit 5082a0f
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 45 deletions.
6 changes: 3 additions & 3 deletions packages/pixel-profile/src/renderer/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Coordinates = [number, number]
export type PixelCoords = [number, number]
export type RGBA = [number, number, number, number]
export type Texture2D = (coords: Coordinates) => RGBA
export type FragShader = (uv: Coordinates, texture2D: Texture2D) => RGBA
export type Texture = (coords: PixelCoords) => RGBA
export type FragShader = (coords: PixelCoords, texture: Texture) => RGBA

export function coordsToIndex(x: number, y: number, width: number): number {
return (y * width + x) * 4
Expand Down
2 changes: 1 addition & 1 deletion packages/pixel-profile/src/renderer/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Coordinates, FragShader, RGBA, Texture2D } from './common'
export type { FragShader, PixelCoords, RGBA, Texture } from './common'
export { coordsToIndex } from './common'
export { render } from './render'
export { TEXTURE_FILTER } from './texture-filter'
6 changes: 3 additions & 3 deletions packages/pixel-profile/src/renderer/render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clamp } from '../utils'
import { Coordinates, coordsToIndex, FragShader, RGBA } from './common'
import { coordsToIndex, FragShader, PixelCoords, RGBA } from './common'
import { TEXTURE_FILTER, textureFilterGeneratorByName, TextureFilterName } from './texture-filter'

type Options = {
Expand All @@ -21,7 +21,7 @@ export function render(

const textureFilterFn = textureFilterGeneratorByName[textureFilter](pixels, width, height)

function texture2D(coords: Coordinates): RGBA {
function texture(coords: PixelCoords): RGBA {
coords[0] = clamp(coords[0], 0, maxX)
coords[1] = clamp(coords[1], 0, maxY)

Expand All @@ -30,7 +30,7 @@ export function render(

for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const rgba = fragShader([x, y], texture2D)
const rgba = fragShader([x, y], texture)
const index = coordsToIndex(x, y, width)
target[index] = rgba[0]
target[index + 1] = rgba[1]
Expand Down
6 changes: 3 additions & 3 deletions packages/pixel-profile/src/renderer/texture-filter/linear.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clamp } from '../../utils'
import { Coordinates, coordsToPixel, RGBA, Texture2D } from '../common'
import { coordsToPixel, PixelCoords, RGBA, Texture } from '../common'

export function genBiLinearFilter(pixels: Buffer, width: number, height: number): Texture2D {
export function genBiLinearFilter(pixels: Buffer, width: number, height: number): Texture {
const maxX = width - 1
const maxY = height - 1

Expand All @@ -11,7 +11,7 @@ export function genBiLinearFilter(pixels: Buffer, width: number, height: number)

return tmp1 * (1 - sy) + tmp2 * sy
}
function biLinearFilter(coords: Coordinates): RGBA {
function biLinearFilter(coords: PixelCoords): RGBA {
const x = coords[0]
const y = coords[1]
const x0 = clamp(Math.floor(x), 0, maxX)
Expand Down
6 changes: 3 additions & 3 deletions packages/pixel-profile/src/renderer/texture-filter/nearest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Coordinates, coordsToPixel, RGBA, Texture2D } from '../common'
import { coordsToPixel, PixelCoords, RGBA, Texture } from '../common'

export function genNearestNeighborFilter(pixels: Buffer, width: number): Texture2D {
function nearestNeighborFilter(coords: Coordinates): RGBA {
export function genNearestNeighborFilter(pixels: Buffer, width: number): Texture {
function nearestNeighborFilter(coords: PixelCoords): RGBA {
const nearestX = Math.round(coords[0])
const nearestY = Math.round(coords[1])

Expand Down
8 changes: 4 additions & 4 deletions packages/pixel-profile/src/shaders/border.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export function addBorder(
source,
width,
height,
(uv, texture2D) => {
(coords, texture) => {
const maxX = width - 1
const maxY = height - 1
const x = uv[0]
const y = uv[1]
const x = coords[0]
const y = coords[1]

const frameWidth = frameWidthRatio * width

const samplerColor: RGBA = texture2D(uv)
const samplerColor: RGBA = texture(coords)

const count =
Number(x < frameWidth) + Number(y < frameWidth) + Number(x > maxX - frameWidth) + Number(y > maxY - frameWidth)
Expand Down
20 changes: 10 additions & 10 deletions packages/pixel-profile/src/shaders/curve.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { type Coordinates, render } from '../renderer'
import { type PixelCoords, render } from '../renderer'
import type { Vec2 } from '../utils'
import { add2, dot2, prod2, subtract2 } from '../utils'

const margin = [0, 0]
const screenCurvature = 0.1

export function curve(source: Buffer, width: number, height: number): Buffer {
return render(source, width, height, (xy, texture2D) => {
const uv = [xy[0] / width, xy[1] / height]
return render(source, width, height, (coords, texture) => {
const uv = [coords[0] / width, coords[1] / height]

const maxX = width - 1
const maxY = height - 1

function distortCoordinates(coords: Coordinates): Vec2 {
const cc = subtract2(coords, [0.5, 0.5])
function distortCoordinates(_coords: PixelCoords): Vec2 {
const cc = subtract2(_coords, [0.5, 0.5])
const dist = dot2(cc, cc) * screenCurvature
const temp = (1 + dist) * dist
cc[0] = cc[0] * temp
cc[1] = cc[1] * temp

return add2(coords, cc)
return add2(_coords, cc)
}

const coords = distortCoordinates([uv[0], uv[1]])
const targetCoords = distortCoordinates([uv[0], uv[1]])

coords[0] = coords[0] * (margin[0] * 2 + 1) - margin[0]
coords[1] = coords[1] * (margin[1] * 2 + 1) - margin[1]
targetCoords[0] = targetCoords[0] * (margin[0] * 2 + 1) - margin[0]
targetCoords[1] = targetCoords[1] * (margin[1] * 2 + 1) - margin[1]

const vignetteCoords: Vec2 = [uv[0] * (1 - uv[1]), uv[1] * (1 - uv[0])]
const vignette = Math.pow(prod2(vignetteCoords) * 15, 0.25)

const samplerColor = texture2D([coords[0] * maxX, coords[1] * maxY])
const samplerColor = texture([targetCoords[0] * maxX, targetCoords[1] * maxY])

return [samplerColor[0] * vignette, samplerColor[1] * vignette, samplerColor[2] * vignette, 255]
})
Expand Down
5 changes: 2 additions & 3 deletions packages/pixel-profile/src/shaders/dithering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ function closestColors(hue: number): [[number, number, number], [number, number,
return [closest, secondClosest]
}

// 抖动和颜色量化
function dither(pos: [number, number], color: [number, number, number]): [number, number, number] {
const x = Math.floor(pos[0] % 8)
const y = Math.floor(pos[1] % 8)
Expand Down Expand Up @@ -359,8 +358,8 @@ function dither(pos: [number, number], color: [number, number, number]): [number
}

export function orderedBayer(source: Buffer, width: number, height: number): Buffer {
return render(source, width, height, (pixelCoords, texture2D) => {
const color = texture2D(pixelCoords)
return render(source, width, height, (pixelCoords, texture) => {
const color = texture(pixelCoords)
const ditheredColor = dither(pixelCoords, rgbToHsl(color))

return [...ditheredColor, color[3]]
Expand Down
10 changes: 5 additions & 5 deletions packages/pixel-profile/src/shaders/glow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export function glow(source: Buffer, width: number, height: number): Buffer {
source,
width,
height,
(uv, texture2D) => {
const originalColor = texture2D(uv)
(coords, texture) => {
const samplerColor = texture(coords)

let bloomColor: Vec3 = [0, 0, 0]
let n = 0

for (let i = -radius; i <= radius; i++) {
for (let j = -radius; j <= radius; j++) {
const offset: Vec2 = [i / width, j / height]
const sampledColor = texture2D(add2(uv, offset))
const sampledColor = texture(add2(coords, offset))
const luminance = dot3(sampledColor, [0.2126, 0.7152, 0.0722])
if (luminance > _threshold) {
bloomColor = add3(bloomColor, sampledColor)
Expand All @@ -31,14 +31,14 @@ export function glow(source: Buffer, width: number, height: number): Buffer {
}

if (n === 0) {
return originalColor
return samplerColor
}

bloomColor = divide3(bloomColor, n)

const _bloomIntensity = intensity * (n / Math.pow(radius * 2 + 1, 2))

const finalColor = mix3(originalColor, bloomColor, _bloomIntensity)
const finalColor = mix3(samplerColor, bloomColor, _bloomIntensity)

return [finalColor[0], finalColor[1], finalColor[2], 255]
},
Expand Down
6 changes: 3 additions & 3 deletions packages/pixel-profile/src/shaders/halftone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const mat = [
const ditherRange = 0

export function halftone(source: Buffer, width: number, height: number): Buffer {
return render(source, width, height, (pixelCoords, texture2D) => {
return render(source, width, height, (pixelCoords, texture) => {
const gridCoords = [Math.floor(pixelCoords[0] / dotSize), Math.floor(pixelCoords[1] / dotSize)]

const originalColor = texture2D(pixelCoords)
const grayValue = (originalColor[0] + originalColor[1] + originalColor[2]) / (3 * 255)
const samplerColor = texture(pixelCoords)
const grayValue = (samplerColor[0] + samplerColor[1] + samplerColor[2]) / (3 * 255)
const ditherValue = (Math.random() - 0.5) * ditherRange
const adjustedGrayValue = Math.min(Math.max(grayValue + ditherValue, 0), 1)

Expand Down
8 changes: 4 additions & 4 deletions packages/pixel-profile/src/shaders/pixelate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { render } from '../renderer'

export function pixelate(source: Buffer, width: number, height: number, blockSize: number): Buffer {
return render(source, width, height, (uv, texture2D) => {
const x = Math.floor(uv[0] / blockSize)
const y = Math.floor(uv[1] / blockSize)
return render(source, width, height, (coords, texture) => {
const x = Math.floor(coords[0] / blockSize)
const y = Math.floor(coords[1] / blockSize)

return texture2D([x * blockSize + blockSize / 2, y * blockSize + blockSize / 2])
return texture([x * blockSize + blockSize / 2, y * blockSize + blockSize / 2])
})
}
6 changes: 3 additions & 3 deletions packages/pixel-profile/src/shaders/scanline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const scanlineIntensity = 0.15
const scanlineThickness = 3

export function scanline(source: Buffer, width: number, height: number): Buffer {
return render(source, width, height, (uv, texture2D) => {
const onScanline = uv[1] % scanlineThickness === 0
return render(source, width, height, (coords, texture) => {
const onScanline = coords[1] % scanlineThickness === 0

const samplerColor = texture2D(uv)
const samplerColor = texture(coords)

const scanlineBrightness = onScanline ? 1 - scanlineIntensity : 1

Expand Down

0 comments on commit 5082a0f

Please sign in to comment.