Skip to content

feat: import spine-core by npm package instead of copy code #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ lib
temp
api
www
.husky
172 changes: 102 additions & 70 deletions plugin-packages/spine/demo/src/api-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck
import type { Camera, JSONValue } from '@galacean/effects';
import { Player } from '@galacean/effects';
import { Player, loadBinary } from '@galacean/effects';
import type { FileFormat } from './files';
import { direct, premultiply } from './files';
import type { SkeletonData } from '@galacean/effects-plugin-spine';
Expand All @@ -14,10 +14,7 @@ import {
import 'fpsmeter';

const playerOptions = {
pixelRatio: 2,
interactive: true,
onEnd: () => console.info('合成播放结束'),
env: 'editor',
};

const filetype = document.getElementById('J-premultiply')!;
Expand Down Expand Up @@ -47,7 +44,7 @@ const format = document.getElementById('J-formatList') as HTMLSelectElement;
format.onchange = handleChange;
delay.onchange = handleChange;

const files: Record<string, FileFormat> = direct;
const files: Record<string, FileFormat> = premultiply;

if (files === premultiply) {
filetype.innerText = '纹理打包选择预乘alpha: true';
Expand All @@ -56,7 +53,7 @@ if (files === premultiply) {
}

let selectedFile = 'mix';
let skeletonData: SkeletonData, activeSkin: string, activeAnimation: string, duration = 5;
let skeletonData: SkeletonData, activeSkin: string, duration = 5;
let file: FileFormat, comp, camera: Camera, scene: JSONValue, animationList: string[] = [];
const cameraPos = [0, 0, 8];

Expand All @@ -83,7 +80,6 @@ fileList.onchange = () => {
skin.options.length = 0;
animation.innerHTML = '';
activeSkin = '';
activeAnimation = '';
const sf = fileList.value;

selectedFile = sf;
Expand All @@ -96,16 +92,17 @@ startEle.onclick = async () => {
if (player.hasPlayable) {
player.pause();
}
if (!scene) {
scene = generateScene(skin.value, animationList, duration) as JSONValue;
player.destroyCurrentCompositions();
duration = 0.1;
for (const a of animationList) {
duration += getAnimationDuration(skeletonData, a);
}
scene = generateScene(skin.value, animationList, duration, Number(delay.value), Number(speed.value), Number(mixDuration.value)) as JSONValue;
const comp = await player.loadScene(scene);

void player.play();

camera = comp.camera;

console.info(`player play file ${selectedFile}, format:${format.value}, skin: ${activeSkin}, animation: ${activeAnimation}`);
console.info(`player play file ${selectedFile}, format:${format.value}, skin: ${activeSkin}, animation: ${animationList}`);
};

function initialFileList () {
Expand Down Expand Up @@ -135,9 +132,15 @@ function loadFile (fileName: string) {
async function loadSelectionData (file: FileFormat): Promise<[SkeletonData, string[], string[]]> {
const atlasText = await loadText(file.atlas);
const atlas = new TextureAtlas(atlasText);

const skeleton = await loadText(file.json);
const skeletonData = createSkeletonData(atlas, skeleton, 'json');
let skeleton, skeletonData;

if (format.value === 'json') {
skeleton = await loadText(file.json);
skeletonData = createSkeletonData(atlas, skeleton, 'json');
} else {
skeleton = await loadBinary(file.skeleton!);
skeletonData = createSkeletonData(atlas, new Uint8Array(skeleton), 'skel');
}

return [skeletonData, getSkinList(skeletonData), getAnimationList(skeletonData)];
}
Expand Down Expand Up @@ -197,16 +200,16 @@ function handleChange () {
scene = generateScene(as, animationList, duration, Number(delay.value)) as JSONValue;
}

function generateScene (activeSkin: string, activeAnimation: string[], duration: number, delay = 0) {
function generateScene (activeSkin: string, activeAnimation: string[], duration: number, delay = 0, speed = 1, mixDuration = 0) {
return {
'compositionId': 654110176,
'requires': [],
'bins': [{ url: file.atlas }, { url: format.value === 'json' ? file.json : file.skeleton }],
'textures': file.png.map((url, index) => ({
source: index,
name: url.slice(url.lastIndexOf('/') + 1),
flipY: false,
})),
'images': file.png.map(img => {
return {
'url': img,
'renderLevel': 'B+',
'oriY': 1,
};
}),
'fonts': [],
'spines': [
{
'atlas': [20, [0, 0]],
Expand All @@ -215,74 +218,95 @@ function generateScene (activeSkin: string, activeAnimation: string[], duration:
'images': file.png.map((item, index) => index),
},
],
'version': '1.8',
'shapes': [],
'plugins': [
'spine',
],
'type': 'mars',
'compositions': [
{
'name': '新建合成2',
'id': 654110176,
'duration': 5,
'endBehavior': 2,
'camera': {
'fov': 80,
'far': 1000,
'near': 1,
'aspect': 1,
'clipMode': 1,
'position': cameraPos,
'rotation': [0, 0, 0],
},
'id': '10',
'name': '新建合成',
'duration': 10,
'startTime': 0,
'endBehavior': 5,
'previewSize': [
750,
1624,
],
'items': [
{
'id': '104',
'name': 'spine_item',
delay,
'id': 4,
'pn': 0,
duration,
'type': 'spine',
'pluginName': 'spine',
'visible': true,
'endBehavior': 5,
delay,
'renderLevel': 'B+',
'content': {
'renderer': {
'renderMode': 1,
},
'transform': {
'position': [0, 0, 0],
'rotation': [0, 0, 0],
'scale': [1, 1, 1],
},
'options': {
'renderLevel': 'B+',
'looping': true,
'startSize': 6,
duration,
'endBehavior': 1,
activeSkin,
'spine': 0,
'size': [
3,
3,
],
'startSize': 3,
activeAnimation,
mixDuration,
speed,
},
},
'transform': {
'position': [
0,
0,
0,
],
'rotation': [
0,
0,
0,
],
'scale': [
1,
1,
1,
],
},
},
],
'meta': {
'previewSize': [
'camera': {
'fov': 60,
'far': 40,
'near': 0.1,
'clipMode': 1,
'position': [
0,
0,
8,
],
'rotation': [
0,
0,
0,
],
},
},
],
'gltf': [],
'images': [
...file.png,
],
'version': '0.9.0',
'shapes': [],
'plugins': [
'spine',
],
'type': 'mars',
'_imgs': {
'654110176': file.png.map((_, index) => index),
},
'requires': [],
'compositionId': '10',
'bins': [{ url: file.atlas }, { url: format.value === 'json' ? file.json : file.skeleton }],
'textures': file.png.map((url, index) => ({
source: index,
name: url.slice(url.lastIndexOf('/') + 1),
flipY: false,
})),
};
}

function setSkinList (list: string[]) {
const options = [];

Expand All @@ -299,7 +323,15 @@ function setSkinList (list: string[]) {

function setAnimationList (e: Event) {
if (e.target) {
animationList.push((e.target as HTMLInputElement).name);
const ele = e.target as HTMLInputElement;

if (!ele.checked) {
animationList.splice(animationList.indexOf(ele.name), 1);
}
if (ele.checked && !animationList.includes(ele.name)) {
animationList.push(ele.name);
}

}
}

Expand Down
15 changes: 15 additions & 0 deletions plugin-packages/spine/demo/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ export const premultiply: Record<string, FileFormat> = {
json: 'https://gw.alipayobjects.com/os/gltf-asset/66086090439549/tank-pro.json',
png: ['https://gw.alipayobjects.com/zos/gltf-asset/66086090439549/tank-pro.png'],
},
celestial: {
skeleton: 'https://gw.alipayobjects.com/os/gltf-asset/02293829268574/celestial-circus-pro.skel',
atlas: 'https://gw.alipayobjects.com/os/gltf-asset/02293829268574/celestial-circus-pma.atlas',
png: [
'https://gw.alipayobjects.com/zos/gltf-asset/02293829268574/celestial-circus-pma.png',
'https://gw.alipayobjects.com/zos/gltf-asset/02293829268574/celestial-circus-pma_2.png',
],
json: '',
},
labayu: {
json:'',
atlas: 'https://gw.alipayobjects.com/os/gltf-asset/02297022467078/labayu.atlas',
skeleton: 'https://gw.alipayobjects.com/os/gltf-asset/02297022467078/labayu-pro.skel',
png: ['https://gw.alipayobjects.com/zos/gltf-asset/02297022467078/labayu.png'],
},
};

export const direct: Record<string, FileFormat> = {
Expand Down
1 change: 0 additions & 1 deletion plugin-packages/spine/demo/src/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import type { spec } from '@galacean/effects';
import { Player } from '@galacean/effects';
import '@galacean/effects-plugin-spine';
import '@galacean/effects-plugin-orientation-transformer';
import type { SpineVFXItem } from '@galacean/effects-plugin-spine';
import { direct, premultiply } from './files';

Expand Down
3 changes: 3 additions & 0 deletions plugin-packages/spine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"devDependencies": {
"@galacean/effects": "workspace:*",
"fpsmeter": "^0.3.1"
},
"dependencies": {
"@esotericsoftware/spine-core": "~4.2.33"
}
}
Loading