Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * per-pixel sprite interaction
  3.  */
  4.  
  5. // Create our application instance
  6. var app = new PIXI.Application({
  7.     width: window.innerWidth,
  8.     height: window.innerHeight,
  9.     backgroundColor: 0x2c3e50
  10. });
  11. document.body.appendChild(app.view);
  12.  
  13. // Load the bunny texture
  14. app.loader.add('bunny', 'https://pixijs.io/examples/examples/assets/bunny.png')
  15.     .load(startup);
  16.  
  17. function startup()
  18. {
  19.     var bunny = new PIXI.Sprite(app.loader.resources.bunny.texture);
  20.  
  21.     // Center the sprite's anchor point
  22.     bunny.anchor.set(0.5);
  23.  
  24.     // Move the sprite to the center of the screen
  25.     bunny.x = app.renderer.width / 2;
  26.     bunny.y = app.renderer.height / 2;
  27.  
  28.     bunny.scale.set(5);
  29.  
  30.     app.stage.addChild(bunny);
  31.  
  32.     bunny.interactive = true;
  33.  
  34.     bunny.on('mouseover', () => {
  35.         bunny.tint = 0xff0000;
  36.     });
  37.  
  38.     bunny.on('mouseout', () => {
  39.         bunny.tint = 0xffffff;
  40.     });
  41.  
  42. }
  43.  
  44. const tempPoint = new PIXI.Point();
  45. PIXI.Sprite.prototype.containsPoint = function(point) {
  46.     this.worldTransform.applyInverse(point, tempPoint);
  47.  
  48.     const width = this._texture.orig.width;
  49.     const height = this._texture.orig.height;
  50.     const x1 = -width * this.anchor.x;
  51.     let y1 = 0;
  52.  
  53.     let flag = false;
  54.  
  55.     if (tempPoint.x >= x1 && tempPoint.x < x1 + width)
  56.     {
  57.         y1 = -height * this.anchor.y;
  58.  
  59.         if (tempPoint.y >= y1 && tempPoint.y < y1 + height)
  60.         {
  61.             flag = true;
  62.         }
  63.     }
  64.  
  65.     if (!flag) {
  66.         return false;
  67.     }
  68.  
  69.     // bitmap check
  70.  
  71.     const tex = this.texture;
  72.     const baseTex = this.texture.baseTexture;
  73.     if (!baseTex.hitmap) {
  74.         if (!genHitMap(baseTex, 127)) {
  75.             return true;
  76.         }
  77.     }
  78.  
  79.     const hitmap = baseTex.hitmap;
  80.     const res = baseTex.resolution;
  81.     // this does not account for rotation yet!!!
  82.     let dx = Math.round((tempPoint.x - x1 + tex.frame.x) * res);
  83.     let dy = Math.round((tempPoint.y - y1 + tex.frame.y) * res);
  84.     let num = dx  + dy * baseTex.hitmapWidth;
  85.     let num32 = num / 32 | 0;
  86.     let numRest = num - num32 * 32;
  87.     return (hitmap[num32] & (1<<numRest)) > 0;
  88. }
  89.  
  90. function genHitMap(baseTex, threshold) {
  91.     if (!baseTex.resource) {
  92.         //renderTexture
  93.         return false;
  94.     }
  95.     const imgSource = baseTex.resource.source;
  96.     let canvas = null;
  97.     if (!imgSource) {
  98.         return false;
  99.     }
  100.     let context = null;
  101.     if (imgSource.getContext) {
  102.         canvas = imgSource;
  103.         context = canvas.getContext('2d');
  104.     } else if (imgSource instanceof Image) {
  105.         canvas = document.createElement('canvas');
  106.         canvas.width = imgSource.width;
  107.         canvas.height = imgSource.height;
  108.         context = canvas.getContext('2d');
  109.         context.drawImage(imgSource, 0, 0);
  110.     } else {
  111.         //unknown source;
  112.         return false;
  113.     }
  114.  
  115.     const w = canvas.width, h = canvas.height;
  116.     const imgData = context.getImageData(0, 0, w, h);
  117.     const hitmap = new Uint32Array(Math.ceil(w*h / 32));
  118.  
  119.     for (let j=0;j<h;j++) {
  120.         for (let i=0;i<w;i++) {
  121.             const num = j * w + i;
  122.             const num32 = num / 32 | 0;
  123.             const numRest = num - num32*32;
  124.  
  125.             if (imgData.data[4 * num + 3] >= threshold) {
  126.                 hitmap[num32] |= (1<<numRest);
  127.             }
  128.         }
  129.     }
  130.     baseTex.hitmap = hitmap;
  131.     baseTex.hitmapWidth = w;
  132.     return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement