Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from gritsenko/skeleton-instance-per-addon-ins…
Browse files Browse the repository at this point in the history
…tance

Skeleton instance per addon instance, RenderQuality, Texture size.
  • Loading branch information
MikalDev authored Aug 8, 2020
2 parents f43967d + 1e75202 commit 63e0766
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 66 deletions.
Binary file added dist/Spine-v1.8.0.c3addon
Binary file not shown.
16 changes: 16 additions & 0 deletions src/aces.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@
"highlight": false,
"returnType": "string",
"params": []
},
{
"id": "texture-width",
"expressionName": "TextureWidth",
"scriptName": "TextureWidth",
"highlight": false,
"returnType": "number",
"params": []
},
{
"id": "texture-height",
"expressionName": "TextureHeight",
"scriptName": "TextureHeight",
"highlight": false,
"returnType": "number",
"params": []
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "plugin",
"name": "Spine",
"id": "Gritsenko_Spine",
"version": "1.7.0",
"version": "1.8.0",
"author": "Igor Gritsenko and Mikal",
"website": "https://gritsenko.github.io/c3_spine_plugin",
"documentation": "https://gritsenko.github.io/c3_spine_plugin",
Expand Down
9 changes: 7 additions & 2 deletions src/c3runtime/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@

Error(){
return this.spineError;
}

},
TextureHeight(){
return this.textureHeight;
},
TextureWidth(){
return this.textureWidth;
},

};
}
118 changes: 57 additions & 61 deletions src/c3runtime/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
this.premultipliedAlpha = properties[7];
this.collisionsEnabled = properties[8];
this.defaultMix = properties[9];
this.skeletonRenderQuality = properties[10];
}

this.isMirrored = false;
Expand All @@ -55,6 +56,8 @@
this.spineError = null
this.animationSpeed = 1.0
this.completeEventName = ""
this.textureWidth = 0;
this.textureHeight = 0;

const wi = this.GetWorldInfo();
// Enable collisions based on property, add ACEs if needed
Expand All @@ -76,17 +79,17 @@
// Context already exists and we want to use (for render to texture)
// XXX Can't change existing context attributes (though we may want to for PMA)
// var config = { alpha: false };
var config = {}
let config = {}
this.gl = this.canvas.getContext("webgl2", config) || this.canvas.getContext("webgl", config) || canvas.getContext("experimental-webgl", config);
var gl = this.gl
let gl = this.gl
if (!gl) {
alert('WebGL is unavailable.');
return;
}

var version = 0;
let version = 0;
this.isWebGL2 = false;
var glVersion = gl.getParameter( gl.VERSION );
let glVersion = gl.getParameter( gl.VERSION );

if ( glVersion.indexOf( 'WebGL' ) !== - 1 )
{
Expand All @@ -100,8 +103,6 @@
}

// Init Spine elements
// Disable PMA when loading Spine textures
spine.webgl.GLTexture.DISABLE_UNPACK_PREMULTIPLIED_ALPHA_WEBGL = true;
this.mvp = new spine.webgl.Matrix4();
this.shader = spine.webgl.Shader.newTwoColoredTextured(gl);
this.batcher = new spine.webgl.PolygonBatcher(gl);
Expand All @@ -111,23 +112,21 @@
this.assetManager = new spine.SharedAssetManager();
this.bgColor = new spine.Color(0.0, 0.0, 0.0, 0.0);

// console.log(gl)

this.pngURI = await globalThis.c3_runtimeInterface._localRuntime._assetManager.GetProjectFileUrl(this.pngPath);
this.atlasURI = await globalThis.c3_runtimeInterface._localRuntime._assetManager.GetProjectFileUrl(this.atlasPath);
this.jsonURI = await globalThis.c3_runtimeInterface._localRuntime._assetManager.GetProjectFileUrl(this.jsonPath);

// console.log("LOADING SPINE STUFF");

var textureLoader = function(img) { return new spine.webgl.GLTexture(gl, img); };
this.assetManager.loadJson(this.DEMO_NAME, this.jsonURI);
this.assetManager.loadTexture(this.DEMO_NAME, textureLoader, this.pngURI);
this.assetManager.loadText(this.DEMO_NAME, this.atlasURI);
if (this._sdkType._skeletonData.notInitialized)
{
// console.log("[Spine] Loading textures, json, atlas");
// Only load textures once for creation of skeletonData, not for each instance
// Disable PMA when loading Spine textures
spine.webgl.GLTexture.DISABLE_UNPACK_PREMULTIPLIED_ALPHA_WEBGL = true;
this.pngURI = await globalThis.c3_runtimeInterface._localRuntime._assetManager.GetProjectFileUrl(this.pngPath);
this.atlasURI = await globalThis.c3_runtimeInterface._localRuntime._assetManager.GetProjectFileUrl(this.atlasPath);
this.jsonURI = await globalThis.c3_runtimeInterface._localRuntime._assetManager.GetProjectFileUrl(this.jsonPath);
let textureLoader = function(img) { return new spine.webgl.GLTexture(gl, img); };
this.assetManager.loadJson(this.DEMO_NAME, this.jsonURI);
this.assetManager.loadTexture(this.DEMO_NAME, textureLoader, this.pngURI);
this.assetManager.loadText(this.DEMO_NAME, this.atlasURI);
}
this.isSpineInitialized = true;

// Restore PMA format to C3 state
// XXX Can not be reset here, causes PMA texture load to be incorrect
// gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,true);

// console.log("Spine renderer initialized");
}
Expand All @@ -146,16 +145,12 @@
var height = (bounds.size.y) * scale;

this.mvp.ortho2d(centerX - width / 2, centerY - height / 2, width, height);
// XXX will resize c3 canvas, if FB bound to C3 canvas
// this.gl.viewport(0, 0, bounds.size.x, bounds.size.y);
// only do in render function where it can be restored
// console.log('[Spine] bounds:',bounds)
}

loadSkeletons() {
// console.log("Loading skeleton");

// this.skeletonInfo = this.loadSkeleton("hero_human_female", this.animationName);
// XXX hack to allow different skeletons loaded by oveloading skinName
this.skeletonInfo = this.loadSkeleton(this.skeletonName, this.animationName);

const skins = this.skeletonInfo.skeleton.data.skins;
Expand All @@ -169,49 +164,42 @@

// console.log("Skeleton loaded");

// XXX Do not resize C3 canvas
// XXX May need to delete and resize texture buffer that is created instead?
this.resize();
// console.log(this.skeletonInfo)

this.Trigger(C3.Plugins.Gritsenko_Spine.Cnds.OnSkeletonLoaded);
// this.Trigger(C3.Plugins.Gritsenko_Spine.Cnds.OnSkeletonLoaded);
}

loadSkeleton(name, animationName, sequenceSlots) {
const assetManager = this.assetManager;

const self = this;
const atlasURI = assetManager.get(this.DEMO_NAME, this.atlasURI);

// console.log("Loading atlas");

var atlas = new spine.TextureAtlas(atlasURI, function(path) {
// console.log(`Loading png atlas ${path} replaced with ${self.pngURI}`);
return assetManager.get(self.DEMO_NAME, self.pngURI);
});


var atlasLoader = new spine.AtlasAttachmentLoader(atlas);

// console.log("Loading json");

var skeletonJson = new spine.SkeletonJson(atlasLoader);

// console.log("Reading skeleton data");

// XXX JSON file with one skeleton, no name?
if (this.skeletonName == "")
// If skeletonData not initialized, create it and stop other instances from creating it
if (this._sdkType._skeletonData.notInitialized)
{
var skeletonData = skeletonJson.readSkeletonData(assetManager.get(this.DEMO_NAME, this.jsonURI) /*[name]*/ );
} else
{
var skeletonData = skeletonJson.readSkeletonData(assetManager.get(this.DEMO_NAME, this.jsonURI) [name] );
console.log('[Spine] Create skeletonData');
this._sdkType._skeletonData.notInitialized = false;
const atlasURI = assetManager.get(this.DEMO_NAME, this.atlasURI);
this._sdkType._atlas = new spine.TextureAtlas(atlasURI, function(path) {
// console.log(`Loading png atlas ${path} replaced with ${self.pngURI}`);
return assetManager.get(self.DEMO_NAME, self.pngURI);
});
this._sdkType._atlasLoader = new spine.AtlasAttachmentLoader(this._sdkType._atlas);
let skeletonJson = new spine.SkeletonJson(this._sdkType._atlasLoader);
skeletonJson.scale = this.skeletonRenderQuality;
// JSON file with one skeleton, no name
if (this.skeletonName == "")
{
this._sdkType._skeletonData = skeletonJson.readSkeletonData(assetManager.get(this.DEMO_NAME, this.jsonURI));
} else
{
this._sdkType._skeletonData = skeletonJson.readSkeletonData(assetManager.get(this.DEMO_NAME, this.jsonURI) [name] );
}
}

// console.log("creating skeleton");

var skeleton = new spine.Skeleton(skeletonData);
//skeleton.setSkinByName(this.skinName);
var skeleton = new spine.Skeleton(this._sdkType._skeletonData);
let subskin = skeleton.data.findSkin(this.skinName);
if (subskin === undefined) {
subskin = skeleton.data.skins[0];
Expand All @@ -221,7 +209,7 @@

if (this.debug) console.log("Loading state");

let stateData = new spine.AnimationStateData(skeletonData);
let stateData = new spine.AnimationStateData(this._sdkType._skeletonData);
stateData.defaultMix = this.defaultMix;

var state = new spine.AnimationState(stateData);
Expand All @@ -233,15 +221,15 @@
skeleton.getBounds(offset, size, []);

return {
atlas: atlas,
atlas: this._sdkType._atlas,
skeleton: skeleton,
state: state,
playTime: 0,
bounds: {
offset: offset,
size: size
},
atlasLoader : atlasLoader
atlasLoader : this._sdkType._atlasLoader
};
}

Expand Down Expand Up @@ -474,6 +462,7 @@

Release() {
super.Release();
if (this.c3renderer && this._elementTexture) this.c3renderer.DeleteTexture(this._elementTexture);
this.DEMO_NAME = null;
this.canvas = null;
this.bgColor = null;
Expand All @@ -499,7 +488,7 @@
this.isMirrored = null;
this._elementId = null;
this._elementTexture = null
this._newElementId = fnull;
this._newElementId = null;
this.pngURI = null;
this.atlasURI = null;
this.jsonURI = null;
Expand All @@ -512,6 +501,9 @@
this.spineError = null
this.animationSpeed = null;
this.completeEventName = null;
this.skeletonRenderQuality = null;
this.textureWidth = null;
this.textureHeight = null;
}

Tick() {
Expand Down Expand Up @@ -563,7 +555,10 @@
this._newElementId = false;

var bounds = this.skeletonInfo.bounds;
this._elementTexture = renderer.CreateDynamicTexture(bounds.size.x, bounds.size.y, { mipMap: false });
this.textureWidth = bounds.size.x;
this.textureHeight = bounds.size.y;
this._elementTexture = renderer.CreateDynamicTexture(this.textureWidth, this.textureHeight, { mipMap: false });
// console.log('[Spine] Texture size:',bounds.size.x,',', bounds.size.y);

var oldFrameBuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
// Create FB and bind texture to spineFB
Expand All @@ -576,6 +571,7 @@
// Restore render to the canvas
gl.bindFramebuffer(gl.FRAMEBUFFER, oldFrameBuffer);
// console.log("Created dynamic texture for spine:" + this._elementId);
this.Trigger(C3.Plugins.Gritsenko_Spine.Cnds.OnSkeletonLoaded);
}

// Render skeleton
Expand Down
1 change: 1 addition & 0 deletions src/c3runtime/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
OnCreate()
{
this.GetImageInfo().LoadAsset(this._runtime);
this._skeletonData = {'notInitialized' : true}
}

LoadTextures(renderer)
Expand Down
14 changes: 14 additions & 0 deletions src/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"default-mix": {
"name": "Default mix",
"desc": "Set default mix time between animations (s)"
},
"render-quality": {
"name": "Render quality",
"desc": "Render quality (1.0 is normal), upsamples (above 1.0) or downsamples (below 1.0) the skeleton render from the original size."
}
},
"aceCategories": {
Expand Down Expand Up @@ -256,6 +260,16 @@
}
}
},
"texture-width": {
"description": "Spine rendered texture width.",
"translated-name": "TextureWidth",
"params": {}
},
"texture-height": {
"description": "Spine rendered texture height.",
"translated-name": "TextureHeight",
"params": {}
},
"spine-error": {
"description": "Error message for on error trigger.",
"translated-name": "Error",
Expand Down
5 changes: 3 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{

const PLUGIN_ID = "Gritsenko_Spine";
const PLUGIN_VERSION = "1.6.0";
const PLUGIN_VERSION = "1.8.0";
const PLUGIN_CATEGORY = "general";

const PLUGIN_CLASS = SDK.Plugins.Gritsenko_Spine = class SpinePlugin extends SDK.IPluginBase {
Expand Down Expand Up @@ -56,7 +56,8 @@
new SDK.PluginProperty("float", "scale", 1),
new SDK.PluginProperty("check", "pre-mult-alpha", false),
new SDK.PluginProperty("check", "collisions", true),
new SDK.PluginProperty("float", "default-mix", 0.0)
new SDK.PluginProperty("float", "default-mix", 0.0),
new SDK.PluginProperty("float", "render-quality", 1.0)
]);
SDK.Lang.PopContext(); //.properties
SDK.Lang.PopContext();
Expand Down

0 comments on commit 63e0766

Please sign in to comment.