-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
6 changed files
with
171 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
104 changes: 104 additions & 0 deletions
104
packages/spelunker-web/src/components/core/MapViewer/index.jsx
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,104 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { AssetManager, FormatManager, TextureManager, MapManager, OrbitControls } from '@wowserhq/scene'; | ||
import * as THREE from 'three'; | ||
import styles from './index.styl'; | ||
|
||
// Maps use z-up coordinates | ||
THREE.Object3D.DEFAULT_UP.set(0, 0, 1); | ||
|
||
// We do our own color management! | ||
THREE.ColorManagement.enabled = false; | ||
|
||
const assetManager = new AssetManager(process.env.DATA_URI, true); | ||
const formatManager = new FormatManager(assetManager); | ||
const textureManager = new TextureManager(formatManager); | ||
|
||
const MapViewer = ({ map: { filename } }) => { | ||
const canvasRef = useRef(); | ||
const mapManagerRef = useRef(); | ||
|
||
useEffect(() => { | ||
if (filename && canvasRef.current) { | ||
const canvasWidth = canvasRef.current.clientWidth; | ||
const canvasHeight = canvasRef.current.clientHeight; | ||
|
||
const camera = new THREE.PerspectiveCamera( | ||
60, | ||
canvasWidth / canvasHeight, | ||
0.1, | ||
1277.0, | ||
); | ||
camera.position.set(100.0, 100.0, 100.0); | ||
|
||
const clock = new THREE.Clock(); | ||
|
||
const renderer = new THREE.WebGLRenderer({ | ||
antialias: true, | ||
alpha: false, | ||
canvas: canvasRef.current, | ||
}); | ||
renderer.setSize(canvasWidth, canvasHeight); | ||
|
||
const clearColor = new THREE.Color(0.25, 0.5, 0.8); | ||
renderer.setClearColor(clearColor, 1.0); | ||
|
||
const scene = new THREE.Scene(); | ||
scene.matrixAutoUpdate = false; | ||
|
||
const controls = new OrbitControls(camera, renderer.domElement); | ||
controls.enableDamping = true; | ||
controls.dampingFactor = 0.25; | ||
controls.minDistance = 10; | ||
controls.maxDistance = 900; | ||
controls.panSpeed = 5.0; | ||
controls.keyPanSpeed = 20.0; | ||
controls.zoomSpeed = 5.0; | ||
controls.screenSpacePanning = false; | ||
controls.update(); | ||
|
||
const raycaster = new THREE.Raycaster(); | ||
const coords = new THREE.Vector2(); | ||
const targetOffset = 100.0; | ||
|
||
const mapManager = new MapManager(filename, formatManager, textureManager); | ||
mapManagerRef.current = mapManager; | ||
|
||
scene.add(mapManager.root); | ||
|
||
let rafId; | ||
|
||
const animate = () => { | ||
const delta = clock.getDelta(); | ||
|
||
raycaster.setFromCamera(coords, camera); | ||
raycaster.ray.at(targetOffset, controls.target); | ||
|
||
mapManager.setTarget(camera.position.x, camera.position.y); | ||
|
||
controls.update(delta); | ||
mapManager.update(delta, camera); | ||
|
||
renderer.render(scene, camera); | ||
|
||
rafId = requestAnimationFrame(animate); | ||
}; | ||
|
||
animate(); | ||
|
||
return () => { | ||
cancelAnimationFrame(rafId); | ||
|
||
renderer.dispose(); | ||
controls.dispose(); | ||
}; | ||
} | ||
}, [filename]); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<canvas ref={canvasRef} className={styles.viewer} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MapViewer; |
12 changes: 12 additions & 0 deletions
12
packages/spelunker-web/src/components/core/MapViewer/index.styl
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,12 @@ | ||
.container { | ||
width: 100% | ||
padding-bottom: 56.25%; | ||
border-radius: 5px; | ||
position: relative; | ||
} | ||
|
||
.viewer { | ||
position: absolute; | ||
width: 100%; | ||
height: 100%; | ||
} |
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