GithubHelp home page GithubHelp logo

Comments (8)

jnsmalm avatar jnsmalm commented on May 18, 2024 1

Added automatic support for spritesheet textures, will come in next release.

from pixi3d.

djlastnight avatar djlastnight commented on May 18, 2024

I was able to fix this, using the following method, but it will be better if you add support for spritesheets either way.

// Do NOT use - huge memory leak!!!
    public static extractFromSpriteSheet(textureName: string): Texture {
        let texture: Texture = Texture.from(textureName);
        let sprite: Sprite = new Sprite(texture);
        let options = {
            width: texture.width,
            height: texture.height
        };

        let baseRenderTexture: BaseRenderTexture = new BaseRenderTexture(options);
        let renderTexture: RenderTexture = new RenderTexture(baseRenderTexture);

        // Change this for your application
        let renderer: Renderer = App.instance.renderer;

        renderer.render(sprite, renderTexture);

        return renderTexture;
    }

from pixi3d.

jnsmalm avatar jnsmalm commented on May 18, 2024

Hey!

You can also try using texture transform. In my example the sprite was rotated inside the sprite sheet, so you might need to adjust the calculations a bit. I'll see if I can support this automatically when setting the material texture.

let texture = resources["assets/spritesheet.json"].textures["texture.png"]
texture.transform = new PIXI3D.TextureTransform()
texture.transform.offset.set(
  (texture.frame.x + texture.frame.width) / texture.baseTexture.width,
  (texture.frame.y + 0) / texture.baseTexture.height
)
texture.transform.scale.set(
  texture.frame.height / texture.baseTexture.height,
  texture.frame.width / texture.baseTexture.width
)
texture.transform.rotation = -90 * PIXI.DEG_TO_RAD

mesh.material.baseColorTexture = texture

from pixi3d.

djlastnight avatar djlastnight commented on May 18, 2024

Thanks for the code. Actually my code has a huge memory leak, so I will try yours :)

from pixi3d.

djlastnight avatar djlastnight commented on May 18, 2024

I just tested the new feature - it works perfectly fine. In my particular case I grab animation frames from one or more spritesheets.
I had to set the spritesheet trim option to None, otherwise I get strange results, because the sprites' transparent outermost part gets cut by texture packer to save space.

Anyway I am wondering do you have a solution for the following problem. See the image below.
image

If I use alpha blend - the model textures looks nice, but got framed.
If I use alpha clip - the models looks bad, but no frame around them is visible.
Any ideas?

from pixi3d.

djlastnight avatar djlastnight commented on May 18, 2024

I found you gave access to this programatically, which is awesome.
So I was able to workaround this as follows:
When I start the animation I change the blend mode to mask, using alphaCutoff 0.80.
On animation complete, I simpy change back the blend mode to blend (which is the default for my models).

    /**
     * Sets the material alpha blend mode.
     *
     * @param {number} meshIndex The index of the mesh, whose material's alpha mode to change.
     * @param {StandardMaterialAlphaMode} mode The new alpha blend mode.
     * @param {number} [alphaCutoff=0.8] The new alpha cutoff value,
     * in case StandardMaterialAlphaMode.mask mode is passed.
     * @memberof Model3D
     */
    public setAlphaMode(meshIndex: number, mode: StandardMaterialAlphaMode, alphaCutoff: number = 0.8) {
        let mat = this.model.meshes[meshIndex].material as StandardMaterial;
        mat.alphaMode = mode;
        if (mode == StandardMaterialAlphaMode.mask) {
            mat.alphaCutoff = Math.min(Math.max(0, alphaCutoff), 1);
        }
    }

The result is not perfect, but very good imho.

from pixi3d.

jnsmalm avatar jnsmalm commented on May 18, 2024

Curious, are you changing the texture of a mesh to play the animation frame by frame?

from pixi3d.

djlastnight avatar djlastnight commented on May 18, 2024

Yes, and actually runs very efficient and fast. I left the game loop the animations for 8 hours and the memory usage did not change at all. Is there a better (built-in) way doing this? I did not find something like AnimatedSprite3D, so I had to do it myself by applying tweens.

from pixi3d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.