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

feat: 503 conditional rendering of primitives #514

Merged
merged 10 commits into from
Feb 21, 2024
Prev Previous commit
Next Next commit
chore: playground primitives with models
  • Loading branch information
alvarosabu committed Jan 18, 2024
commit 5a24eea1088d334378bc90b5d1123e1a87d3600a
1 change: 1 addition & 0 deletions playground/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module 'vue' {
DanielTest: typeof import('./src/components/DanielTest.vue')['default']
DebugUI: typeof import('./src/components/DebugUI.vue')['default']
DeleteMe: typeof import('./src/components/DeleteMe.vue')['default']
DynamicModel: typeof import('./src/components/DynamicModel.vue')['default']
FBXModels: typeof import('./src/components/FBXModels.vue')['default']
Gltf: typeof import('./src/components/gltf/index.vue')['default']
LocalOrbitControls: typeof import('./src/components/LocalOrbitControls.vue')['default']
Expand Down
19 changes: 19 additions & 0 deletions playground/src/components/DynamicModel.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script setup lang="ts">
import { useControls } from '@tresjs/leches'
import { useGLTF } from '@tresjs/cientos'

const { scene } = await useGLTF('https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/blender-cube.glb', { draco: true })

Check failure on line 5 in playground/src/components/DynamicModel.vue

View workflow job for this annotation

GitHub Actions / Lint (16)

This line has a length of 133. Maximum allowed is 120

const { nodes } = await useGLTF(
'https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/aku-aku/AkuAku.gltf',
{ draco: true },
)
const model = nodes.AkuAku
const { isCube } = useControls({
isCube: true,
})
</script>

<template>
<primitive :object="isCube ? scene : model" />
</template>
21 changes: 15 additions & 6 deletions playground/src/pages/primitives.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { BasicShadowMap, SRGBColorSpace, NoToneMapping, Mesh, TorusGeometry, MeshToonMaterial, TorusKnotGeometry, PlaneGeometry } from 'three'
import { BasicShadowMap, SRGBColorSpace, NoToneMapping, Mesh, TorusGeometry, MeshToonMaterial, TorusKnotGeometry, PlaneGeometry, Group, SphereGeometry } from 'three'

Check failure on line 3 in playground/src/pages/primitives.vue

View workflow job for this annotation

GitHub Actions / Lint (16)

This line has a length of 165. Maximum allowed is 120
import { TresCanvas } from '@tresjs/core'
import { OrbitControls } from '@tresjs/cientos'
import { TresLeches, useControls } from '@tresjs/leches'
Expand Down Expand Up @@ -44,12 +44,19 @@
}),
)

const plane = new Mesh(
new PlaneGeometry(10, 10, 10, 10),
const sphere = new Mesh(
new SphereGeometry(1, 32, 32),
new MeshToonMaterial({
color: '#82DBC5',
}),
)

Copy link
Contributor

@andretchen0 andretchen0 Jan 20, 2024

Choose a reason for hiding this comment

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

In nodeOps.ts, I mentioned that cloning means that setup references are lost. Below is some demonstration code that can be pasted here.

useRenderLoop().onLoop(() => {
  torus.rotateX(0.01)
  torusKnot.rotateY(0.01)
})

Both torus and torusKnot continue to exist after the clone, but they no longer point to the on-screen objects, so the on-screen objects don't update.

If the objects aren't clone()d in nodeOps, the on-screen shapes will rotate – but other errors pop up.

I don't know if there's a workaround here. Just pointing it out.

Copy link
Member Author

Choose a reason for hiding this comment

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

@andretchen0 This argument makes me think we should abandon the idea of trying to make :object prop reactive, swiping instances without losing the ref is getting too complicated.

Users could still use primitives with conditional rendering, I wanted primitive to work similar to component but honestly at this point I don't know how to achieve it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@alvarosabu

I don't understand the internals of Tres well enough to have an opinion on the feasibility here.

I'll start reading the source after I finish <AnimatedSprite /> for Cientos.

I wanted primitive to work similar to component but honestly at this point I don't know how to achieve it.

I agree that this is a worthwhile goal. It'd be great if it "just works".

Copy link
Member Author

Choose a reason for hiding this comment

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

@andretchen0 let me know if you are up to a pair-programming call to discuss the internals when you get free let me know, we definitely can use your knowledge and your quality feedback inside of the custom renderer code. 💚

Copy link
Contributor

Choose a reason for hiding this comment

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

@alvarosabu

Sure thing. That could be fun!

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @andretchen0 I created a thread on the core team discord channel discussing this but to give a summary:

I found a way of making it work (animations), the only constraint is that the object passed through :object needs to be of the same type (mesh -> mesh) (group -> group). If we do (mesh -> group), it doesn't work anymore.

Screen_Recording_2024-02-02_at_12.43.44.mov

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! I'll go read the Discord.

const firstGroup = new Group()
firstGroup.add(torusMesh)
firstGroup.add(torusKnot)

const secondGroup = new Group()
secondGroup.add(sphere)
</script>

<template>
Expand All @@ -67,10 +74,12 @@
/>
<OrbitControls />
<primitive
:position="[0, 2, 0]"
:object="knot ? torusKnot : torusMesh"
:position="[4, 2, 0]"
:object="knot ? firstGroup : secondGroup"
/>
<primitive :object="plane" />
<Suspense>
<DynamicModel />
</Suspense>
<TresAxesHelper :args="[1]" />
<TresDirectionalLight
:position="[0, 2, 4]"
Expand Down
Loading