Skip to content

Commit

Permalink
Merge pull request #47 from bananu7/gltf-models
Browse files Browse the repository at this point in the history
Gltf models
  • Loading branch information
bananu7 authored Mar 18, 2023
2 parents 79d61ba + f66b44b commit 314d8d3
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 26 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

- name: Set outputs
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Build client
working-directory: ./packages/client
Expand All @@ -50,7 +50,6 @@ jobs:

docker-server:
needs: build
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -60,7 +59,7 @@ jobs:

- name: Set version string
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Store version to a file
run: echo "${{ steps.vars.outputs.sha_short }}" > ./packages/server/version.txt
Expand All @@ -76,5 +75,5 @@ jobs:
uses: docker/build-push-action@v3
with:
context: ./
push: true
push: ${{ github.ref == 'refs/heads/main' && github.event_name != 'pull_request' }}
tags: ${{ secrets.DOCKERHUB_USERNAME }}/rts:server, ${{ secrets.DOCKERHUB_USERNAME }}/rts:server-${{ steps.vars.outputs.sha_short }}
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ COPY ./packages/client/src ./src
COPY ./packages/client/public ./public
COPY ./packages/client/index.html ./

# get assets
RUN git clone https://github.com/bananu7/rts-assets.git --depth 1
RUN cp ./rts-assets/models/**/*.glb ./public/

RUN yarn build


################################
# second stage - run
FROM node:18
Expand Down
1 change: 1 addition & 0 deletions packages/client/public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.glb
71 changes: 71 additions & 0 deletions packages/client/src/gfx/FileModel.tsx
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_);
19 changes: 16 additions & 3 deletions packages/client/src/gfx/Map3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,24 @@ export function Map3D(props: Map3DProps) {
const isPassable = props.map.tiles[ix] === 0;

const color = isPassable ? 0x11aa11 : 0x888888;
const height = isPassable ? 0 : 0.8;
const height = (isPassable ? 0 : 0.8 + Math.random() * 0.7) - 0.01; // TODO quick hack

mat4Pos.makeTranslation(x * xSize, height, y * ySize);
mat4Pos.makeTranslation(x * xSize, height, y * ySize); // TODO -1 to move them down because of their height
vec3Color.set(color);

// TODO - this is just a quick and dirty solution
if (isPassable) {
const f = 0.06;
vec3Color.r += (Math.random() - 0.5) * f;
vec3Color.g += (Math.random() - 0.5) * f;
vec3Color.b += (Math.random() - 0.5) * f;
} else {
const d = (Math.random() - 0.5) * 0.1;
vec3Color.r += d;
vec3Color.g += d;
vec3Color.b += d;
}

ref.current.setMatrixAt(ix, mat4Pos);
ref.current.setColorAt(ix, vec3Color);
}
Expand Down Expand Up @@ -114,7 +127,7 @@ export function Map3D(props: Map3DProps) {
receiveShadow
>
{/*<planeGeometry args={[xSize, ySize]} />*/}
<boxGeometry args={[xSize, 1, ySize]} />
<boxGeometry args={[xSize, 2, ySize]} />
<meshStandardMaterial />
</instancedMesh>
</group>
Expand Down
4 changes: 1 addition & 3 deletions packages/client/src/gfx/SelectionCircle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const friendlyMaterial = new THREE.MeshBasicMaterial({
color: 0x00ff00
});

const ringGeometry = new THREE.RingGeometry(1, 1.1, 32);

export function SelectionCircle(props: { size: number, enemy?: boolean }) {
const innerRadius = props.size;
const outerRadius = props.size * 1.1;
Expand All @@ -30,8 +28,8 @@ export function SelectionCircle(props: { size: number, enemy?: boolean }) {
name="SelectionRing"
position={[0, 0, 0]}
material={props.enemy ? enemyMaterial : friendlyMaterial}
geometry={ringGeometry}
>
<ringBufferGeometry args={[innerRadius, outerRadius, segments]} />
</mesh>
);
}
25 changes: 12 additions & 13 deletions packages/client/src/gfx/Unit3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {

import * as THREE from 'three';

//import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
//import { SkeletonUtils } from "three/examples/jsm/utils/SkeletonUtils"

import { Board, Unit, GameMap, UnitId, Position, UnitState } from 'server/src/types'
import { SelectionCircle } from './SelectionCircle'
import { Line3D } from './Line3D'
import { Map3D, Box } from './Map3D'
import { ThreeCache } from './ThreeCache'
import { FileModel } from './FileModel'
import { UNIT_DISPLAY_CATALOG } from './UnitDisplayCatalog'

import { Horizon } from '../debug/Horizon'

Expand Down Expand Up @@ -90,10 +89,13 @@ export function Unit3D(props: Unit3DProps) {

const color = ownerToColor(props.unit.owner);

// TODO proper unit catalog
const isBuilding = props.unit.kind === 'Base' || props.unit.kind === 'Barracks';
const unitSize = isBuilding ? 4 : 1;
const selectorSize = isBuilding ? 3 : 1;
const unitCatalogEntry = UNIT_DISPLAY_CATALOG[props.unit.kind];
if (!unitCatalogEntry)
throw new Error("Unit doesn't exist in catalog");

const unitDetails = unitCatalogEntry();
const modelPath = unitDetails.modelPath;
const selectorSize = unitDetails.selectorSize;

// smoothing
const unitGroupRef = useRef<THREE.Group>(null);
Expand Down Expand Up @@ -162,6 +164,8 @@ export function Unit3D(props: Unit3DProps) {
return new THREE.Vector3(a.x, 1, a.y);
});

const animate = props.unit.status === 'Moving';

return (
<group>
{ debugFlags.showPaths &&
Expand Down Expand Up @@ -198,12 +202,7 @@ export function Unit3D(props: Unit3DProps) {
<ConeIndicator unit={props.unit} smoothing={smoothingVelocity.x > 0.01 || smoothingVelocity.y > 0.01} />
}

<mesh
castShadow
receiveShadow
geometry={cache.getBoxGeometry(unitSize)}
material={cache.getStandardMaterial(color)}
/>
<FileModel path={modelPath} accentColor={color} animate={animate}/>
</group>
</group>
</group>
Expand Down
29 changes: 29 additions & 0 deletions packages/client/src/gfx/UnitDisplayCatalog.ts
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,
})
};
7 changes: 4 additions & 3 deletions packages/client/src/gfx/View3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ function MapSpotlight() {
angle={0.16}
distance={0}
decay={0}
intensity={6}
intensity={4}
color={0xffffff}

castShadow
shadow-camera-near={300}
shadow-camera-far={500}
shadow-mapSize-height={1024}
shadow-mapSize-width={1024}
shadow-bias={-0.002}
/>
</group>
)
Expand Down Expand Up @@ -123,7 +124,7 @@ export function View3D(props: Props) {
<Suspense fallback={null}>
<div style={style} >
<Canvas
camera={{ fov: 60, near: 0.1, far: 2000, up:[0,1,0], position: [50, 90, 90] }}
camera={{ fov: 27.8, near: 10, far: 500, up:[0,1,0], position: [50, 60, 90] }}
gl={{
physicallyCorrectLights: true,
pixelRatio: window.devicePixelRatio,
Expand All @@ -136,7 +137,7 @@ export function View3D(props: Props) {
>
<color attach="background" args={[0x11aa11]} />
<CameraControls />
<ambientLight />
<ambientLight args={[0xffffff, 2]} />
<MapSpotlight />
{props.children}
<Stats />
Expand Down

0 comments on commit 314d8d3

Please sign in to comment.