-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6eb18de
commit 5bc555a
Showing
10 changed files
with
392 additions
and
114 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,76 @@ | ||
import { useEffect, useState } from "preact/hooks"; | ||
import "./app.css"; | ||
import { Forma } from "forma-embedded-view-sdk/auto"; | ||
import { | ||
BufferAttribute, | ||
BufferGeometry, | ||
Mesh, | ||
MeshBasicMaterial, | ||
Scene, | ||
} from "three"; | ||
import { computeBoundsTree, disposeBoundsTree } from "three-mesh-bvh"; | ||
import { biggerGenerateDepthMap } from "./biggerDepthMapProcessor"; | ||
// Speed up raycasting using https://github.com/gkjohnson/three-mesh-bvh | ||
// @ts-ignore | ||
BufferGeometry.prototype.computeBoundsTree = computeBoundsTree; | ||
// @ts-ignore | ||
BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree; | ||
|
||
export function RenderDepthMap() { | ||
const [allBufferGeometry, setAllBufferGeometry] = | ||
useState<BufferGeometry | null>(null); | ||
|
||
useEffect(() => { | ||
Forma.geometry | ||
.getTriangles({ | ||
path: "root", | ||
}) | ||
.then((terrainVertexPositions) => { | ||
const allGeometry = new BufferGeometry(); | ||
allGeometry.setAttribute( | ||
"position", | ||
new BufferAttribute(terrainVertexPositions, 3) | ||
); | ||
setAllBufferGeometry(allGeometry); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (allBufferGeometry) { | ||
console.time("everything"); | ||
const allScene = new Scene(); | ||
|
||
allScene.add(new Mesh(allBufferGeometry, new MeshBasicMaterial())); | ||
biggerGenerateDepthMap(allScene, [0, 0]).then((depthMap) => { | ||
const imageData = new ImageData( | ||
new Uint8ClampedArray(depthMap.data), | ||
depthMap.width, | ||
depthMap.height | ||
); | ||
createImageBitmap(imageData, { imageOrientation: "flipY" }).then( | ||
(imageBitmap) => { | ||
const canvas = document.createElement("canvas"); | ||
const ctx = canvas.getContext("2d")!; | ||
canvas.height = depthMap.height; | ||
canvas.width = depthMap.width; | ||
ctx.drawImage(imageBitmap, 0, 0); | ||
Forma.terrain.groundTexture.add({ | ||
name: "test", | ||
canvas, | ||
position: { x: 0, y: 0, z: 30 }, | ||
scale: { x: depthMap.resolution, y: depthMap.resolution }, | ||
}); | ||
ctx.restore(); | ||
console.timeEnd("everything"); | ||
} | ||
); | ||
}); | ||
} | ||
}, [allBufferGeometry]); | ||
|
||
return ( | ||
<> | ||
<div>Hello</div> | ||
</> | ||
); | ||
} |
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
Oops, something went wrong.