Added uniforms.

This commit is contained in:
Michael Mainguy 2023-09-20 08:37:05 -05:00
parent 31b87e26ae
commit ad3c8db41d
2 changed files with 16 additions and 5 deletions

View File

@ -5,12 +5,14 @@ import {
Mesh, Mesh,
MeshBuilder, MeshBuilder,
Observable, Observable,
PBRMaterial,
PhysicsAggregate, PhysicsAggregate,
PhysicsMotionType, PhysicsMotionType,
PhysicsShapeType, PhysicsShapeType,
Scene, Scene,
SceneLoader, SceneLoader,
Skeleton, Skeleton,
Texture,
Vector2, Vector2,
Vector3 Vector3
} from "@babylonjs/core"; } 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}`); 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 forward = true;
private destination: Vector2; 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.scene = scene;
this.position = position; this.position = position;
this.number = number; this.number = number;
this.teamName = teamName; this.teamName = teamName;
const data = container.instantiateModelsToScene(undefined, false, {doNotInstantiate: true}); const data = container.instantiateModelsToScene(undefined, false, {doNotInstantiate: true});
this.mesh = (data.rootNodes[0] as Mesh); 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.skeleton = data.skeletons[0];
this.animationGroup = data.animationGroups[6]; this.animationGroup = data.animationGroups[6];
this.buildPlayer(); this.buildPlayer();

View File

@ -1,5 +1,5 @@
import {Player, PlayerFactory} from "./player"; import {Player, PlayerFactory} from "./player";
import {Scene, Vector2, Vector3} from "@babylonjs/core"; import {Scene, Texture, Vector2, Vector3} from "@babylonjs/core";
export class Team { export class Team {
private readonly scene: Scene; private readonly scene: Scene;
@ -21,11 +21,14 @@ export class Team {
]; ];
private name: string; private name: string;
private uniforms: Texture[] = [];
constructor(scene: Scene, side: number = 1, name: string = "team") { constructor(scene: Scene, side: number = 1, name: string = "team") {
this.scene = scene; this.scene = scene;
this.goalSide = side; this.goalSide = side;
this.name = name; 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 = new PlayerFactory(this.scene);
this.playerFactory.onReadyObservable.add(() => { this.playerFactory.onReadyObservable.add(() => {
this.buildTeam(); this.buildTeam();
@ -37,6 +40,7 @@ export class Team {
for (let i = 0; i < 11; i++) { for (let i = 0; i < 11; i++) {
const player = this.playerFactory const player = this.playerFactory
.buildPlayer(new Vector3(this.positions[i].x * this.goalSide, 1, this.positions[i].y * this.goalSide), i, .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); this.name);
player.lookAt(new Vector2(0, -50 * this.goalSide)) player.lookAt(new Vector2(0, -50 * this.goalSide))
this.players.push(player); this.players.push(player);