Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
めたねのおくすり専用アバターを読み込めるようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
calmery committed Feb 27, 2021
1 parent 3d11764 commit e9140a7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/components/Exhibition/3d/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ControllerKeys } from "./Controller";
import { useMultiplay } from "~/hooks/exhibition/useMultuplay";
import { Area, AreaName, AreaObject } from "~/types/exhibition";
import { getGltf, rewriteMaterials } from "~/utils/exhibition";
import { getMetanenoToken } from "~/utils/exhibition/metaneno";

CameraControls.install({ THREE });

Expand Down Expand Up @@ -129,7 +130,7 @@ export const Exhibition3dPlayer = React.memo<

accessory.position.set(
scene?.position.x || 0,
(scene?.position.y || 0) + 0.95,
(scene?.position.y || 0) + (getMetanenoToken() ? 1.21 : 0.95),
scene?.position.z || 0
);

Expand All @@ -152,7 +153,9 @@ export const Exhibition3dPlayer = React.memo<

useEffect(() => {
(async () => {
const { animations, scene } = await getGltf(url);
const { animations, scene } = await getGltf(
getMetanenoToken() ? "/objects/metaneno.glb" : url
);

scene.scale.set(0.5, 0.5, 0.5);

Expand Down Expand Up @@ -284,7 +287,7 @@ export const Exhibition3dPlayer = React.memo<
if (accessory) {
accessory.position.set(
nextPosition.x,
nextPosition.y + 0.95,
nextPosition.y + (getMetanenoToken() ? 1.21 : 0.95),
nextPosition.z
);
accessory.rotation.y = Math.atan2(rotation.x, rotation.z);
Expand Down
30 changes: 16 additions & 14 deletions src/components/Exhibition/3d/Players.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { getGltf, rewriteMaterials } from "~/utils/exhibition";

type S = Scene & {
accessory: "fried_egg" | "pancake" | null;
hasAccessory: boolean;
accessoryScene: Scene | null;
metaneno: boolean;
mixer: AnimationMixer;
nextPosition: Vector3 | null;
lerpAlpha: number;
Expand Down Expand Up @@ -49,7 +50,6 @@ const applyPlayerTransform = async (scene: S, payload: UpdateResponse) => {

if (!scene.ready) {
scene.accessory = null;
scene.hasAccessory = false;
scene.mixer = new AnimationMixer(scene);
scene.nextPosition = null;
scene.position.set(
Expand All @@ -72,23 +72,18 @@ const applyPlayerTransform = async (scene: S, payload: UpdateResponse) => {
// Accessory

if (scene.accessory !== payload.accessory) {
if (scene.hasAccessory) {
scene.children = scene.children.slice(0, scene.children.length - 1);
}

if (payload.accessory === "pancake" || payload.accessory === "fried_egg") {
const { scene: accessory } = await getGltf(
`/objects/accessories/${payload.accessory}.glb`
);
rewriteMaterials(accessory);
accessory.position.set(
scene.position.x || 0,
(scene.position.y || 0) + 0.95,
(scene.position.y || 0) + (scene.metaneno ? 1.21 : 0.95),
scene.position.z || 0
);
scene.children.push(accessory);
scene.accessoryScene = accessory;
scene.accessory = payload.accessory;
scene.hasAccessory = true;
}
}

Expand Down Expand Up @@ -128,10 +123,10 @@ const applyPlayerTransform = async (scene: S, payload: UpdateResponse) => {
const position = scene.position.lerp(nextPosition, scene.lerpAlpha);
scene.lerpAlpha += ALPFA;

if (scene.hasAccessory) {
scene.children[scene.children.length - 1].position.set(
if (scene.accessoryScene) {
scene.accessoryScene.position.set(
position.x,
position.y + 0.95,
position.y + (scene.metaneno ? 1.21 : 0.95),
position.z
);
}
Expand Down Expand Up @@ -188,9 +183,13 @@ export const Exhibition3dPlayers = React.memo<{
});
} else {
const { scene } = await getGltf(
`/objects/${payload.metaneno ? "" : "other_"}player.glb`
`/objects/${
payload.metaneno ? "metaneno" : "other_player"
}.glb`
);

(scene as S).metaneno = !!payload.metaneno;

return Promise.resolve({
[playerId]: await applyPlayerTransform(scene as S, payload),
});
Expand All @@ -211,7 +210,10 @@ export const Exhibition3dPlayers = React.memo<{
return (
<>
{Object.values(scenes).map((scene) => (
<primitive key={scene.id} object={scene} />
<React.Fragment key={scene.id}>
<primitive object={scene} />
{scene.accessoryScene && <primitive object={scene.accessoryScene} />}
</React.Fragment>
))}
</>
);
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/exhibition/useMultuplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from "react";
import { io } from "socket.io-client";
import { AreaName } from "~/types/exhibition";
import * as GA from "~/utils/exhibition/google-analytics";
import { getMetanenoToken } from "~/utils/exhibition/metaneno";

export type UpdateResponse = UpdatePayload & {
metaneno: boolean;
Expand Down Expand Up @@ -47,7 +48,7 @@ export const useMultiplay = () => {
//

useEffect(() => {
const token = localStorage.getItem("_metaneno");
const token = getMetanenoToken();

if (token) {
if (socket.connected) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/exhibition/metaneno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getMetanenoToken = () => localStorage.getItem("_metaneno");

0 comments on commit e9140a7

Please sign in to comment.