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: attach detach #749

Merged
merged 27 commits into from
Jul 5, 2024
Merged

fix: attach detach #749

merged 27 commits into from
Jul 5, 2024

Conversation

andretchen0
Copy link
Contributor

@andretchen0 andretchen0 commented Jun 29, 2024

closes #496

Context

This PR deals with "attached objects", e.g., materials, geometries.

These objects can't be add()ed via THREE's Object3D.add(value). Instead, they are "attached" as values as in someObject[someField] = value.

Compared to add()ing, "attach"ing requires extra implementation steps and logic in order to support Vue's declarative paradigm transparently for users.

This PR is another in a series to prepare for a rework of Tres' <primitive />. Issue: #701

Playground

http://localhost:5173/advanced/material-array

Problem

Fields with attached objects don't revert back their original values when unmounted. As a result, an unmounted material will continue to affect its parent.

Solution

When attaching, save the previous value to a variable and restore it when detaching.

Problem

The current implementation of :attach doesn't allow for array indices. This means e.g., it's not possible to attach multiple materials, even though THREE allows this. #496

Solution

Add "attach" logic to traverse arrays.

Problem

:attach is a prop but isn't reactive, as users might expect.

Solution

Implement reactivity for :attach.

Problem

:attach prop value can't be a "pierced prop", i.e., it can only replace values directly on the parent – parent[field]. It can't be used to "drill down" into parent[object][field].

Solution

Implement "pierced" logic for :attach – see resolve in src/utils/index.ts and tests in src/utils/index.test.ts.

Refactor

THREE does not define the attach field. Tres defines it. Most Tres data is stored in object.__tres. attach is stored directly on object as object.attach.

Refactor and store :attach value in object.__tres.attach.

Refactor

Move src/utils/nodeOpsUtils.ts to src/utils/index.ts.

Copy link

netlify bot commented Jun 29, 2024

Deploy Preview for tresjs-docs ready!

Name Link
🔨 Latest commit 87fd48a
🔍 Latest deploy log https://app.netlify.com/sites/tresjs-docs/deploys/6687f87e3eb79000083d862b
😎 Deploy Preview https://deploy-preview-749--tresjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@andretchen0 andretchen0 marked this pull request as ready for review June 29, 2024 22:06
@alvarosabu alvarosabu self-assigned this Jul 1, 2024
@alvarosabu alvarosabu added the p4-important-bug Violate documented behavior or significantly improve performance (priority) label Jul 1, 2024
@garrlker garrlker self-requested a review July 1, 2024 21:19
Copy link
Member

@alvarosabu alvarosabu left a comment

Choose a reason for hiding this comment

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

Amazing work @andretchen0 thanks for solving this 🙏🏻

Copy link
Collaborator

@garrlker garrlker left a comment

Choose a reason for hiding this comment

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

Sorry, it took me a while to review the full code changes
I love the attention to detail in your unit tests. No issues here

I also ran through the playground suites as well for more of a hands on verification and everything looks good.

Approved!

@alvarosabu alvarosabu merged commit 8c1c668 into main Jul 5, 2024
7 checks passed
@alvarosabu alvarosabu deleted the fix/attach-detach branch July 5, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important-bug Violate documented behavior or significantly improve performance (priority)
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Allow TresObject3D#material to set Array
3 participants