-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from andrewisen-tikab/feature
Add `labels`
- Loading branch information
Showing
8 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
body { | ||
margin: 0; | ||
} | ||
|
||
.shape-3d-css-renderer { | ||
pointer-events: none; | ||
} | ||
|
||
.shape-3d-label-container { | ||
pointer-events: none; | ||
position: relative; | ||
} | ||
|
||
.shape-3d-label { | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
pointer-events: auto !important; | ||
z-index: 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js'; | ||
import { Vertex } from '../../types'; | ||
import { getLength2D, getMidpointOffsetFromLine } from '../../utils'; | ||
|
||
function addPrefix(this: HTMLInputElement, _ev: Event) { | ||
this.setAttribute('size', `${this.value!.length}`); | ||
} | ||
|
||
export const generateLabel = ( | ||
parent: THREE.Object3D, | ||
firstVertex: Vertex, | ||
secondVertex: Vertex, | ||
center: Vertex, | ||
offsetDistance: number, | ||
) => { | ||
const offset = getMidpointOffsetFromLine(firstVertex, secondVertex, center, offsetDistance); | ||
const length = getLength2D(firstVertex, secondVertex); | ||
|
||
const divElement = document.createElement('div'); | ||
divElement.className = 'shape-3d-label-container'; | ||
|
||
const inputElement = document.createElement('input'); | ||
inputElement.type = 'text'; | ||
|
||
inputElement.id = 'myInput'; | ||
inputElement.className = 'shape-3d-label'; | ||
inputElement.placeholder = `${length.toFixed(2)}`; | ||
inputElement.setAttribute('size', `${inputElement.getAttribute('placeholder')!.length}`); | ||
inputElement.oninput = addPrefix.bind(inputElement); | ||
|
||
divElement.appendChild(inputElement); | ||
const label = new CSS2DObject(divElement); | ||
label.position.set(offset[0], offset[1], offset[2]); | ||
|
||
parent.add(label); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { default as Shape3D } from './Shape3D'; | ||
export * from './controls/TransformShapeControls'; | ||
export * from './controls/TransformShapeControls/TransformShapeControls'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,78 @@ | ||
import * as THREE from 'three'; | ||
import type { Vertex } from './types'; | ||
|
||
export const getMidpoint = (firstVertex: Vertex, secondVertex: Vertex): Vertex => [ | ||
(firstVertex[0] + secondVertex[0]) / 2, | ||
(firstVertex[1] + secondVertex[1]) / 2, | ||
(firstVertex[2] + secondVertex[2]) / 2, | ||
]; | ||
|
||
const _midpoint = new THREE.Vector3(); | ||
const _center = new THREE.Vector3(); | ||
|
||
/** | ||
* Offset a midpoint from a center. | ||
* @param midpoint | ||
* @param center | ||
* @param offsetDistance | ||
* @returns | ||
*/ | ||
export const getMidpointOffsetFromCenter = ( | ||
midpoint: Vertex, | ||
center: Vertex, | ||
offsetDistance = 1, | ||
): Vertex => { | ||
_midpoint.fromArray(midpoint); | ||
_center.fromArray(center); | ||
const displacement = _midpoint.clone().sub(_center).normalize().multiplyScalar(offsetDistance); | ||
return _midpoint.clone().add(displacement).toArray(); | ||
}; | ||
|
||
const _firstVertex = new THREE.Vector3(); | ||
const _secondVertex = new THREE.Vector3(); | ||
const _line = new THREE.Vector3(); | ||
const _perpendicular = new THREE.Vector3(); | ||
const _up = new THREE.Vector3(0, 1, 0); | ||
|
||
export const getMidpointOffsetFromLine = ( | ||
firstVertex: Vertex, | ||
secondVertex: Vertex, | ||
center: Vertex, | ||
offsetDistance = 1, | ||
) => { | ||
_firstVertex.fromArray(firstVertex); | ||
_secondVertex.fromArray(secondVertex); | ||
_center.fromArray(center); | ||
|
||
_line.subVectors(_secondVertex, _firstVertex); | ||
_midpoint.addVectors(_firstVertex, _secondVertex).multiplyScalar(0.5); | ||
|
||
_perpendicular.crossVectors(_up, _line).normalize(); | ||
|
||
const distance1 = _center.distanceTo( | ||
_midpoint.clone().add(_perpendicular.multiplyScalar(offsetDistance)), | ||
); | ||
|
||
const distance2 = _center.distanceTo( | ||
_midpoint.clone().add(_perpendicular.multiplyScalar(-offsetDistance)), | ||
); | ||
|
||
_midpoint.add( | ||
_perpendicular.multiplyScalar(distance1 > distance2 ? -offsetDistance : offsetDistance), | ||
); | ||
|
||
return _midpoint.toArray(); | ||
}; | ||
|
||
/** | ||
* Return the length of the line segment in 2D space. | ||
* @param firstVertex | ||
* @param secondVertex | ||
* @returns | ||
*/ | ||
export const getLength2D = (firstVertex: Vertex, secondVertex: Vertex): number => { | ||
return Math.sqrt( | ||
Math.pow(firstVertex[0] - secondVertex[0], 2) + | ||
Math.pow(firstVertex[2] - secondVertex[2], 2), | ||
); | ||
}; |