question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

THREE.CSS3DRenderer does not respect THREE.PerspectiveCamera.setViewOffset

See original GitHub issue

Describe the bug

I would like to use both the WebGLRenderer and CSS3DRenderer in an application, however that application calls “setViewOffset” to adjust the vanishing point of the drawing. This has the effect of moving the viewport as I desire, but the CSS3DRenderer layer does not shift.

To Reproduce

Please see the attached zip file.

THREE.js Bug Report.zip

Expected behavior

The setViewOffset should apply to the CSS3DRenderer.

Screenshots

This is what I expect:

Example1

This is what I get:

Example1

I have included a modified version of “CSS3DRenderer.js” that almost corrects the problem and which I used to generate the “good” image. It works by applying an additional transform to the domElement, like so:

				if (camera.view && camera.view.enabled) {
					const style = 'translate(' + (-camera.view.offsetX) + 'px,' + (-camera.view.offsetY) + 'px)';
					if ( cache.domElement.style !== style ) {
						domElement.style.transform = style;
						cache.domElement.style = style;
					}
				}

While this works when the CSS elements are at the center of the page, it works by shifting the entire parent DOM element and causes cropping of the CSS elements to happen on the edges of the screen when the camera is close up, as seen in this screen shot:

Example3

I tried to work the translation into the cameraCSSMatrix, which seemed like a better place to do this, but I could not get it to work right. I am by no means an expert in CSS transforms. I would love it if someone could propose a better fix and incorporate it into THREE.js

Platform:

  • Device: Desktop
  • OS: Windows
  • Browser: Chrome
  • Three.js version: dev

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Mugen87commented, Jul 21, 2021

Reopening this when more users request the feature.

2reactions
yomotsucommented, May 12, 2021

If we shift cameraCSSMatrix with view offset, CSS perspective-origin should be shifted too. (The default value of perspective-origin is 50% 50%, which means looking from the center)

e.g:


cameraElement.style.transform += `translate( ${ - _viewOffset.x }px, ${ - _viewOffset.y }px)`;
domElement.style.perspectiveOrigin = `calc( 50% - ${ viewOffset.x }px ) calc( 50% - ${ viewOffset.y }px )`;

see the result: https://jsfiddle.net/4cy5atvq/


However, if the camera is rotated, the above will not work.

https://jsfiddle.net/grky2hxc/

Because cameraElement is just a plane that is projected the scene, and perspective-origin will be used projection, I guess. So, cameraElement can’t shift with view offset.


I think the best way is adding one more div as the view.

└domElement (as viewport)
+  └viewElement
    └cameraElement

Then shift the viewElement with CSS translate 2d. -> https://jsfiddle.net/Lv4f25o8/

What do you think? @Mugen87 @mrdoob

Read more comments on GitHub >

github_iconTop Results From Across the Web

PerspectiveCamera#setViewOffset – three.js docs
Camera that uses perspective projection. This projection mode is designed to mimic the way the human eye sees. It is the most common...
Read more >
renderer - Is there ANY way to have the three.js camera lookat ...
If you want your perspective camera to have an off-center view, the pattern you need to use is: camera = new THREE.PerspectiveCamera( for ......
Read more >
Getting started with the CSS 3D renderer | Three.js Cookbook
With the THREE.CSS3DRenderer object, we can directly access these CSS 3D features and transform an arbitrary HTML element in 3D space.
Read more >
Camera view offset - Questions - Babylon.js Forum
https://threejs.org/docs/#api/en/cameras/PerspectiveCamera.setViewOffset ^^link to three.js docs where this is explained.
Read more >
Introduction to Computer Graphics, Section 5.1 -- Three.js Basics
The examples use many features that are not part of the three.js core. These add-ons can be ... Typical code for creating a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found