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

How to access Three.js custom objects created on scene? #4970

Open
RodrigoRVSN opened this issue Jan 24, 2025 · 3 comments
Open

How to access Three.js custom objects created on scene? #4970

RodrigoRVSN opened this issue Jan 24, 2025 · 3 comments

Comments

@RodrigoRVSN
Copy link

RodrigoRVSN commented Jan 24, 2025

Question

How can I get the children of the scene, including the custom objects added with addObject3D?

Workaround

I managed to do that using

const viewer = new Cognite3DViewer({
    sdk: client,
    domElement: widget,
    loadingIndicatorStyle: { placement: 'topRight', opacity: 1 },
    antiAliasingHint: 'disabled',
})

console.log(viewer?.models[0].cadNode.parent.children)

With some TS errors (cadNode is not defined in TS), but would like to know if there's a more straightforward way to do that that I'm missing since it looks like getScene() was removed

Context

  • Using version ^4.15.1 of library
  • The idea of being able to access the children on the scene would be to, in our measurement tool feature created from scratch, be able to intersect with the Raycast the LineSegments, etc. created on the scene
@RodrigoRVSN RodrigoRVSN changed the title [question] How to access THREE.js scene children with custom objects created? How to access Three.js custom objects created on scene? Jan 27, 2025
@pramod-cog
Copy link
Contributor

@RodrigoRVSN To get intersection for custom objects added into Reveal, you can use getAnyIntersectionFromPixel() API by enabling options.

public async getAnyIntersectionFromPixel(

Note: The API is still in Beta, but it can be used.

@RodrigoRVSN
Copy link
Author

Thanks for the answer @pramod-cog

I tried to do it like this

    const getHitObject = useCallback(
        async (event: MouseEvent) => {
            const intersection = await viewer?.getAnyIntersectionFromPixel(
                new THREE.Vector2(event.offsetX, event.offsetY),
                {
                    predicate: () => true,
                }
            )
            console.log(intersection)

            return intersection
        },
        [viewer]
    )

It is triggered in a mouvemove event listener (event variable), but for some reason, the intersection returns the cad elements on the scene, ignoring the custom objects created, like the LineSegments, Sprite, etc., and returning undefined for them.

Am I missing something in this implementation?

@pramod-cog
Copy link
Contributor

You need to pass the custom object in predicate callback. e.g Reference

const predicate = (customObject: ICustomObject): boolean => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants