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
32 changes: 32 additions & 0 deletions packages/viewer/src/lib/materials.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @ts-expect-error — bun:test is provided by the Bun runtime; viewer does not
// depend on @types/bun so the import type is unresolved at compile time.
import { describe, expect, test } from 'bun:test'
import type { MaterialSchema } from '@pascal-app/core'
import { getTextureKey, resolveTextureRepeat } from './materials'

function materialWithRepeat(repeat: unknown): MaterialSchema {
return {
texture: {
url: 'https://example.com/texture.png',
repeat,
},
} as unknown as MaterialSchema
}

describe('legacy texture repeat values', () => {
test('normalizes tuple, scalar, and Vector2-shaped repeats', () => {
expect(resolveTextureRepeat([2, 3], undefined)).toEqual([2, 3])
expect(resolveTextureRepeat(2, undefined)).toEqual([2, 2])
expect(resolveTextureRepeat({ x: 2, y: 3 }, undefined)).toEqual([2, 3])
})

test('falls back to scale for malformed repeats', () => {
expect(resolveTextureRepeat({ width: 2 }, 4)).toEqual([4, 4])
})

test('keeps distinct Vector2-shaped repeats in distinct cache entries', () => {
expect(getTextureKey(materialWithRepeat({ x: 2, y: 3 }))).not.toBe(
getTextureKey(materialWithRepeat({ x: 4, y: 5 })),
)
})
})
27 changes: 21 additions & 6 deletions packages/viewer/src/lib/materials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,28 @@ function getCacheKey(props: MaterialProperties, shading: RenderShading): string
return `${shading}-${props.color}-${props.roughness}-${props.metalness}-${props.opacity}-${props.transparent}-${props.side}`
}

function getTextureKey(material?: MaterialSchema): string {
function isFiniteNumber(value: unknown): value is number {
return typeof value === 'number' && Number.isFinite(value)
}

export function resolveTextureRepeat(repeat: unknown, scale: unknown): [number, number] {
const fallback = isFiniteNumber(scale) ? scale : 1
if (Array.isArray(repeat) && isFiniteNumber(repeat[0]) && isFiniteNumber(repeat[1])) {
return [repeat[0], repeat[1]]
}
if (isFiniteNumber(repeat)) return [repeat, repeat]
if (repeat && typeof repeat === 'object' && 'x' in repeat && 'y' in repeat) {
const { x, y } = repeat
if (isFiniteNumber(x) && isFiniteNumber(y)) return [x, y]
}
return [fallback, fallback]
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete tuple repeats use scale

Medium Severity

When texture.repeat is an array with only one finite element, resolveTextureRepeat rejects the whole value and applies scale on both axes. The previous path used each index with per-axis scale fallback, so U/V tiling can change and different materials can share the same getTextureKey and createMaterial cache entry.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c40517d. Configure here.


export function getTextureKey(material?: MaterialSchema): string {
const texture = material?.texture
if (!texture) return 'none'
const repeat = texture.repeat?.join('x') ?? 'default'
const scale = texture.scale ?? 'default'
return `${texture.url}-${repeat}-${scale}`
const [repeatX, repeatY] = resolveTextureRepeat(texture.repeat, texture.scale)
return `${texture.url}-${repeatX}x${repeatY}`
}

function getTexture(material?: MaterialSchema): THREE.Texture | undefined {
Expand All @@ -200,8 +216,7 @@ function getTexture(material?: MaterialSchema): THREE.Texture | undefined {
texture.wrapS = THREE.RepeatWrapping
texture.wrapT = THREE.RepeatWrapping

const repeatX = textureConfig.repeat?.[0] ?? textureConfig.scale ?? 1
const repeatY = textureConfig.repeat?.[1] ?? textureConfig.scale ?? 1
const [repeatX, repeatY] = resolveTextureRepeat(textureConfig.repeat, textureConfig.scale)
texture.repeat.set(repeatX, repeatY)
texture.updateMatrix()
texture.colorSpace = THREE.SRGBColorSpace
Expand Down
Loading