AS3 to JS or C#

Hi,

I’m trying to convert an AS3 class to JS or C# so i can use it in Unity3D. Now this is not the most easy class i think in AS3 so i could use some help.
Here’s the code:

package  com.ubekar.motiontracker
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.display.BlendMode;
    import flash.filters.BlurFilter;
    import flash.filters.ConvolutionFilter;
    import flash.geom.Point;
    import flash.media.Video;
    import flash.geom.Rectangle;
    
    public class CMotionTacker extends Point 
    {
        
        private var input:Video;
        private var curr:BitmapData;
        private var last:BitmapData;
        private var trackImg:BitmapData;
        private var areaThreshold:int;
        private var motion:Boolean;
        private var camX:int;
        private var camY:int;
        private var brect:Rectangle;
        
        /**
         * Construtor
         * @param    v    A video object to track the motion
         */
        public function CMotionTacker(v:Video) {
            input = v;            
            areaThreshold = 7000;
            camX = input.width;
            camY = input.height;
            trackImg = new BitmapData(camX, camY);
            last = new BitmapData(camX, camY);
        }
        
        /**
         * Track motion
         */
        public function track():Boolean {
            var curr:BitmapData = new BitmapData(camX, camY);
            
            curr.draw(input);    
            
            var rect:Rectangle = new Rectangle(0, 0, camX, camY);
            var p:Point = new Point();
            
            trackImg = curr.clone();
                        
            // get the difference of the current and last image
            trackImg.draw(last, null, null, BlendMode.DIFFERENCE);
                        
            // brighten the image
            var mat:Array = [0, 0, 0,
                            0, 15, 0,
                            0, 0, 0];        
            var conv:ConvolutionFilter = new ConvolutionFilter(3, 3, mat);
            conv.divisor = 1;            
            trackImg.applyFilter(trackImg, rect, p, conv);
            
            // black areas now represent still area, INVERT it to get motion areas as black
            trackImg.draw(trackImg, null, null, BlendMode.INVERT);
            
            // blur the image
            trackImg.applyFilter(trackImg, rect, p, new BlurFilter(8, 8));
            
            // get bounding rectangle containing black areas
            brect = trackImg.getColorBoundsRect(0x00ffffff, 0x00000000, true);
            
            // area of the tracked area
            var area:int = brect.width * brect.height;
            
            // calculate amount of motion detected, valid if greater than threshold
            motion = false;
            if (area > areaThreshold) {
                motion = true;
                this.x = brect.x + brect.width / 2;
                this.y = brect.y + brect.height / 2;
            }
            
            // copy current image to last pic
            last.copyPixels(curr, rect, p);
            return motion;
        }
        
        /**
         * Get the processed image which is used to track motion.
         */
        public function get trackImage():BitmapData {
            return trackImg;
        }
        
        /**
         * Get the rectangle tha bounds the current area of motion.
         */
        public function get bound():Rectangle {
            return brect;
        }
        
    }// eof class
    }// eof package

Most of the code is pretty straight forward only problem i have (i think) is with the direct camera feed and the bitmap data. Can anyone help me get this code working in Unity3D?

this isn’t just a conversion of language, this is a matter of framework.

unity is using the mono framework, which has its own library of classes. AS3 runs in flash which has its own library of classes.

You need to find classes for the members of this class that are like the other. Things like Boolean and Point have parallels. (though I have NO idea why your as3 class extends Point, that’s just weird. And you won’t be able to in C# because any similar in mono will be Point or Vector2 which are structs).

You can of course write implementations of some of the classes/structs you can’t find to meet your needs exactly.

The difficult ones will be Video and BitmapData though. Texture2D could be your BitmapData, but they’re very different in some ways. I really don’t know about Video though.

high level languages hide heaps of code behind a very user friendly API. There are several functions been used in this class that would not be a walk in the park to implement yourself. Drawing with various blendmodes , getColourBoundRect , ConvolutionFilter etc…

True, i read over a few parts of code yesterday and it’s going to be a hard one. So if rewriting the code to JS / C# is not an option, is there a way to use the AS3 code in unity3D (Android / IOS development).

No. Rewriting the code is an option, it’s just not going to be simple. The issue is not the language (it would be nearly identical to Unityscript if Unity had the same functionality), but the APIs.

–Eric

Right so i’m going to need to check every function which is not available in Unity3D and rewrite them. Well i’ll see how it goes then :slight_smile: there’s no hurt in trying it out :slight_smile:

That’s the spirit!

–Eric