Skip to content

Commit bef9807

Browse files
author
Avaer Kazmer
committed
Add initial label mesh support
1 parent 29a8e93 commit bef9807

File tree

1 file changed

+20
-111
lines changed

1 file changed

+20
-111
lines changed

app.html

Lines changed: 20 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@
12031203
layers.splice(layers.indexOf(xrIframe), 1);
12041204
xrIframe.destroy();
12051205
scene.remove(moveMesh);
1206+
scene.remove(moveMesh.labelMesh);
12061207
moveMeshes.splice(moveMeshes.indexOf(moveMesh), 1);
12071208
currentMoveMeshes[i] = null;
12081209
}
@@ -1769,118 +1770,13 @@
17691770
return object;
17701771
};
17711772

1772-
const _makeLabelMesh = s => {
1773-
const width = 2;
1774-
const height = 0.2;
1775-
const geometry = new THREE.PlaneBufferGeometry(width, height);
1776-
const labelVsh = `
1777-
varying vec2 vUv;
1778-
void main() {
1779-
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);
1780-
vUv = uv;
1781-
}
1782-
`;
1783-
const labelFsh = `
1784-
varying vec2 vUv;
1785-
// vec4 color = vec4(${new THREE.Color(0x1976d2).toArray().map(n => n.toFixed(8)).join(',')}, 1.0);
1786-
vec4 color = vec4(${new THREE.Color(0x333333).toArray().map(n => n.toFixed(8)).join(',')}, 1.0);
1787-
void main() {
1788-
if ((vUv.x < 0.005 || vUv.x > 0.995) || (vUv.y < 0.03 || vUv.y > 0.97)) {
1789-
// gl_FragColor = vec4(vec3(0.11055276381909548, 0.27638190954773867, 0.4103852596314908) * 2.0, 1.0);
1790-
gl_FragColor = color;
1791-
} else {
1792-
// gl_FragColor = vec4(vUv.x, vUv.y, 0.0, 0.5);
1793-
gl_FragColor = vec4(1., 1., 1., 1.);
1794-
}
1795-
}
1796-
`;
1797-
const material = new THREE.ShaderMaterial({
1798-
vertexShader: labelVsh,
1799-
fragmentShader: labelFsh,
1800-
transparent: true,
1801-
side: THREE.DoubleSide,
1802-
// depthWrite: false,
1773+
const _makeLabelMesh = () => {
1774+
const geometry = new THREE.PlaneBufferGeometry(1, 0.1);
1775+
const material = new THREE.MeshBasicMaterial({
1776+
color: 0x000080,
18031777
});
18041778
const mesh = new THREE.Mesh(geometry, material);
1805-
mesh.width = width;
1806-
mesh.height = height;
18071779
mesh.frustumCulled = false;
1808-
mesh.getGlyphs = () => {
1809-
const glyphs = textMesh.geometry.layout.glyphs.slice();
1810-
if (glyphs.length > 0) {
1811-
const lastGlyph = glyphs[glyphs.length - 1];
1812-
glyphs.push({x: lastGlyph.x + lastGlyph.data.width});
1813-
}
1814-
return glyphs;
1815-
};
1816-
mesh.plane = new THREE.Plane();
1817-
mesh.leftLine = new THREE.Line3();
1818-
mesh.topLine = new THREE.Line3();
1819-
mesh.update = () => {
1820-
const menuWorldWidth = width;
1821-
const menuWorldHeight = height;
1822-
mesh.leftLine.start
1823-
.set(-menuWorldWidth/2, menuWorldHeight/2, 0)
1824-
.applyMatrix4(mesh.matrixWorld);
1825-
mesh.leftLine.end
1826-
.set(-menuWorldWidth/2, -menuWorldHeight/2, 0)
1827-
.applyMatrix4(mesh.matrixWorld);
1828-
1829-
mesh.topLine.start
1830-
.set(-menuWorldWidth/2, menuWorldHeight/2, 0)
1831-
.applyMatrix4(mesh.matrixWorld);
1832-
mesh.topLine.end
1833-
.set(menuWorldWidth/2, menuWorldHeight / 2, 0)
1834-
.applyMatrix4(mesh.matrixWorld);
1835-
1836-
mesh.plane.setFromCoplanarPoints(
1837-
mesh.leftLine.start,
1838-
mesh.leftLine.end,
1839-
mesh.topLine.end
1840-
);
1841-
};
1842-
mesh.intersect = ray => {
1843-
const intersectionPoint = ray.intersectPlane(mesh.plane, localVector);
1844-
if (intersectionPoint) {
1845-
const leftIntersectionPoint = mesh.leftLine.closestPointToPoint(intersectionPoint, true, localVector2);
1846-
const topIntersectionPoint = mesh.topLine.closestPointToPoint(intersectionPoint, true, localVector3);
1847-
1848-
const xFactor = topIntersectionPoint.distanceTo(mesh.topLine.start) / mesh.width;
1849-
const yFactor = leftIntersectionPoint.distanceTo(mesh.leftLine.start) / mesh.height;
1850-
const distance = ray.origin.distanceTo(intersectionPoint);
1851-
1852-
if (xFactor > 0 && xFactor < 0.99999 && yFactor > 0 && yFactor < 0.99999 && distance < rayDistance) {
1853-
const xScale = mesh.width / mesh.textMesh.scale.x;
1854-
const x = xFactor * xScale;
1855-
const yScale = mesh.height;
1856-
const y = yFactor * yScale;
1857-
return {
1858-
type: 'label',
1859-
mesh,
1860-
distance,
1861-
cancel: true,
1862-
x,
1863-
y,
1864-
xScale,
1865-
yScale,
1866-
};
1867-
}
1868-
}
1869-
return null;
1870-
};
1871-
1872-
const textMesh = _makeTextMesh(s, 0x000000);
1873-
textMesh.position.x -= width/2;
1874-
// textMesh.position.x += -0.45;
1875-
textMesh.position.y += 0.02;
1876-
textMesh.position.z += 0.001;
1877-
mesh.add(textMesh);
1878-
mesh.textMesh = textMesh;
1879-
1880-
const caretMesh = _makeCaretMesh(mesh, 0x000000);
1881-
mesh.add(caretMesh);
1882-
mesh.caretMesh = caretMesh;
1883-
18841780
return mesh;
18851781
};
18861782

@@ -3520,6 +3416,10 @@
35203416
scene.add(moveMesh);
35213417
moveMeshes.push(moveMesh);
35223418
currentMoveMeshes[controllerIndex] = moveMesh;
3419+
3420+
const labelMesh = _makeLabelMesh();
3421+
scene.add(labelMesh);
3422+
moveMesh.labelMesh = labelMesh;
35233423
} /* else {
35243424
console.log('no match', intersectionSpec, new Error().stack);
35253425
} */
@@ -5541,8 +5441,10 @@
55415441
for (let i = 0; i < moveMeshes.length; i++) {
55425442
const moveMesh = moveMeshes[i];
55435443
moveMesh.scale.set(menuFactor, menuFactor, menuFactor);
5544-
moveMesh.updateMatrixWorld();
55455444
moveMesh.visible = true;
5445+
moveMesh.labelMesh.scale.set(menuFactor, menuFactor, menuFactor);
5446+
moveMesh.labelMesh.visible = true;
5447+
// moveMesh.updateMatrixWorld();
55465448
}
55475449
for (let i = 0; i < controllerMeshes.length; i++) {
55485450
const controllerMesh = controllerMeshes[i];
@@ -5573,7 +5475,9 @@
55735475
if (end.factor === 0) {
55745476
screenMesh.visible = false;
55755477
for (let i = 0; i < moveMeshes.length; i++) {
5576-
moveMeshes[i].visible = false;
5478+
const moveMesh = moveMeshes[i];
5479+
moveMesh.visible = false;
5480+
moveMesh.labelMesh.visible = false;
55775481
}
55785482
for (let i = 0; i < controllerMeshes.length; i++) {
55795483
controllerMeshes[i].rayMesh.visible = false;
@@ -5614,6 +5518,11 @@
56145518
moveMesh.scale.multiplyScalar(1 + (1-Math.cos(angle))*0.2);
56155519
}
56165520
}
5521+
5522+
const {labelMesh} = moveMesh;
5523+
labelMesh.position.copy(moveMesh.position).add(localVector.set(0, 1, 0));
5524+
const vrCamera = renderer.vr.enabled ? renderer.vr.getCamera(camera).cameras[0] : camera;
5525+
labelMesh.quaternion.copy(vrCamera.quaternion);
56175526
}
56185527
}
56195528
};

0 commit comments

Comments
 (0)