From ad3c8db41dcafec747576e627352085d3c5ba789 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Wed, 20 Sep 2023 08:37:05 -0500 Subject: [PATCH] Added uniforms. --- src/soccer/player.ts | 13 ++++++++++--- src/soccer/team.ts | 8 ++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/soccer/player.ts b/src/soccer/player.ts index 5ab9b9c..2a6f901 100644 --- a/src/soccer/player.ts +++ b/src/soccer/player.ts @@ -5,12 +5,14 @@ import { Mesh, MeshBuilder, Observable, + PBRMaterial, PhysicsAggregate, PhysicsMotionType, PhysicsShapeType, Scene, SceneLoader, Skeleton, + Texture, Vector2, Vector3 } from "@babylonjs/core"; @@ -35,9 +37,9 @@ export class PlayerFactory { } - public buildPlayer(position: Vector3, number: number, teamName: string = "team"): Player { + public buildPlayer(position: Vector3, number: number, uniformTexture: Texture = null, teamName: string = "team"): Player { this.logger.debug(`Building player #${number}, for team ${teamName}`); - return new Player(this.scene, position, this.container, number, teamName); + return new Player(this.scene, position, this.container, number, uniformTexture, teamName); } } @@ -57,13 +59,18 @@ export class Player { private forward = true; private destination: Vector2; - constructor(scene: Scene, position: Vector3, container: AssetContainer, number: number = 0, teamName: string = "team") { + constructor(scene: Scene, position: Vector3, container: AssetContainer, number: number = 0, texture: Texture = null, teamName: string = "team") { this.scene = scene; this.position = position; this.number = number; this.teamName = teamName; const data = container.instantiateModelsToScene(undefined, false, {doNotInstantiate: true}); this.mesh = (data.rootNodes[0] as Mesh); + if (texture) { + ((this.mesh.getChildren()[0].getChildren()[2] as Mesh).material as PBRMaterial).albedoTexture = texture; +// (this.mesh.material as PBRMaterial).albedoTexture = texture; + } + this.skeleton = data.skeletons[0]; this.animationGroup = data.animationGroups[6]; this.buildPlayer(); diff --git a/src/soccer/team.ts b/src/soccer/team.ts index e4da896..068daa0 100644 --- a/src/soccer/team.ts +++ b/src/soccer/team.ts @@ -1,5 +1,5 @@ import {Player, PlayerFactory} from "./player"; -import {Scene, Vector2, Vector3} from "@babylonjs/core"; +import {Scene, Texture, Vector2, Vector3} from "@babylonjs/core"; export class Team { private readonly scene: Scene; @@ -21,11 +21,14 @@ export class Team { ]; private name: string; - + private uniforms: Texture[] = []; constructor(scene: Scene, side: number = 1, name: string = "team") { this.scene = scene; this.goalSide = side; this.name = name; + this.uniforms.push(new Texture("/assets/textures/team1.png", this.scene)) + this.uniforms.push(new Texture("/assets/textures/team2.png", this.scene)) + this.playerFactory = new PlayerFactory(this.scene); this.playerFactory.onReadyObservable.add(() => { this.buildTeam(); @@ -37,6 +40,7 @@ export class Team { for (let i = 0; i < 11; i++) { const player = this.playerFactory .buildPlayer(new Vector3(this.positions[i].x * this.goalSide, 1, this.positions[i].y * this.goalSide), i, + this.uniforms[this.goalSide == 1 ? 0 : 1], this.name); player.lookAt(new Vector2(0, -50 * this.goalSide)) this.players.push(player);