skinview3d/examples/stories/demo.story.js

157 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-07-29 02:08:07 +02:00
import * as skinview3d from "../../libs/skinview3d";
2020-07-16 03:56:05 +02:00
import { createViewer } from "./util";
2020-07-29 02:08:07 +02:00
import { radios, withKnobs, optionsKnob, number } from "@storybook/addon-knobs";
2020-07-16 03:56:05 +02:00
let { viewer, element } = createViewer();
2020-07-29 02:08:07 +02:00
let currentAnimation;
2020-07-16 03:56:05 +02:00
const bodyParts = viewer.playerObject.skin;
const skinParts = {};
// set inner parts
skinParts.head = bodyParts.head.innerLayer;
skinParts.body = bodyParts.body.innerLayer;
skinParts.leftArm = bodyParts.leftArm.innerLayer;
skinParts.rightArm = bodyParts.rightArm.innerLayer;
skinParts.leftLeg = bodyParts.leftLeg.innerLayer;
skinParts.rightLeg = bodyParts.rightLeg.innerLayer;
// set outter parts
skinParts.head2 = bodyParts.head.outerLayer;
skinParts.body2 = bodyParts.body.outerLayer;
skinParts.leftArm2 = bodyParts.leftArm.outerLayer;
skinParts.rightArm2 = bodyParts.rightArm.outerLayer;
skinParts.leftLeg2 = bodyParts.leftLeg.outerLayer;
skinParts.rightLeg2 = bodyParts.rightLeg.outerLayer;
export default {
title: "Skinview3d",
decorators: [withKnobs],
};
2020-07-29 02:08:07 +02:00
const handleSizeControl = () => {
const width = number("Canvas Width", 300);
const height = number("Canvas Height", 400);
viewer.setSize(width, height);
};
const handleTextureControl = () => {
2020-07-16 03:56:05 +02:00
const skinOptions = {
"1.8 Skin": "1_8_texturemap_redux",
"Classic Skin": "Hacksore",
"HD Skin": "ironman_hd",
};
const skinUrl = radios("Skin Textures", skinOptions, "1_8_texturemap_redux");
const capeOptions = {
None: "none",
"Classic Cape": "cape",
"HD Cape": "hd_cape",
};
const capeUrl = radios("Cape Textures", capeOptions, "none");
viewer.loadSkin(`textures/${skinUrl}.png`);
if (capeUrl === "none") {
viewer.loadCape(null);
} else {
viewer.loadCape(`textures/${capeUrl}.png`);
}
// bottom layers
const valuesBottomLayer = {
Head: "head",
Body: "body",
LeftArm: "leftArm",
RightArm: "rightArm",
RightLeg: "rightLeg",
LeftLeg: "leftLeg",
};
const optionsBottomLayer = optionsKnob(
"Bottom Layer",
valuesBottomLayer,
Object.values(valuesBottomLayer),
{
display: "inline-check",
}
);
// bottom layers
const valuesTopLayer = {
Head2: "head2",
Body2: "body2",
LeftArm2: "leftArm2",
RightArm2: "rightArm2",
RightLeg2: "rightLeg2",
LeftLeg2: "leftLeg2",
};
const optionsTopLayer = optionsKnob(
"Top Layer",
valuesTopLayer,
Object.values(valuesTopLayer),
{
display: "inline-check",
}
);
// toggle parts
console.log(skinParts);
for (let partName in skinParts) {
const visible = optionsBottomLayer
.concat(optionsTopLayer)
.includes(partName);
skinParts[partName].visible = visible;
}
2020-07-29 02:08:07 +02:00
};
const handleAnimationControl = () => {
if (currentAnimation) {
currentAnimation.remove();
}
const animationMap = {
None: null,
Walk: skinview3d.WalkingAnimation,
Run: skinview3d.RunningAnimation,
Rotate: skinview3d.RotatingAnimation,
};
const animationKey = radios("Animations", Object.keys(animationMap), "Run");
console.log("animationKey", animationKey);
if (currentAnimation || animationKey === "None") {
currentAnimation.resetAndRemove();
}
currentAnimation = viewer.animations.add(animationMap[animationKey]);
const label = "Speed";
const defaultValue = 0.5;
const options = {
range: true,
min: 0.1,
max: 2,
step: 0.01,
};
const value = number(label, defaultValue, options);
currentAnimation.speed = value;
};
export const Component = () => {
handleSizeControl();
handleTextureControl();
handleAnimationControl();
2020-07-16 03:56:05 +02:00
return element;
};
2020-07-29 02:08:07 +02:00
Component.story = {
name: "Demo",
};