marcofugaro / three-projected-material

📽 Three.js Material which lets you do Texture Projection on a 3d Model
https://marcofugaro.github.io/three-projected-material/
MIT License
671 stars 57 forks source link

Update Projection with Camera Position #28

Closed sonvuhoang closed 2 years ago

sonvuhoang commented 2 years ago

Is it possible to update the projection of the texture to follow the camera position?

This question has already been asked before, but since the repo has changed so much since it was asked, what changes do I need to make to get this to work?

marcofugaro commented 2 years ago

Hey, for now you have to call material.project(mesh) each frame, but in the future this may be simplified.

sonvuhoang commented 2 years ago

Hi, thanks for the quick response! I adapted your basic example to include:

webgl.onUpdate(() => {
    if(webgl.isDragging){
        material.project(box)
    }
})

When I drag to another side of the box, the projection stays in the original position. Did I misunderstand what "each frame" meant?

marcofugaro commented 2 years ago

You have to change the projector camera position (the one with the helper), not the view camera.

You can take instead the example where the projector camera and the view camera are the same, the same-camera example, and add this in the webgl.onUpdate() call:

elements.children.forEach((element) => {
  element.material.project(element)
})

This should be the result

https://user-images.githubusercontent.com/7217420/152066480-7c62b65b-8d66-4398-90d8-a55b8182deda.mov

sonvuhoang commented 2 years ago

Thanks, I got it working! I didn't realize I had to change the projector camera position instead of the view position.