Advertisement
KoctrX

Untitled

May 8th, 2023
821
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as PIXI from 'pixi.js';
  2. import Stats from 'stats.js';
  3. import * as PIXIFilters from 'pixi-filters';
  4.  
  5. let enabledGodray = true;
  6. let enabledShadows = true;
  7.  
  8. function setupFilters(app, sp) {
  9.     const sFilters = [];
  10.     const spFilters = [];
  11.  
  12.     if(enabledGodray) {
  13.         const filter = new PIXIFilters.GodrayFilter({
  14.             time: 0,
  15.             gain: .5,
  16.             lacunarity: 2.5,
  17.             alpha: 1,
  18.             parallel: true,
  19.             angle: 30,
  20.             center: { x: 0, y: 0 }
  21.         });
  22.    
  23.         filter.isGoodray = true;
  24.         sFilters.push(filter);
  25.     }
  26.  
  27.     if(enabledShadows) {
  28.         const shadow = new PIXIFilters.DropShadowFilter({
  29.             blur: 0, quality: 5, alpha: .5,
  30.             offset: { x: 4, y: 7 }, color: '#000000'
  31.         });
  32.  
  33.         spFilters.push(shadow);
  34.     }
  35.    
  36.     app.stage.filters = sFilters;
  37.     sp.filters = spFilters;
  38. }
  39.  
  40. function render(app) {
  41.     (app.stage.filters || []).forEach(filter => {
  42.         if (filter && filter.isGoodray) {
  43.             filter.time += 0.005;
  44.         }
  45.     });
  46. }
  47.  
  48. async function boot() {
  49.     const element = document.querySelector('.render');
  50.     const stats = new Stats();
  51.     const app = new PIXI.Application({
  52.         width: 1366,
  53.         height: 768,
  54.         backgroundColor: '0xffffff',
  55.         resolution: 1,
  56.         antialias: true
  57.     });
  58.  
  59.     element.append(stats.dom);
  60.     stats.showPanel(0);
  61.     element.appendChild(app.view);
  62.     const sp = await new PIXI.Sprite(await PIXI.Assets.load('game_assets/players/anim/turtle4/10.png', () => { }));
  63.     const scale = Math.min(200 / sp.width, 200 / sp.height);
  64.  
  65.     sp.scale.set(scale, scale);
  66.     sp.position.set(0, (app.renderer.height - sp.height) / 2);
  67.     const rect = new PIXI.Graphics();
  68.     rect.lineStyle(0);
  69.     rect.beginFill(0xffffff, .6);
  70.     rect.drawRect(0, 0, app.renderer.width, app.renderer.height);
  71.  
  72.     app.stage.addChild(rect, sp);
  73.     setupFilters(app, sp);
  74.    
  75.     let direction = 'right';
  76.     const animationFrame = () => {
  77.         stats.begin();
  78.  
  79.         render(app);
  80.         const bounds = sp.getBounds();
  81.         if (bounds.width + bounds.x >= app.renderer.width) {
  82.             direction = 'left';
  83.         } else if (bounds.x <= 0) {
  84.             direction = 'right';
  85.         }
  86.  
  87.         sp.position.x += 3 * (direction == 'left' ? -1 : 1);
  88.         sp.angle += 5;
  89.         sp.pivot.set(sp.width / 2, sp.height / 2);
  90.         stats.end();
  91.         requestAnimationFrame(animationFrame);
  92.     };
  93.  
  94.     requestAnimationFrame(animationFrame);
  95.  
  96.     document.querySelector('[name="godray"]').addEventListener('click', e => {
  97.         enabledGodray = e.target.checked;
  98.         setupFilters(app, sp);
  99.     });
  100.    
  101.     document.querySelector('[name="shadows"]').addEventListener('click', e => {
  102.         enabledShadows = e.target.checked;
  103.         setupFilters(app, sp);
  104.     });
  105. }
  106.  
  107. boot();
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement