jnsmalm / pixi3d

The 3D renderer for PixiJS. Seamless integration with 2D applications.
https://pixi3d.org
MIT License
759 stars 44 forks source link

mixing 2d and 3d geometry? #64

Closed ojack closed 2 years ago

ojack commented 2 years ago

Thanks for making this! Just digging in now and have some questions:::

Is it possible to add 3d objects as children to 2d objects?

I tried adding an object to a container, but was unable to get position transformations applied to the container to affect the 3d object. https://codesandbox.io/s/damp-haze-gdy0k?file=/src/index.js

jnsmalm commented 2 years ago

Hello!

Yes, you can add 3d objects as children to 2d object but as you already noticed the transforms are not compatible. To render a 3d object at the same position as a 2d object you can use Camera.screenToWorld. See https://api.pixi3d.org/classes/Camera.html#screenToWorld

Something like:

let sprite = new PIXI.Sprite()
let object3d = ...
let distanceFromCamera = 5
object3d.position = PIXI3D.Camera.main.screenToWorld(sprite.x, sprite.y, distanceFromCamera)