Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(primitive): implement as proxy to avoid breaking references #764

Merged
merged 46 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
cb83888
test: add insert/remove tests
andretchen0 Jun 19, 2024
700d1ba
feat: add filterInPlace
andretchen0 Jun 20, 2024
3d1541f
refactor: make some LocalState fields non-optional
andretchen0 Jun 20, 2024
808e082
test: add LocalState graph tests
andretchen0 Jun 20, 2024
9af075a
refactor: add prepare() to add __tres field
andretchen0 Jun 20, 2024
b2eb17a
refactor: add TODOs
andretchen0 Jun 20, 2024
6880acf
refactor: maintain parent/objects relationship in __tres
andretchen0 Jun 20, 2024
ec47e0d
test: add dispose=null test
andretchen0 Jun 21, 2024
fac2a08
feat: allow "dispose=null" to bail out tree disposal
andretchen0 Jun 21, 2024
f57de05
refactor: update comments
andretchen0 Jun 24, 2024
4ecd5ea
refactor: add todo
andretchen0 Jun 24, 2024
00ed8ae
test: add/unskip tests
andretchen0 Jun 24, 2024
a585a35
refactor(nodeOps): move helper functions to new file
andretchen0 Jun 24, 2024
f7c8ce9
chore: merge upstream branch
andretchen0 Jun 24, 2024
46e6238
test: add primitive tests
andretchen0 Jun 27, 2024
21dded6
refactor: move nodeOpsUtils to utils
andretchen0 Jun 28, 2024
81e9008
feat: add pierced attach/detach
andretchen0 Jun 28, 2024
ee6cd48
Merge branch 'main' into fix/attach-detach
andretchen0 Jun 29, 2024
5a037c4
chore: clean up merge
andretchen0 Jun 29, 2024
6c2a436
chore: lint
andretchen0 Jun 29, 2024
7f4e72f
docs: add playground demo
andretchen0 Jun 29, 2024
ff825fa
Merge branch 'main' into fix/attach-detach
andretchen0 Jun 29, 2024
00e1cb4
chore: update demos
andretchen0 Jun 29, 2024
94aa6f1
fix: use proxy for primitive
andretchen0 Jun 30, 2024
9af94fd
fix(primitive): add playground
andretchen0 Jun 30, 2024
163bfd3
test: add material swap test
andretchen0 Jul 1, 2024
b520204
refactor: remove unused variable
andretchen0 Jul 1, 2024
be372d9
refactor: rewrite comment
andretchen0 Jul 1, 2024
2dec745
docs: add attach/detach demo
andretchen0 Jul 1, 2024
05f3d13
test: update test with new function signature
andretchen0 Jul 1, 2024
2529710
refactor: format playground demo
andretchen0 Jul 1, 2024
652ef4c
refactor: add isTresContext
andretchen0 Jul 4, 2024
403f3cc
feat: add general purpose dispose function
andretchen0 Jul 4, 2024
a17177e
fix: do not clone primitive object
andretchen0 Jul 4, 2024
c1b88ef
fix: add shallow removal for primitives
andretchen0 Jul 7, 2024
7fd181b
docs: update primitive object playground
andretchen0 Jul 8, 2024
4b536d9
refactor(remove): move detach, deregister to utils
andretchen0 Jul 8, 2024
260d6ca
chore: merge files from main
andretchen0 Jul 8, 2024
8dc3378
Merge branch 'main' into fix/primitive
andretchen0 Jul 8, 2024
9ff32cc
docs: add Sparkles playground
andretchen0 Jul 8, 2024
171d34f
chore: check if node.__tres is not undefined for increasing eventCount
alvarosabu Jul 9, 2024
bf79e91
docs: improve primitives docs
alvarosabu Jul 9, 2024
b077b5a
docs: added shallowRef suggestion for object
alvarosabu Jul 9, 2024
3c65d6b
chore: added dvanced disposal to playground
alvarosabu Jul 9, 2024
f7eb00e
refactor: add type cast
andretchen0 Jul 9, 2024
9fb8cc9
refactor(TresContext): remove isTresContext
andretchen0 Jul 9, 2024
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
34 changes: 29 additions & 5 deletions docs/advanced/primitive.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Primitives

The `<primitive />` component is a versatile low-level component in TresJS that allows you to directly use any three.js object within your Vue application without an abstraction. It acts as a bridge between Vue's reactivity system and three.js's scene graph.
The `<primitive />` component is a versatile low-level component in TresJS that allows you to directly use any [three.js](https://threejs.org/) object within your Vue application without an abstraction. It acts as a bridge between Vue's reactivity system and THREE's scene graph.

## Usage

Expand All @@ -18,15 +18,39 @@ The `<primitive />` component is a versatile low-level component in TresJS that
</script>

<template>
<TresCanvas>
<primitive :object="meshWithMaterial" />
</TresCanvas>
<primitive :object="meshWithMaterial" />
</template>
```

## Props

`object`: This prop expects a three.js Object3D or any of its derived classes. It is the primary object that the `<primitive />` component will render. In the updated example, a `Mesh` object with an associated `Material` is passed to this prop.
- `object`: This prop expects either a plain or a reactive three.js [Object3D](https://threejs.org/docs/index.html?q=Object#api/en/core/Object3D) (preferably a [shallowRef](https://vuejs.org/api/reactivity-advanced.html#shallowref)) or any of its derived classes. It is the primary object that the `<primitive />` component will render. In the updated example, a `Mesh` object with an associated `Material` is passed to this prop.

## Events

The same pointer events available on the TresJS components are available on the `<primitive />` component. You can use these events to interact with the object in the scene. See the complete list of events [here](/api/events).

```html
<template>
<primitive
:object="meshWithMaterial"
@click="onClick"
@pointermove="onPointerMove"
/>
</template>
```

## Passing childrens via slots

You can also pass children to the `<primitive />` component using slots. This is useful when you want to add additional objects to the scene that are not part of the main object.

```html
<template>
<primitive :object="meshWithOnlyGeometry">
<MeshBasicMaterial :color="0xff0000" />
</primitive>
</template>
```

## Usage with Models

Expand Down
57 changes: 57 additions & 0 deletions playground/src/pages/advanced/disposal/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<script setup lang="ts">
import { shallowRef } from 'vue'
import { TresCanvas } from '@tresjs/core'

const boxRef = shallowRef()

const tests = [
{
getPass: () => {
const show = boxRef.value?.show
const parentName = boxRef.value?.instance?.parent?.name || null
return !show || parentName === 'intended-parent'
},
msg: 'v-if is false or Box has intended parent',
},
]

const testsRef = shallowRef({ run: () => {} })
let intervalId: ReturnType<typeof setInterval> | null = null
onMounted(() => {
intervalId = setInterval(() => testsRef.value.run(), 100)
})
onUnmounted(() => intervalId && clearInterval(intervalId))
</script>

<template>
<TresCanvas clear-color="gray">
<TresPerspectiveCamera :position="[5, 5, 5]" :look-at="[1, 2, 3]" />
<TresMesh :position="[1, 2, 3]" name="intended-parent">
<TresMesh
v-for="(_, i) of Array.from({ length: 8 }).fill(0)"
:key="i"
:position="[
i % 2 ? -0.5 : 0.5,
Math.floor(i / 2) % 2 ? -0.5 : 0.5,
Math.floor(i / 4) % 2 ? -0.5 : 0.5,
]"
>
<TresBoxGeometry :args="[0.1, 0.1, 0.1]" />
<TresMeshBasicMaterial color="red" />
</TresMesh>
</TresMesh>
</TresCanvas>
<OverlayInfo>
<h1>Issue #717: v-if</h1>
<h2>Setup</h2>
<p>
In this scene, there is a Box with a <code>v-if</code>. Its <code>v-if</code> value is toggled on and off.
When visible, the box's 8 corners should appear at the centers of the red boxes.
</p>
<h2>Tests</h2>
<Tests ref="testsRef" :tests="tests" />
<h2>Issue</h2>
<a href="https://github.com/Tresjs/tres/issues/706#issuecomment-2146244326">
Toggle v-if on a Tres component declared in a separate SFC makes it detach from its parent</a>
</OverlayInfo>
</template>
13 changes: 13 additions & 0 deletions playground/src/pages/issues/701-cientos-v4/TheExperience.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- eslint-disable no-console -->
<script setup lang="ts">
import { OrbitControls, Sparkles, TorusKnot } from '@tresjs/cientos'
</script>

<template>
<TresPerspectiveCamera />
<OrbitControls />
<TorusKnot :scale="0.5" :args="[1, 0.35, 128, 32]">
<TresMeshBasicMaterial color="black" />
<Sparkles />
</TorusKnot>
</template>
35 changes: 35 additions & 0 deletions playground/src/pages/issues/701-cientos-v4/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!-- eslint-disable no-console -->
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import TheExperience from './TheExperience.vue'
</script>

<template>
<TresCanvas window-size clear-color="gray">
<TheExperience />
</TresCanvas>

<OverlayInfo>
<h1>&lt;primitive&gt; in Cientos v4</h1>
<h2>Setup</h2>
<p>This scene contains a TorusKnot and Cientos' Sparkles component.</p>
<h2>Context</h2>
<p>Sparkles uses a primitive under the hood. Changes to Tres v4's primitives caused the component to stop working.</p>
</OverlayInfo>
</template>

<style scoped>
.overlay {
position: fixed;
max-width: 400px;
top: 0px;
left: 0px;
margin: 10px;
padding: 10px;
background-color: white;
border-radius: 6px;
font-family: sans-serif;
font-size: 14px;
color: #444;
}
</style>
Loading