-
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.
Merge pull request #47 from bananu7/gltf-models
Gltf models
- Loading branch information
Showing
9 changed files
with
142 additions
and
26 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 @@ | ||
*.glb |
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,71 @@ | ||
import { useLoader, useFrame } from '@react-three/fiber' | ||
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader' | ||
|
||
import * as THREE from 'three'; | ||
import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils'; | ||
import { useRef, useEffect, useState, useLayoutEffect, memo } from 'react' | ||
|
||
export type FileModelProps = { | ||
path: string, | ||
accentColor: THREE.ColorRepresentation, | ||
animate: boolean, // TODO which anims how fast etc | ||
} | ||
|
||
const ACCENT_MATERIAL_NAME = 'Accent'; | ||
|
||
function FileModel_(props: FileModelProps) { | ||
const gltf = useLoader(GLTFLoader, props.path) | ||
|
||
const origAccentMaterial = gltf.materials[ACCENT_MATERIAL_NAME]; | ||
const accentMaterial = (() => { | ||
if (!origAccentMaterial || !(origAccentMaterial instanceof THREE.MeshStandardMaterial)) | ||
return undefined; | ||
const accentMaterial = origAccentMaterial.clone(); | ||
accentMaterial.color.set(props.accentColor); | ||
return accentMaterial; | ||
})(); | ||
|
||
const ref = useRef<THREE.Group>(null); | ||
|
||
let mixer: THREE.AnimationMixer | null = null; | ||
useFrame((state, delta) => { | ||
if(!ref.current) | ||
return; | ||
|
||
if(gltf.animations.length === 0) { | ||
return; | ||
} | ||
|
||
if (!mixer) { | ||
mixer = new THREE.AnimationMixer(ref.current); | ||
const moveAction = gltf.animations.find(a => a.name === "Move"); | ||
if (moveAction) { | ||
mixer.clipAction(moveAction).play(); | ||
} | ||
} | ||
|
||
if (props.animate) | ||
mixer.update(delta); | ||
}); | ||
|
||
const clonedObject = SkeletonUtils.clone(gltf.scene); | ||
clonedObject.traverse(o => { | ||
if (o instanceof THREE.Mesh) { | ||
if (o.material.name === ACCENT_MATERIAL_NAME) | ||
o.material = accentMaterial; | ||
o.castShadow = true; | ||
o.receiveShadow = true; | ||
} | ||
}); | ||
|
||
return ( | ||
<group | ||
ref={ref} | ||
rotation={[0, 0, 0]} | ||
> | ||
<primitive object={clonedObject} /> | ||
</group> | ||
) | ||
} | ||
|
||
export const FileModel = memo(FileModel_); |
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
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,29 @@ | ||
interface UnitDisplayCatalog { | ||
[kind: string]: () => { | ||
modelPath: string; | ||
selectorSize: number; | ||
}; | ||
} | ||
|
||
export const UNIT_DISPLAY_CATALOG : UnitDisplayCatalog = { | ||
'Harvester': () => ({ | ||
modelPath: 'peasant_1.glb', | ||
selectorSize: 1, | ||
}), | ||
'Base': () => ({ | ||
modelPath: 'castle_1.glb', | ||
selectorSize: 6, | ||
}), | ||
'ResourceNode': () => ({ | ||
modelPath: 'gold_node.glb', | ||
selectorSize: 1.8, | ||
}), | ||
'Barracks': () => ({ | ||
modelPath: 'castle_1.glb', | ||
selectorSize: 6, | ||
}), | ||
'Trooper': () => ({ | ||
modelPath: 'catapult.glb', | ||
selectorSize: 2.5, | ||
}) | ||
}; |
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