Skip to content

Commit 88a03ce

Browse files
committed
fix transparency in overlaymodel viewer
1 parent 317b546 commit 88a03ce

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

src/react/OverlayModelViewer.tsx

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@ subscribe(activeModalStack, () => {
9393
}
9494
})
9595

96+
// Helper function to setup material transparency
97+
const setupMaterialTransparency = (material: THREE.Material): void => {
98+
if (material instanceof THREE.MeshStandardMaterial ||
99+
material instanceof THREE.MeshBasicMaterial ||
100+
material instanceof THREE.MeshPhongMaterial) {
101+
// Check if material should be transparent
102+
const hasAlpha = material.alphaMap ||
103+
(material.opacity !== undefined && material.opacity < 1) ||
104+
(material.map && material.map.format === THREE.RGBAFormat)
105+
106+
if (hasAlpha) {
107+
// Configure transparency properly
108+
material.transparent = true
109+
material.alphaTest = 0.01 // Lower threshold for better transparency
110+
material.side = THREE.DoubleSide // Show both sides for transparent materials
111+
// Keep depthWrite enabled as requested - don't disable it
112+
} else {
113+
// Opaque materials
114+
material.transparent = false
115+
material.side = THREE.FrontSide
116+
}
117+
material.needsUpdate = true
118+
}
119+
}
120+
121+
// Helper function to check if mesh is transparent
122+
const isMeshTransparent = (mesh: THREE.Mesh): boolean => {
123+
if (Array.isArray(mesh.material)) {
124+
return mesh.material.some(mat => mat.transparent)
125+
}
126+
return mesh.material.transparent
127+
}
128+
96129
export default () => {
97130
const { model } = useSnapshot(modelViewerState)
98131
const containerRef = useRef<HTMLDivElement>(null)
@@ -230,20 +263,34 @@ export default () => {
230263
}
231264
object.traverse((child) => {
232265
if (child instanceof THREE.Mesh) {
233-
if (child.material && customization) {
234-
const material = child.material as THREE.MeshStandardMaterial
266+
const material = child.material as THREE.MeshStandardMaterial | THREE.MeshBasicMaterial | THREE.MeshPhongMaterial
267+
268+
if (material && customization) {
235269
if (customization.color) {
236270
material.color.setHex(parseInt(customization.color.replace('#', ''), 16))
237271
}
238272
if (customization.opacity !== undefined) {
239273
material.opacity = customization.opacity
240274
material.transparent = customization.opacity < 1
241275
}
242-
if (customization.metalness !== undefined) {
243-
material.metalness = customization.metalness
276+
if (material instanceof THREE.MeshStandardMaterial) {
277+
if (customization.metalness !== undefined) {
278+
material.metalness = customization.metalness
279+
}
280+
if (customization.roughness !== undefined) {
281+
material.roughness = customization.roughness
282+
}
244283
}
245-
if (customization.roughness !== undefined) {
246-
material.roughness = customization.roughness
284+
}
285+
286+
// Enable transparency for materials that need it
287+
if (material) {
288+
if (Array.isArray(material)) {
289+
for (const mat of material) {
290+
setupMaterialTransparency(mat)
291+
}
292+
} else {
293+
setupMaterialTransparency(material)
247294
}
248295
}
249296
}
@@ -375,7 +422,7 @@ export default () => {
375422
camera.position.set(0, 0, 3) // Position camera to view player model optimally
376423

377424
// Setup renderer with pixel density awareness
378-
const renderer = new THREE.WebGLRenderer({ alpha: true })
425+
const renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true })
379426
renderer.useLegacyLights = false
380427
renderer.outputColorSpace = THREE.LinearSRGBColorSpace
381428
let scale = window.devicePixelRatio || 1
@@ -479,11 +526,22 @@ export default () => {
479526
playerObject.ears.visible = false
480527
playerObject.cape.visible = false
481528

482-
// Enable shadows for player object
529+
// Enable shadows for player object and setup transparency
483530
wrapper.traverse((child) => {
484531
if (child instanceof THREE.Mesh) {
485532
child.castShadow = true
486533
child.receiveShadow = true
534+
535+
// Setup transparency for player object materials
536+
if (child.material) {
537+
if (Array.isArray(child.material)) {
538+
for (const mat of child.material) {
539+
setupMaterialTransparency(mat)
540+
}
541+
} else {
542+
setupMaterialTransparency(child.material)
543+
}
544+
}
487545
}
488546
})
489547

0 commit comments

Comments
 (0)