-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add measurement utils tutorial
- Loading branch information
Showing
6 changed files
with
137 additions
and
18 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
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,34 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<link rel="stylesheet" href="../../../resources/styles.css"> | ||
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> | ||
<link rel="icon" type="image/x-icon" href="../../../resources/favicon.ico"> | ||
<title>Measurement Utils</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-family: "Plus Jakarta Sans", sans-serif; | ||
} | ||
|
||
.full-screen { | ||
width: 100vw; | ||
height: 100vh; | ||
position: relative; | ||
overflow: hidden; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div class="full-screen" id="container"></div> | ||
<script type="module" src="./example.ts"></script> | ||
</body> | ||
|
||
</html> |
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,86 @@ | ||
/* eslint import/no-extraneous-dependencies: 0 */ | ||
|
||
import * as THREE from "three"; | ||
import Stats from "stats.js"; | ||
import * as OBC from "../.."; | ||
|
||
const container = document.getElementById("container")!; | ||
|
||
const components = new OBC.Components(); | ||
|
||
const worlds = components.get(OBC.Worlds); | ||
|
||
const world = worlds.create< | ||
OBC.SimpleScene, | ||
OBC.SimpleCamera, | ||
OBC.SimpleRenderer | ||
>(); | ||
|
||
world.scene = new OBC.SimpleScene(components); | ||
world.renderer = new OBC.SimpleRenderer(components, container); | ||
world.camera = new OBC.SimpleCamera(components); | ||
|
||
components.init(); | ||
|
||
world.camera.controls.setLookAt(12, 6, 8, 0, 0, -10); | ||
|
||
world.scene.setup(); | ||
|
||
const grids = components.get(OBC.Grids); | ||
grids.create(world); | ||
|
||
const fragments = new OBC.FragmentsManager(components); | ||
|
||
const file = await fetch("../../../../../resources/small.frag"); | ||
const data = await file.arrayBuffer(); | ||
const buffer = new Uint8Array(data); | ||
const model = fragments.load(buffer); | ||
world.scene.three.add(model); | ||
|
||
const measurements = components.get(OBC.MeasurementUtils); | ||
|
||
const casters = components.get(OBC.Raycasters); | ||
const caster = casters.get(world); | ||
|
||
const edges = new THREE.EdgesGeometry(); | ||
const material = new THREE.LineBasicMaterial({ | ||
color: 0xff0000, | ||
depthTest: false, | ||
}); | ||
const line = new THREE.LineSegments(edges, material); | ||
world.scene.three.add(line); | ||
|
||
if (world.renderer) { | ||
const canvas = world.renderer.three.domElement; | ||
canvas.addEventListener("mousemove", () => { | ||
const result = caster.castRay([model]); | ||
|
||
if (!result) return; | ||
if (!(result.object instanceof THREE.Mesh)) return; | ||
if (result.faceIndex === undefined) return; | ||
|
||
const face = measurements.getFace( | ||
result.object, | ||
result.faceIndex, | ||
result.instanceId, | ||
); | ||
|
||
if (face) { | ||
const points: THREE.Vector3[] = []; | ||
for (const edge of face.edges) { | ||
points.push(...edge.points); | ||
} | ||
edges.setFromPoints(points); | ||
} | ||
}); | ||
} | ||
|
||
// Set up stats | ||
const stats = new Stats(); | ||
stats.showPanel(2); | ||
document.body.append(stats.dom); | ||
stats.dom.style.left = "0px"; | ||
stats.dom.style.right = "auto"; | ||
|
||
world.renderer.onBeforeUpdate.add(() => stats.begin()); | ||
world.renderer.onAfterUpdate.add(() => stats.end()); |
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