All Projects → skyfish-qc → pixi-miniprogram

skyfish-qc / pixi-miniprogram

Licence: MIT license
一个可运行于微信小程序的PIXI引擎,通过模拟window环境,有些功能小程序无法模拟,就直接修改了PIXI引擎代码,最终使得PIXI引擎正常运行在小程序上

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to pixi-miniprogram

pixi-omber-gltf2-vector
Pixi.js library for using vector art created in Omber that's saved in glTF 2.0 format
Stars: ✭ 13 (-81.94%)
Mutual labels:  pixijs, pixi
React Pixi Fiber
Write PixiJS applications using React declarative style.
Stars: ✭ 568 (+688.89%)
Mutual labels:  pixijs, pixi
Leaflet.pixioverlay
Bring Pixi.js power to Leaflet maps
Stars: ✭ 264 (+266.67%)
Mutual labels:  pixijs, pixi
pixijs-ts-boilerplate
Just another PixiJS Typescript Boilerplate with some basic functionalities
Stars: ✭ 54 (-25%)
Mutual labels:  pixijs, pixi
Gown.js
UI system for pixi.js inspired by feathers-ui
Stars: ✭ 195 (+170.83%)
Mutual labels:  pixijs, pixi
D Zone
An ambient life simulation driven by user activity within a Discord server
Stars: ✭ 466 (+547.22%)
Mutual labels:  pixijs, pixi
Pixi Live2d
Display live2D model as a sprite in pixi.js.
Stars: ✭ 537 (+645.83%)
Mutual labels:  pixijs, pixi
Pixi.js
The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
Stars: ✭ 34,982 (+48486.11%)
Mutual labels:  pixijs, pixi
Pixi Haxe
Externs of Pixi.js for Haxe
Stars: ✭ 175 (+143.06%)
Mutual labels:  pixijs, pixi
Pixi Seed
Pixi.js project seed with ES6 and webpack
Stars: ✭ 149 (+106.94%)
Mutual labels:  pixijs, pixi
Creature webgl
2D Skeletal Animation WebGL Runtimes for Creature ( PixiJS, PhaserJS, ThreeJS, BabylonJS, Cocos Creator )
Stars: ✭ 140 (+94.44%)
Mutual labels:  engine, pixijs
Pixi Sound
WebAudio API playback library, with filters. Modern audio playback for modern browsers.
Stars: ✭ 201 (+179.17%)
Mutual labels:  pixijs, pixi
pixi-shim
PIXI.js Back-End "shim". For mocking Canvas in Node.js with ❤️
Stars: ✭ 45 (-37.5%)
Mutual labels:  pixijs, pixi
exengine
A C99 3D game engine
Stars: ✭ 487 (+576.39%)
Mutual labels:  engine
quickjs-build
Build for QuickJS JavaScript Engine
Stars: ✭ 25 (-65.28%)
Mutual labels:  engine
ray engine
A toy raycasting engine in Go + Ebiten
Stars: ✭ 19 (-73.61%)
Mutual labels:  engine
tortuga
A modern game engine built using dot net core
Stars: ✭ 14 (-80.56%)
Mutual labels:  engine
Video-Engine-Dash
A Dash plugin for playing back video and optionally syncing video to timestamped CSV Data
Stars: ✭ 26 (-63.89%)
Mutual labels:  engine
Spatial.Engine
[WIP] Spatial is a cross-platform C++ game engine.
Stars: ✭ 50 (-30.56%)
Mutual labels:  engine
delphi3d-engine
A 3D-graphic and game engine for Delphi and Windows.
Stars: ✭ 52 (-27.78%)
Mutual labels:  engine

#pixijs 小程序 WebGL 的适配版本。

  • 2021.1.11 修复graphics在新版微信内不正常显示的bug
  • 2021.1.14 改写PIXI.Text和PIXI.Graphics的渲染逻辑,需要在wxml文件中添加两个type 2d的canvas,然后把canvas传入PIXI中。其中一个用于Graphics渲染,一个用于Text渲染,传入参数示例:PIXI = createPIXI(canvas,stageWidth,canvas2d,canvas2dText)
  • 2021.3.25 添加遮罩实现示例
  • 2021.3.29 添加performance的判断
  • 2021.5.11 修改animate库不能显示的问题
  • 2021.12.09 使用微信小程序新版本的获取离屏canvas接口获取2d离屏canvas,减少创建PIXI时候额外传入的两个canvas,该版本需要微信小程序基础库2.16.1以上。如果要兼容旧版本的基础库,请使用v1.0版本代码。
  • 2022.03.29 添加音频支持
  • 2022.04.11 添加3.8版本spine库
  • 2022.06.12 修改背景不能透明的问题

使用

可参考 example 目录下的示例项目或参照以下流程:

  1. 复制dist目录的pixi.miniprogram.js到目录libs下

  2. 导入小程序适配版本的 pixi.js

import {createPIXI} from "../../libs/pixi.miniprogram"
var unsafeEval = require("../../libs/unsafeEval")
var installSpine = require("../../libs/pixi-spine")
var installAnimate = require("../../libs/pixi-animate")
var myTween = require("../../libs/myTween")
var PIXI = {};
var app = getApp()
Page({
    onLoad:function () {
        var info = wx.getSystemInfoSync();
        var sw = info.screenWidth;//获取屏幕宽高
        var sh = info.screenHeight;//获取屏幕宽高
        var tw = 750;
        var th = parseInt(tw*sh/sw);//计算canvas实际高度
        var stageWidth = tw;
        var stageHeight = th;
        var query = wx.createSelectorQuery();
        query.select('#myCanvas').node().exec((res) => {
            var canvas = res[0].node;
            canvas.width = sw;//设置canvas实际宽高
            canvas.height = sh;//设置canvas实际宽高,从而实现全屏
            PIXI = createPIXI(canvas,stageWidth);//传入canvas,传入canvas宽度,用于计算触摸坐标比例适配触摸位置
            unsafeEval(PIXI);//适配PIXI里面使用的eval函数
            installSpine(PIXI);//注入Spine库
            installAnimate(PIXI);//注入Animate库
            var renderer = PIXI.autoDetectRenderer({width:stageWidth, height:stageHeight,transparent:true,premultipliedAlpha:true,'view':canvas});//通过view把小程序的canvas传入
            var stage = new PIXI.Container();
            var bg = PIXI.Sprite.from("img/bg.jpg");
            // stage.addChild(bg);
            bg.interactive=true;
            bg.on("touchstart",function(e){
                console.log("touchstart",e.data.global)
            });
            bg.on("pointerup",function(e){
                console.log("touchend")
            });
            //小程序不支持加载本地fnt,json文件,所以涉及到fnt,json文件的加载需要放到网络服务器
            PIXI.Loader.shared
                .add({
                    name:'sound',
                    url:'https://96.f.1ting.com/local_to_cube_202004121813/96kmp3/2021/12/08/08b_zd/01.mp3'
                })
                .add("https://raw.githubusercontent.com/skyfish-qc/imgres/master/blog.fnt")
                .add("https://raw.githubusercontent.com/skyfish-qc/imgres/master/mc.json")
                .add('spineboypro', "https://raw.githubusercontent.com/skyfish-qc/imgres/master/spineboy-pro.json").load(function(loader, res){
                res["sound"].data.play();//播放音乐
                var btext = new PIXI.BitmapText('score:1234',{'font':{'name':'blog','size':'60px'},'tint':0xffff00});
                btext.x = 40;
                btext.y = 140;
                stage.addChild(btext);
                var explosionTextures = [];
                for (var i = 0; i < 26; i++) {
                    var texture = PIXI.Texture.from('pic'+(i+1)+'.png');
                    explosionTextures.push(texture);
                }

                for (i = 0; i < 2; i++) {
                    var explosion = new PIXI.AnimatedSprite(explosionTextures);

                    explosion.x = Math.random() * stageWidth;
                    explosion.y = Math.random() * stageHeight*0.2;
                    explosion.anchor.set(0.5);
                    explosion.rotation = Math.random() * Math.PI;
                    explosion.scale.set(0.75 + Math.random() * 0.5);
                    explosion.gotoAndPlay(Math.random() * 27);
                    stage.addChild(explosion);
                }
                var spineBoyPro = new PIXI.spine.Spine(res.spineboypro.spineData);
                spineBoyPro.x = stageWidth / 2;
                spineBoyPro.y = 1200;

                spineBoyPro.scale.set(0.5);
                spineBoyPro.state.setAnimation(0, "hoverboard",true);
                stage.addChild(spineBoyPro);
                
                //测试Animate
                var mymc = new PIXI.animate.MovieClip();
                stage.addChild(mymc);

                const testTxt = new PIXI.Text("test",{fill:'#ff0000',fontSize:44});
                testTxt.x = 100;
                testTxt.y = 400;
                stage.addChild(testTxt);

                const testTxt2 = new PIXI.Text("",{fill:'#ff0000',fontSize:44});
                testTxt2.x = 100;
                testTxt2.y = 500;
                stage.addChild(testTxt2);
                testTxt2.text = "test2";

                const graphics = new PIXI.Graphics();
                graphics.beginFill(0xFF3300);
                graphics.drawRect(0, 0, 100, 100);
                graphics.endFill();
                graphics.x = 100;
                graphics.y = 200;
                stage.addChild(graphics);

                const graphics2 = new PIXI.Graphics();
                graphics2.beginFill(0xFFFF00);
                graphics2.drawRect(0, 0, 200, 200);
                graphics2.endFill();
                graphics2.x = 200;
                graphics2.y = 400;
                stage.addChild(graphics2);

                //遮罩示例start
                //遮罩示意shader
                var frag = `
                varying vec2 vTextureCoord;
                uniform vec4 inputPixel;
                uniform vec2 dimensions;
                uniform sampler2D uSampler;
                uniform sampler2D masktex;
                void main(void) {
                    vec4 color = texture2D(uSampler, vTextureCoord);
                    vec2 coord = vTextureCoord.xy * inputPixel.xy / dimensions.xy;
                    vec4 maskcolor = texture2D(masktex, coord);
                    gl_FragColor = color*maskcolor;
                }
                `;
                const maskshape = new PIXI.Graphics();
                maskshape.beginFill(0xFFFFFF);//用于遮罩的形状必须为白色,因为shader遮罩原理是目标颜色乘以遮罩形状颜色,设置成白色可以避免干扰目标颜色。
                maskshape.drawCircle(100, 100, 100);
                maskshape.endFill();
                maskshape.x = 200;
                maskshape.y = 600;
                stage.addChild(maskshape);//先加入渲染
                var masktex = renderer.generateTexture(maskshape);//获取到遮罩形状纹理,如果是直接加载外部遮罩图片,上面部分可以省略。
                stage.removeChild(maskshape);//获得纹理后移除
                var uniform = {
                    masktex:masktex,
                    dimensions: [200, 200]//传入遮罩纹理图片尺寸,用于计算纹理的实际uv
                }
                
                var shader = new PIXI.Filter(null,frag,uniform);
                graphics2.filters = [shader];//给graphics2物体进行遮罩,原来是方形的经过遮罩后变成圆形
                //遮罩示例end

                renderer.render(stage);
            });
            //myTween缓动库使用示例
            /*
            缓动公式:Linear,Quad,Cubic,Quart,Sine,Expo,Circ,Elastic,Back,Bounce,Quint
            比如myTween.Quad.Out,myTween.Quad.In,myTween.Quad.InOut
            onEnd:结束事件
            onUpdate:每帧触发
            myTween.clean();//清除所有事件
            */
            var tweenObj = PIXI.Sprite.from("img/head.png");
            tweenObj.y = 500;
            stage.addChild(tweenObj);
            var tx = 600;
            function tweenMove() {
                myTween.to(tweenObj,1,{x:tx,ease:myTween.Quad.Out,onEnd:function(){
                    if(tx>0) {
                        tx = 0;
                    } else {
                        tx = 600;
                    }
                    tweenMove();
                }});
            }
            tweenMove();
            function animate() {
                canvas.requestAnimationFrame(animate);
                renderer.render(stage);
                myTween.update();
            }
            animate();
            // renderer.render(stage);
        });
    },
    touchEvent:function(e){
        //接收小程序的触摸事件传给PIXI
        PIXI.dispatchEvent(e);
    }
})

说明

  • 本项目当前使用的 pixi.js 版本号为 5.2.1。
  • 该适配版本的 PIXI 不在全局环境中,如使用 pixi.js 的其他配套类库,需要自行传入 PIXI 到类库中。可参考libs里面的pixi-spine的做法。
  • 改写PIXI.Text和PIXI.Graphics的渲染逻辑,以适配小程序的显示。
  • 视频不支持
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].