Skip to content

Commit

Permalink
fix: Handles now maintain screen size in orthographic viewports.
Browse files Browse the repository at this point in the history
  • Loading branch information
phtaylor authored and phtaylor committed Nov 25, 2022
1 parent aa16706 commit 4e44551
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@rollup/plugin-node-resolve": "^13.0.6",
"@zeainc/jsdocs2md": "^0.0.7",
"@zeainc/zea-collab": "^6.0.1",
"@zeainc/zea-engine": "^4",
"@zeainc/zea-engine": "4.6.0",
"canvas": "^2.6.1",
"copyfiles": "^2.4.1",
"cypress": "^5.6.0",
Expand Down
38 changes: 28 additions & 10 deletions src/Handles/Shaders/HandleShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ attribute vec2 texCoords;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform lowp int isOrthographic;
uniform vec4 viewportFrustum;
#ifdef ENABLE_MULTI_DRAW
<%include file="materialparams.glsl"/>
Expand Down Expand Up @@ -94,15 +96,31 @@ void main(void) {
}
mat4 modelViewMatrix = viewMatrix * modelMatrix;
if (maintainScreenSize != 0) {
float dist = modelViewMatrix[3][2];
float sc = abs(dist); // Note: items in front of the camera will have a negative value here.
mat4 scmat = mat4(
sc, 0.0, 0.0, 0.0,
0.0, sc, 0.0, 0.0,
0.0, 0.0, sc, 0.0,
0.0, 0.0, 0.0, 1.0
);
modelViewMatrix = modelViewMatrix * scmat;
if (isOrthographic > 0){
// At a distance of 1, we should match the orthographic and perspective sizes.
// Calculate the size of the handle in perpective projection at 1 meter.
// With a 24mm camera, the Fov is 46.4 degrees, or 0.8098327729253689 radians.
// (0.81 / 2.0) * focalDist * 2.0
// The frustum height at 1m is 0.81.
float sc = viewportFrustum.y / 0.8098327;
mat4 scmat = mat4(
sc, 0.0, 0.0, 0.0,
0.0, sc, 0.0, 0.0,
0.0, 0.0, sc, 0.0,
0.0, 0.0, 0.0, 1.0
);
modelViewMatrix = modelViewMatrix * scmat;
} else {
float dist = modelViewMatrix[3][2];
float sc = abs(dist); // Note: items in front of the camera will have a negative value here.
mat4 scmat = mat4(
sc, 0.0, 0.0, 0.0,
0.0, sc, 0.0, 0.0,
0.0, 0.0, sc, 0.0,
0.0, 0.0, 0.0, 1.0
);
modelViewMatrix = modelViewMatrix * scmat;
}
}
vec4 viewPos = modelViewMatrix * vec4(positions, 1.0);
Expand Down Expand Up @@ -142,7 +160,7 @@ uniform int BaseColorTexType;
#elif defined(DRAW_GEOMDATA)
uniform int isOrthographic;
uniform lowp int isOrthographic;
import 'surfaceGeomData.glsl'
#elif defined(DRAW_HIGHLIGHT)
Expand Down
14 changes: 11 additions & 3 deletions testing-e2e/xfo-handle.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,23 @@
translationXfoHandle.setTargetParam(translationTreeItem.getParameter('GlobalXfo'))
translationXfoHandle.showHandles('Translate')

const camera = renderer.getViewport().getCamera()
// {{{ Messages handler.
const handleMessage = (event) => {
const { data } = event

if (data === 'front') {
renderer.getViewport().getCamera().setPositionAndTarget(new Vec3(1, 1, 1.7), new Vec3(0, 0, 0))
if (data === 'orthographic') {
camera.setIsOrthographic(1)
window.postMessage(`done-${data}`)
}
if (data === 'perspective') {
camera.setIsOrthographic(0)
window.postMessage(`done-${data}`)
} else if (data === 'front') {
camera.setPositionAndTarget(new Vec3(1, 1, 1.7), new Vec3(0, 0, 0))
window.postMessage(`done-${data}`)
} else if (data === 'back') {
renderer.getViewport().getCamera().setPositionAndTarget(new Vec3(-1, -1, 1.7), new Vec3(0, 0, 0))
camera.setPositionAndTarget(new Vec3(-1, -1, 1.7), new Vec3(0, 0, 0))
window.postMessage(`done-${data}`)
}
}
Expand Down
11 changes: 5 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2182,12 +2182,11 @@
socket.io-client "^2.3.0"
socketio-wildcard "^2.0.0"

"@zeainc/zea-engine@^4":
version "4.2.0"
resolved "https://registry.yarnpkg.com/@zeainc/zea-engine/-/zea-engine-4.2.0.tgz#d2b997d21866f82cbc8d236cfc4e3b6edeb6c214"
integrity sha512-rM8sLbvMo35mg80NZw88KPDnGSSAQWMznSDndrQIkPjhUOsWFj/hVn2w8e1SW0L6D9sFR0YMgMeBxGIG6SGSdg==
"@zeainc/zea-engine@4.6.0":
version "4.6.0"
resolved "https://registry.yarnpkg.com/@zeainc/zea-engine/-/zea-engine-4.6.0.tgz#9ada50c892d5033e780d61dc658d08664a6235f9"
integrity sha512-xbt6u5tZLeJCG081pQkHOnE8pDu1FdmIPWgPdvVat5lA9yCsby7eNgk2fxKdcU3E7UIDlSmiB4um5s6dQR3BJg==
dependencies:
caniuse-lite "^1.0.30001309"
lodash-es "^4.17.21"

JSONStream@^1.0.3, JSONStream@^1.0.4:
Expand Down Expand Up @@ -3074,7 +3073,7 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001033, caniuse-lite@^1.0.30001309, caniuse-lite@^1.0.30001312:
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001033, caniuse-lite@^1.0.30001312:
version "1.0.30001312"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001312.tgz#e11eba4b87e24d22697dae05455d5aea28550d5f"
integrity sha512-Wiz1Psk2MEK0pX3rUzWaunLTZzqS2JYZFzNKqAiJGiuxIjRPLgV6+VDPOg6lQOUxmDwhTlh198JsTTi8Hzw6aQ==
Expand Down

0 comments on commit 4e44551

Please sign in to comment.