pixijs / spine

Pixi.js plugin that enables Spine support.
Other
564 stars 217 forks source link

Read atlas from cache. #224

Closed congtung10t2 closed 6 years ago

congtung10t2 commented 6 years ago

the last time what I did is here: https://github.com/pixijs/pixi-spine/pull/222 but i see at L6664, it's still read atlas file again. but I'm not sure this will read from cache or read from file as new one. I guess it read as new one. I stored atlas file at host so every skeleton should read atlas once instead of read again. How can I do it? (just for optimization). As I see, it read png file once, but it keep reading atlas file again.

congtung10t2 commented 6 years ago

should we take out the atlas loading from json loading? then we can load atlas first. When we load a json file, we just point it to atlas file. i mean we will add atlas file to loader. then we can load it independent. it related to this: https://github.com/pixijs/pixi-spine/issues/161

ivanpopelyshev commented 6 years ago

Please look again, there's a way to specify whole atlas. Once you load atlas, then you load all your json files. I'm busy right now, I'll respond later.

congtung10t2 commented 6 years ago

I saw we already have a way to load image first and json later. but it's not good to me. cause what i want is load .atlas file directly instead of each attachment.

ivanpopelyshev commented 6 years ago

Try this one: https://github.com/pixijs/pixi-spine/blob/master/examples/preload_atlas_image.md#how-to-use-generated-or-preloaded-base-textures

Just preload necessary textures and then load all spine objects.

congtung10t2 commented 6 years ago

thanks ivan but what i want is we load from .atlas file. cause I have big game, i cannot load 1 by 1 image. and what i get from artist is atlas file with a lot of image inside, it's not a single image

ivanpopelyshev commented 6 years ago

Those two images are pages of same atlas, you just need an object with "page_0" and your atlas.

I still didnt look in your issue, I'm very busy. I'm just suggesting existing solutions.

congtung10t2 commented 6 years ago

ok. my point is optimization. I want combine many animation to use 1 atlas file with just read atlas 1 times. At your suggest, you load many images, that mean it will increase number of file. I don't want to increase number of file.

ivanpopelyshev commented 6 years ago

It works with one image. If you cant understand how to do it based on https://github.com/pixijs/pixi-spine/blob/master/examples/preload_atlas_image.md#how-to-use-generated-or-preloaded-base-textures , you have to wait when I have free time to make a demo for you.

congtung10t2 commented 6 years ago

hmm. look like we have some mistake. what i want is simple like: I have an atlas file: abc.atlas then i have 2 json file: 123.json 345.json now i want read abc.atlas file first, added to loader. How can i do it? (atlas file export from spine2d). after that, how can I do to load skeleton file with that atlas already done loaded. Sorry for my bad english make you can't understand :(

ivanpopelyshev commented 6 years ago
loader.add('atlasImage', 'abc.png', (loader, resource) => {
    // i dont remember if first paramer is actually loader here
    loader.add('spine1', { metadata: { metadata_from_that_example_for_abc_png })
        .add (...)
});

loader.load();

If you want more information - please wait for weekend. Yes, 'atlas' file will be loaded multiple times in that case, but it will have only one image.

ivanpopelyshev commented 6 years ago

Also, if you have custom logic for loading, please just copy my Spine loader and make your own. You have to understand how it works anyway.

ivanpopelyshev commented 6 years ago

You can preload whole atlas and then specify it with {metadata: {spineAtlas: atlas}}

congtung10t2 commented 6 years ago

this is what i want: https://github.com/pixijs/pixi-spine/pull/229. I preload whole atlas text. then I create skeletonData from raw atlas text file. Thank you for your kind support

ivanpopelyshev commented 6 years ago

Good job!

Congratulations on becoming pixi-spine contributor.

pixi-spine npm package version 1.5.13.

Do you want to add example for other people, as well?

congtung10t2 commented 6 years ago

ok I will do it. thanks @ivanpopelyshev

congtung10t2 commented 6 years ago

@ivanpopelyshev after you merged it https://github.com/pixijs/pixi-spine/pull/232 we can close this issue as well. thanks

lancety commented 2 years ago

hi @ivanpopelyshev , after reading these commonts and confirmed I am using the #229 version code, I have a question about a issue caused by the duplicated atlas loading

pixiApp.loader.add(spineName, spineUrl)
            .load(function (loader, resources) {
                const spine = new Spine(resources[spineName].spineData);

                spine.skeleton.setSkinByName('default');
                spine.skeleton.setSlotsToSetupPose();

                _setAnimation(spine, defaultAnim);
                resolve(spine);
            })

I am using above code to load and create Spine. By checking the logic with several breakpoint, I can see the sequence is

It is ok it blocks loading the duplicated load of atlas, but with that exception, the ".load" callback of above sample code wont be triggered. Is there any solution to make the above callback work? Cheers~~

ivanpopelyshev commented 2 years ago

Sorry, you have to debug it yourself, Im currently away from the project. Please ask at https://github.com/pixijs/pixijs/discussions

Yes, there is a solution, no, i dont remember where was it. If you find, make PR to put it in examples so other people will know :)

Alsu current loader is very bad for multiple file resources, @GoodBoyDigital is making new system for it. Until then, you have to stick to debugging and modifying middlewares

lancety commented 2 years ago

I will make workaround fix for now. Thanks for quick response~ :)

congtung10t2 commented 2 years ago

@lancety: Hello, here is my example for preload atlas text seperate. it has been working fine 4 years ago but not sure still work now or not. https://github.com/pixijs/spine/blob/master/examples/preload_atlas_text.md Kindly choose to use option 1

lancety commented 2 years ago

thanks for quick response @congtung10t2 , my understanding about the example is - the options allow me to pre load atlas. then use it with json to load rest. I am trying to use load callback to create spine when it is finish loading. but no matter which way I use, it finally will call "createSkeletonWithRawAtlas" in #229 , and "createSkeletonWithRawAtlas" will later call calbacks of assetName.atlas again, which causing the exception, which fail the callback in '.load'

I made a noob solution by using interval and try catch as a quick fix ;D

        // the duplicated atlas load exception is throwed at async time, 
        // so this try catch wont really catch it, can be removed
        try {
            pixiApp.loader.add(spineName, spineUrl);
            pixiApp.loader.load();
        } catch (err) {
            console.error(err);
        }
        const interval = setInterval(()=>{
            const resource: iPixiLoaderResource = pixiApp.loader.resources[spineName];
            if (resource.spineDataReady) {
                const spine = new Spine(resource.spineData);

                spine.skeleton.setSkinByName('default');
                spine.skeleton.setSlotsToSetupPose();

                _setAnimation(spine, defaultAnim);
                clearInterval(interval);
                resolve(spine);
            }
        }, 50)
lancety commented 2 years ago

I just reviewed the example #232 again, I guess the purpose of this example is saying - load json and atlas data first, then use them to make spine like this link, right? https://github.com/pixijs/spine/blob/master/examples/preloaded_json.md

congtung10t2 commented 2 years ago

Not sure I understand you but step 1 just for loading raw text. Step 2 will load json as the animation. Example: There are 5 animations (5 json) using same atlas file, you need to load the atlas file first. Step 2 will do 5 times with 5 json file. it means atlas - json is 1-n relationship. Hope it will help you. I'm not sure about the example you give me. It can be another problem.

congtung10t2 commented 2 years ago

the purpose of loading raw atlas text is reducing the number of request to the same atlas file. Which cause the traffic for server. Not too much functionality at it

lancety commented 2 years ago

my game dont have that 1-n case yet, but good to know I can do it this way. as Ivan said, I guess my code might have bug causing atlas being loaded twice, or maybe it is known issue. I will use interval as solution for now until need to improve it.
thanks for your explaination 👍