Skip to content
This repository was archived by the owner on Feb 1, 2026. It is now read-only.

Commit fa47c12

Browse files
authored
refactor: streamline instance exposure and remove unused refs (#629)
* refactor(Sky.vue, Sparkles/component.vue): streamline instance exposure and remove unused refs - Updated `Sky.vue` to directly expose the `skyImpl` instance instead of using a shallow reference, improving clarity and performance. - Removed the unused `sparkleRef` in `Sparkles/component.vue`, simplifying the component structure while maintaining functionality. - These changes enhance code maintainability and align with best practices for instance management in Vue components. * fix: update camera reference handling in Billboard and ScreenSpace components - Modified the `update` function in `Billboard.vue` to correctly use `camera.value` for improved reactivity. - Updated the camera reference handling in `ScreenSpace.vue` to ensure both `outerRef` and `camera.value` are checked before accessing their properties, enhancing stability. - Adjusted the `imageBounds` computed property in `Image/component.vue` to safely access image dimensions, preventing potential runtime errors. - Removed unused `shallowRef` import in `Sparkles/component.vue` to streamline the component structure. * refactor: update type references to TresObject for improved type safety - Changed the type of `dummy` in `TransformPayload` to `TresObject` for better alignment with TresJS framework. - Updated `objRef` and `parentRef` in `Helper/component.vue` to use `TresObject`, enhancing type accuracy. - Modified the `useHelper` function to accept `MaybeRefOrGetter<TresObject>` instead of `Object3D`, ensuring consistency across helper functionalities. - These changes improve type safety and maintainability in the codebase. * refactor(Text3D.vue, material.ts, component.vue): enhance type safety and clarity - Updated `Text3D.vue` to explicitly type the `slots` variable as `Slots` and the return type of `localText` as `string`, improving type safety and clarity. - Removed the unused type import of `Object3D` in `Helper/component.vue` to streamline the component structure. - Extended Three.js types in `material.ts` to include runtime properties in the `Material` interface and created a properly typed interface for `MeshGlassMaterial`, enhancing type accuracy and maintainability. - These changes contribute to better type safety and code clarity across the components. * fix(ContactShadows.vue): remove unnecessary TypeScript error suppression - Removed the `@ts-expect-error` comments in the `blurShadow` function, as they are no longer needed. This change improves code clarity and maintains a cleaner codebase by eliminating outdated error handling comments. - The removal aligns with ongoing efforts to enhance type safety and maintainability in the project.
1 parent d59ef26 commit fa47c12

File tree

11 files changed

+44
-29
lines changed

11 files changed

+44
-29
lines changed

src/core/abstractions/Billboard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function update(camera?: Camera) {
6060
}
6161
6262
useLoop().onBeforeRender(({ camera }) => {
63-
if (props.autoUpdate) { update(camera) }
63+
if (props.autoUpdate) { update(camera.value) }
6464
})
6565
6666
defineExpose({ instance: outerRef, update })

src/core/abstractions/Image/component.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const imageRef = shallowRef()
7979
const texture = shallowRef<Texture | null>(props.texture ?? null)
8080
const size = useTres().sizes
8181
const planeBounds = computed(() => Array.isArray(props.scale) ? [props.scale[0], props.scale[1]] : [props.scale, props.scale])
82-
const imageBounds = computed(() => [texture.value?.image.width ?? 0, texture.value?.image.height ?? 0])
82+
const imageBounds = computed(() => [texture.value?.image?.width ?? 0, texture.value?.image?.height ?? 0])
8383
const resolution = computed(() => Math.max(size.width.value, size.height.value))
8484
8585
watchEffect(() => {
@@ -88,7 +88,7 @@ watchEffect(() => {
8888
}
8989
else {
9090
const { state: t } = useTexture(props.url!)
91-
texture.value = t
91+
texture.value = t.value
9292
}
9393
})
9494

src/core/abstractions/ScreenSpace.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ withDefaults(defineProps<ScreenSpaceProps>(), {
1313
const outerRef = shallowRef()
1414
1515
useLoop().onBeforeRender(({ camera }) => {
16-
if (outerRef.value) {
17-
outerRef.value.quaternion.copy(camera.quaternion)
18-
outerRef.value.position.copy(camera.position)
16+
if (outerRef.value && camera.value) {
17+
outerRef.value.quaternion.copy(camera.value.quaternion)
18+
outerRef.value.position.copy(camera.value.position)
1919
}
2020
})
2121

src/core/abstractions/Text3D.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { useTres } from '@tresjs/core'
33
import { FontLoader, TextGeometry } from 'three-stdlib'
44
import { computed, shallowRef, toRefs, toValue, useSlots, watch, watchEffect } from 'vue'
5+
import type { Slots } from 'vue'
56
import type { TextGeometryParameters } from 'three-stdlib'
67
78
export interface Glyph {
@@ -161,9 +162,9 @@ extend({ TextGeometry })
161162
162163
const loader = new FontLoader()
163164
164-
const slots = useSlots()
165+
const slots: Slots = useSlots()
165166
166-
const localText = computed(() => {
167+
const localText = computed((): string => {
167168
if (text?.value) { return text.value }
168169
else if (slots.default) { return (slots.default()[0].children as string)?.trim() }
169170
return needUpdates.value ? '' : 'TresJS'

src/core/abstractions/useSurfaceSampler/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { MeshSurfaceSampler } from 'three-stdlib'
88
import { ref } from 'vue'
99
import type { InstancedMesh, Mesh, Object3DEventMap } from 'three'
10+
import type { TresObject } from '@tresjs/core'
1011

1112
export interface useSurfaceSamplerProps {
1213
/*
@@ -67,7 +68,7 @@ type TransformPayload = SamplePayload & {
6768
* This object's matrix will be updated after transforming & it will be used
6869
* to set the instance's matrix.
6970
*/
70-
dummy: Object3D<Object3DEventMap>
71+
dummy: TresObject
7172
/**
7273
* The mesh that's initially passed to the sampler.
7374
* Use this if you need to apply transforms from your mesh to your instances
@@ -101,7 +102,7 @@ export const useSurfaceSampler = (
101102
const position = new Vector3()
102103
const normal = new Vector3()
103104
const color = new Color()
104-
const dummy = new Object3D<Object3DEventMap>()
105+
const dummy = new Object3D<Object3DEventMap>() as TresObject
105106

106107
mesh.updateMatrixWorld(true)
107108

src/core/controls/Helper/component.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
2-
import type { Object3D } from 'three'
32
import { shallowRef, watchEffect } from 'vue'
43
import { useHelper } from './useHelper'
4+
import type { TresObject } from '@tresjs/core'
55
66
type HelperConstructor = new (...args: any[]) => any
77
@@ -12,8 +12,8 @@ export interface HelperProps {
1212
1313
const props = defineProps<HelperProps>()
1414
15-
const objRef = shallowRef<Object3D>()
16-
const parentRef = shallowRef<Object3D>()
15+
const objRef = shallowRef<TresObject>()
16+
const parentRef = shallowRef<TresObject>()
1717
1818
watchEffect(() => {
1919
if (objRef.value && objRef.value.parent) {

src/core/controls/Helper/useHelper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Object3D } from 'three'
22
import type { MaybeRefOrGetter } from 'vue'
33
import { onBeforeUnmount, shallowRef, toValue, watchEffect } from 'vue'
4+
import type { TresObject } from '@tresjs/core'
45
import { useLoop, useTres } from '@tresjs/core'
56

67
// NOTE: Source
@@ -10,7 +11,7 @@ type HelperType = Object3D & { update: () => void, dispose: () => void }
1011
type HelperConstructor = new (...args: any[]) => any
1112

1213
export function useHelper<T extends HelperConstructor>(
13-
object3D: MaybeRefOrGetter<Object3D | null | undefined | false>,
14+
object3D: MaybeRefOrGetter<TresObject | null | undefined | false>,
1415
helperConstructor: T,
1516
...args: any[]
1617
) {

src/core/materials/meshGlassMaterial/material.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import type { MeshStandardMaterialParameters } from 'three'
22
import { Color, MathUtils, MeshStandardMaterial, Vector2 } from 'three'
33

4-
class MeshGlassMaterial extends MeshStandardMaterial {
4+
// Extend Three.js types to include properties that exist at runtime but aren't in the type definitions
5+
declare module 'three' {
6+
interface Material {
7+
defines: Record<string, string | number | boolean>
8+
}
9+
}
10+
11+
// Create a properly typed interface for our glass material
12+
interface IMeshGlassMaterial extends MeshStandardMaterial {
13+
defines: Record<string, string | number | boolean>
14+
version: number
15+
}
16+
17+
class MeshGlassMaterial extends MeshStandardMaterial implements IMeshGlassMaterial {
518
isMeshPhysicalMaterial: boolean
619
clearcoatMap: null
720
clearcoatRoughness: number
@@ -74,7 +87,8 @@ class MeshGlassMaterial extends MeshStandardMaterial {
7487
set clearcoat(value) {
7588
// eslint-disable-next-line style/no-mixed-operators
7689
if (this._clearcoat > 0 !== value > 0) {
77-
this.version++
90+
// Increment version to trigger shader recompilation - using mutable interface
91+
;(this as IMeshGlassMaterial).version++
7892
}
7993

8094
this._clearcoat = value
@@ -87,13 +101,14 @@ class MeshGlassMaterial extends MeshStandardMaterial {
87101
set transmission(value) {
88102
// eslint-disable-next-line style/no-mixed-operators
89103
if (this._transmission > 0 !== value > 0) {
90-
this.version++
104+
// Increment version to trigger shader recompilation - using mutable interface
105+
;(this as IMeshGlassMaterial).version++
91106
}
92107

93108
this._transmission = value
94109
}
95110

96-
copy(source: any) {
111+
copy(source: MeshGlassMaterial) {
97112
super.copy(source)
98113

99114
this.defines = {

src/core/staging/ContactShadows.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ function blurShadow(
165165
pool.horizontalBlurMaterial.uniforms.h.value = blur / 256
166166
167167
renderer.setRenderTarget(pool.renderTargetBlur)
168-
// @ts-expect-error - TODO: wait for https://github.com/Tresjs/tres/issues/1040 to be fixed
168+
169169
renderer.render(pool.blurPlane, pool.shadowCamera)
170170
171171
pool.blurPlane.material = pool.verticalBlurMaterial
172172
pool.verticalBlurMaterial.uniforms.tDiffuse.value = pool.renderTargetBlur.texture
173173
pool.verticalBlurMaterial.uniforms.v.value = blur / 256
174174
175175
renderer.setRenderTarget(pool.renderTarget)
176-
// @ts-expect-error - TODO: wait for https://github.com/Tresjs/tres/issues/1040 to be fixed
176+
177177
renderer.render(pool.blurPlane, pool.shadowCamera)
178178
179179
pool.blurPlane.visible = false

src/core/staging/Sky.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useTres } from '@tresjs/core'
33
// eslint-disable-file vue/attribute-hyphenation
44
import { MathUtils, Vector3 } from 'three'
55
import { Sky as SkyImpl } from 'three-stdlib'
6-
import { computed, shallowRef, watch } from 'vue'
6+
import { computed, watch } from 'vue'
77
88
export interface SkyProps {
99
/**
@@ -53,7 +53,6 @@ watch(props, () => {
5353
invalidate()
5454
})
5555
56-
const skyRef = shallowRef<SkyImpl>()
5756
const skyImpl = new SkyImpl()
5857
const sunPosition = computed(() =>
5958
getSunPosition(props.azimuth, props.elevation),
@@ -66,14 +65,13 @@ function getSunPosition(azimuth: number, elevation: number) {
6665
}
6766
6867
defineExpose({
69-
instance: skyRef,
68+
instance: skyImpl,
7069
sunPosition: sunPosition.value,
7170
})
7271
</script>
7372

7473
<template>
7574
<primitive
76-
ref="skyRef"
7775
:object="skyImpl"
7876
:material-uniforms-turbidity-value="props.turbidity"
7977
:material-uniforms-rayleigh-value="props.rayleigh"

0 commit comments

Comments
 (0)