Archive for the ‘C#’ Category

Sunday, April 11th, 2010

iTween is a tweening kit for Unity that is in the same style as TweenLite, Tweener and others in the tween format that is common for flash tween libraries in Actionscript 3. This library is available in javascript and C# for Unity 3d Projects and is quite fast and solid.

iTween is a simple one file drop in for some great scripted animation and easing that is very reminiscent of Flash using Penner equations and common libraries so it is easy to get started. It works for web player, desktop and iPhone Unity (however long that lasts).

Some sample code looks like this:

private GameObject go;
private GameObject cam;

void Awake()
{
    go = gameObject;
    cam = Camera.main.gameObject;
}

private void Start()
{
    iTween.rotateFrom(go, 1.5f, 0, null, 90, null, iTween.EasingType.easeInExpo);
    iTween.moveFrom(go, 1.5f, 0, null, 3.5f, null, iTween.EasingType.easeInExpo);
    iTween.colorTo(go, .3f, 1.5f, 3, .5f, 1.2f);
    iTween.shake(cam, .8f, 1.5f, null, .3f, null);
    iTween.scaleTo(go, 2, 2.3f, null, 2, null);
    iTween.rotateBy(go, null, 4.3f, .5f, null, null);
    iTween.moveTo(go, null, 4.6f, null, 1.2f, null);
    iTween.moveTo(go, null, 5.8f, null, 0, null, iTween.EasingType.easeInExpo);
    iTween.shake(cam, .8f, 6.8f, null, .3f, null);
    iTween.colorTo(go, .5f, 7.6f, .165f, .498f, .729f);
    iTween.scaleTo(go, null, 7.6f, null, 1, null);
}
Friday, February 12th, 2010

Balder was one of the first 3d prototype engines in Silverlight and it has evolved quite nicely.  Balder by Einar Ingebrigtsen is described as a “Managed GameEngine with both 2D and 3D support targetting Silverlight, Xna and OpenGL.”

The sample browser will show you what Balder is capable of and it has come pretty far since the first version showing a wireframe teapot.

You know you’ve made it as a 3D engine when there are Augmented Reality apps for it.

Here’s a glance at what some of the C# source looks like for a feel of the engine code from the Silverlight4 TestApp:

using Balder.Core.Execution;
using Balder.Core.Objects.Geometries;
using System;
using Balder.Core.Lighting;
using Balder.Core;
using Colors=System.Windows.Media.Colors;
 
namespace Balder.Silverlight4.TestApp
{
    public class MyGame : Game
    {
        public override void OnInitialize()
        {
 
            Camera.Position.X = 0;
            Camera.Position.Y = 0;
            Camera.Position.Z = -80;
 
            var light = new OmniLight();
            light.Diffuse = Color.FromSystemColor(Colors.Green);
            light.Ambient = Color.FromSystemColor(Colors.Green);
            light.Specular = Color.FromSystemColor(Colors.White);
            light.Position.X = 0;
            light.Position.Y = 0;
            light.Position.Z = -130;
 
            Scene.AddNode(light);
 
            base.OnInitialize();
        }
 
 
        public override void OnLoadContent()
        {
            var teapot = ContentManager.Load("teapot.ase");
            Scene.AddNode(teapot);
            base.OnLoadContent();
        }
 
 
        private double _sin;
 
        public override void OnUpdate()
        {
 
            Camera.Position.X = (float)(Math.Sin(_sin)*80);
            Camera.Position.Y = 0;
            Camera.Position.Z = (float)(Math.Cos(_sin) * 80);
 
            _sin += 0.05;
            base.OnUpdate();
        }
 
    }
}
Wednesday, October 28th, 2009

Unity 3D Indie is now free for all developers and just called Unity now.  The Unity 3D Pro license is still $1500 and worth every penny.  But this news is great for indies and moreso the pro users that want the Unity Web Player to have more penetration and installs in the market.

Companies like EA, Cartoon Network and Lego are using Unity 3D and just about every game developer I know including myself has been excited about the possibility of an engine that allows creation of hardware rendered web based games and desktop games, which are multi-platform and paths into the mobile market (iPhone/Touch) and console like Wii and XBOX in development (for additional licenses).

When Unity 3D released support for Windows as a development environment in addition to Mac it  literally blew up as predicted this year. Also, Unity 2.6 is out which is big because it finally supports third party source control such as Subversion and Perforce. Many of the barriers that were keeping it from integration into gaming pipelines are gone:  the price, the single platform and the source code integration issues.  Unity 3D has addressed all those issues.

What are you waiting for? Get your Unity3D on!

Thursday, April 30th, 2009

Unity 3D iPhone was updated recently to 1.0.2 and it has been greatly improved in performance and a much more solid 1.0 toolkit.  According to Unity 3D information by up to 50% which means much more room for assets to munch memory for us yay!

I updated to iPhone SDK 3 beta 4 and iPhone OS 3 beta 4 and the latest Unity iPhone and things were much better in perception of speed at least in early testing.  Not sure if it was more from one or the other but the games I am testing/building so far are quicker and the OS feels faster overall.

Get the latest Unity 3d iPhone dev kit (only for Mac OSX obviously since it uses XCode to compile per Apple licensing requirements)

This build fixes many issues and makes some great optimizations for speed as listed here:

New Features and Improvements

  • Reduced memory footprint for uncompressed audio by 50%
  • “Memory usage for textures reduced by 50%. Texture memory is now freed once it has been submitted to OpenGLES on the device. The “Enable Get/SetPixels” flag in the Texture Import Settings lets you disable this feature on a per texture basis in order to access the texture data from a script using GetPixel etc.
  • Improved iPhone script call optimization
  • Removed unused parts of Mono runtime
  • Reduced memory overhead while reading data from disk and slightly improved load times.
  • Support for several predefined splash-screens (portrait/landscape) for Indie version. Just rename one of the splash-screens in the output directory to Default.png
  • Exported audio session activation/deactivation functions to AppController.mm
  • Added Scripting Reference code examples for iPhone specific APIs

Bug Fixes

  • Fixed audio to play correctly after phone call / text message / alarm interruption occurs
  • Fixed compressed audio occasionally refusing to play
  • Fixed AudioSource.PlayOneShot to work correctly with compressed audio
  • Fixed audio to respect Mute switch and background iPod music
  • Fixed Pause function and time property for compressed audio clips
  • Fixed OpenAL memory leak
  • Fixed PhysX memory leaks
  • Fixed Audio and Animation assets leaking while loading new scene
  • Fixed a crash related to playing compressed audio in a sequence
  • Fixed memory leak while updating Mesh geometry data
  • Fixed several small memory leaks in rendering module
  • Fixed asynchronous .NET sockets
  • Fixed .NET threads
  • Fixed cross thread boundary calling to the delegates
  • Fixed UnityEngine.TextEditor stripping
  • Fixed GUI slider stripping
  • Fixed GUI scroll view stripping
  • Fixed IndexOutOfRange exception checking
  • Fixed Boo.Lang.dll stripping
  • Fixed occasional crashes of AOT cross compiler
Thursday, February 19th, 2009

The guys over at Flashbang Studios, a web game development studio in phoenix, az, and some of the most visible developers in the Unity3d space with Blurst, released something that may interest both FlashDevelop users and Unity3d developers.

UnityDevelop was released by Flashbang Studios recently and it is a modded version of FlashDevelop (originally from SharpDevelop a really nice open source .NET and mono IDE) and it supports intellisense for Javascript or Unity3d’s use of Javascript which is called UnityScript much like ActionScript.  UnityScript can be a little more strict and has access to all of Unity3d’s API calls just like C# and Boo in the mono based virtual machine that Unity3d uses.

FlashDevelop, is by far the best Flash / Flex /haXe IDE in my opinion so it is really great to release this for Unity.  I hope one day I or someone has the time to port to Mono so it can be used on Macs even with the 140 pinvokes, it would be a good spread mechanism for mono.

With Unity3d coming to windows soon, UnityDevelop could be a good go to IDE for unity if you aren’t using solely C# with VS.NET. Currently this is based on FlashDevelop2 source code.

Video Overview of UnityDevelop


UnityDevelop Walkthrough from Flashbang Studios on Vimeo.

Downloads

Thanks flashbang!

Sunday, January 13th, 2008

Here is an interesting look to start the new year at Google Trends for some common keywords to this blog audience. Comparing AS2, AS3, Silverlight and actionscript you can see that there is some pretty interesting things happening.

as3 as2 actionscript silverlight  

First off, AS2 and AS3 are clouded because they are also related to EDI and EDI-INT so they get a bit inflated. Silverlight though is pretty unique in the naming. So from this graph we can see this happening:

  • Silverlight and AS3 are growing rapidly
  • Silverlight is crossing over as3 or meeting it
  • The market looking for Silverlight is about 8-10 times as large as actionscript/as3/as3
  • Silverlight and AS3 are growing, AS2 has no growth left and is an EOL language (end of life)
  • AS2 (even with crossover to EDI trends for “as2″) leveled out, where AS3 is starting to lift to a larger market. This is strongly due to it being a fun language based on ES4 and interests programmers.
  • The as3 effect started right in March-April 2007 (hrm I started this blog in April 2007 coincidenc? j/k :))

Another chart including Flex shows a better picture of the keyword wars between flex and silverlight.

as3 as2 actionscript silverlight flex

So from this graph we can see this happening:

  • Flex has a large buzz
  • Adobe’s marketing efforts are many while silverlight is more unique and focused
  • Flex, as3 and Silverlight are popular, and growing in their support (the growth market for technology is in these areas, not in tech from Flash 8/as2)
  • AS2 still taking a nosedive

Flash and Flex programmers and designers should know that with Silverlight 2.0 release coming and the capabilities of Silverlight 2.0 more competitive, flashers should be working on Flex, Flash9 or at least AS3 if not Flex. The RIA competition market will heat up immensely this year with Silverlight 2.0 and possibly Flex3 and coding and programming for Flash and Flex is becoming more involved. It also has a very strong competitor in Silverlight 2.0 coming that will drive this market.

This is all great news if you are ready for it, if you are still coding actionscript2 (AS2) and paying no mind to Silverlight, Flex or at least actionscript3 (AS3) then you will see your market slowly start to fade as things are ramped up and more of a programming focus in the vector wars. If you are a flash coder and ignoring Silverlight, your solutions will suffer. If you are a silverlight coder or .NET coder and ignoring the Flex and AS3 rise your solutions will suffer. I have been playing in AS3, Flex and Silverlight for over a year on both now and they are an entirely new platform with great programming models. The competition puts focus on this market so it is a great time to be skilled in these areas.

Thursday, January 10th, 2008

Using SharedObjects in Flash is very simple. Flash has SharedObjects that have been in the player since Flash6 when the introduction of Flash Communication Server which is now Flash Media Server which is releasing version 3 soon (also remote SharedObjects in Red5 is an open source RTMP media server that is based on Flash Media Server). So we can thank this release for SharedObjects, Camera objects, Audio, lots of the NetConnections, protocol enhancements and many other things. However to keep the tips simple we will just touch on the local usage and post a series of posts on these objects.

SharedObjects locally and remote have changed the way offline is thought about and are the backbone of many offline systems and prototypes. They have been influential in moving storage locally to remote in a lightweight AMF0 or AMF3 format.

SharedObject is in the flash.net namespace in AS3.

Here we show how to use the local version of a SharedObject to store data in the most simple form.

import flash.net.SharedObject;
var so:SharedObject = SharedObject.getLocal("userData");
so.data.username= "user1377";
so.data.pwdhash= "[hash] or pwd";
so.flush(); // writes changes to disk

You can see this is extremely simple to store data.Here we show how to use the local version of a SharedObject to retrieve data in the most simple form.

import flash.net.SharedObject;
var so:SharedObject = SharedObject.getLocal("userData");
var username:String = so.data.username;
var pwdhash:String = so.data.pwdhash;

That is it! In the most basic form SharedObjects are more simple than cookies and also are quite nice living outside the bounds of the cookies folder. If a user deletes all cookies it will not delete the SharedObjects. To delete SharedObjects you need to roght click on the Flash player, go to Settings and delete the objects there.You can store any type of data Flash supports from objects to numbers to strings in the SharedObject data.

import flash.net.SharedObject;
var so:SharedObject = SharedObject.getLocal("userData");
so.data.username= "user1377";
so.data.uid= new Number(1337);
var obj:Object = new Object();
obj.prop = "value";
so.data.userobj= obj;
so.flush(); // writes changes to disk

For large applications SharedObjects local are great because users can have their SharedObject space set to a high amount of space or unlmited for large offline type apps or more complex apps stored in a state object. It gets interesting when you pass this to the server in AMF or extremely compact AMF3 format to a remote stored object, via remoting, Shared Object events or any way you want to.We will be posting more on remote shared objects and some of the other tools such as Camera and Streams for AS3 over the coming weeks and deeper into the Sync Events for remote and local SOs.Sephiroth has a great Python tool to peer into the SOs on disk.

The first version of SharedObject Reader was written in python 2.2.
This new version is written in C# (C Sharp) as it’s now part of FlashDevelop editor.

Red5 also does this as well as many AMF kits.

Sync Events for SharedObject (as they change they launch a sync event)

Event object type: flash.events.SyncEvent
SyncEvent.type property = flash.events.SyncEvent.SYNC

Read more at adobe docs:

flash.net.SharedObject

Sample from AS3 docs showing usage

package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.net.SharedObject;
    import flash.net.SharedObjectFlushStatus;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;

    public class SharedObjectExample extends Sprite {

        private var mySo:SharedObject;

        public function SharedObjectExample() {
            buildUI();
            saveBtn.addEventListener(MouseEvent.CLICK, saveValue);
            clearBtn.addEventListener(MouseEvent.CLICK, clearValue);

            mySo = SharedObject.getLocal("application-name");
            output.appendText("SharedObject loaded...\n");
            output.appendText("loaded value: " + mySo.data.savedValue + "\n\n");
        }

         private function saveValue(event:MouseEvent):void {
            output.appendText("saving value...\n");
            mySo.data.savedValue = input.text;

            var flushStatus:String = null;
            try {
                flushStatus = mySo.flush(10000);
            } catch (error:Error) {
                output.appendText("Error...Could not write SharedObject to disk\n");
            }
            if (flushStatus != null) {
                switch (flushStatus) {
                    case SharedObjectFlushStatus.PENDING:
                        output.appendText("Requesting permission to save object...\n");
                        mySo.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
                        break;
                    case SharedObjectFlushStatus.FLUSHED:
                        output.appendText("Value flushed to disk.\n");
                        break;
                }
            }
            output.appendText("\n");
        }

        private function clearValue(event:MouseEvent):void {
            output.appendText("Cleared saved value...Reload SWF and the value should be \"undefined\".\n\n");
            delete mySo.data.savedValue;
        }

        private function onFlushStatus(event:NetStatusEvent):void {
            output.appendText("User closed permission dialog...\n");
            switch (event.info.code) {
                case "SharedObject.Flush.Success":
                    output.appendText("User granted permission -- value saved.\n");
                    break;
                case "SharedObject.Flush.Failed":
                    output.appendText("User denied permission -- value not saved.\n");
                    break;
            }
            output.appendText("\n");

            mySo.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
        }

        // UI elements
        private var inputLbl:TextField;
        private var input:TextField;
        private var output:TextField;
        private var saveBtn:Sprite;
        private var clearBtn:Sprite;

        private function buildUI():void {
            // input label
            inputLbl = new TextField();
            addChild(inputLbl);
            inputLbl.x = 10;
            inputLbl.y = 10;
            inputLbl.text = "Value to save:";

            // input TextField
            input = new TextField();
            addChild(input);
            input.x = 80;
            input.y = 10;
            input.width = 100;
            input.height = 20;
            input.border = true;
            input.background = true;
            input.type = TextFieldType.INPUT;

            // output TextField
            output = new TextField();
            addChild(output);
            output.x = 10;
            output.y = 35;
            output.width = 250;
            output.height = 250;
            output.multiline = true;
            output.wordWrap = true;
            output.border = true;
            output.background = true;

            // Save button
            saveBtn = new Sprite();
            addChild(saveBtn);
            saveBtn.x = 190;
            saveBtn.y = 10;
            saveBtn.useHandCursor = true;
            saveBtn.graphics.lineStyle(1);
            saveBtn.graphics.beginFill(0xcccccc);
            saveBtn.graphics.drawRoundRect(0, 0, 30, 20, 5, 5);
            var saveLbl:TextField = new TextField();
            saveBtn.addChild(saveLbl);
            saveLbl.text = "Save";
            saveLbl.selectable = false;

            // Clear button
            clearBtn = new Sprite();
            addChild(clearBtn);
            clearBtn.x = 230;
            clearBtn.y = 10;
            clearBtn.useHandCursor = true;
            clearBtn.graphics.lineStyle(1);
            clearBtn.graphics.beginFill(0xcccccc);
            clearBtn.graphics.drawRoundRect(0, 0, 30, 20, 5, 5);
            var clearLbl:TextField = new TextField();
            clearBtn.addChild(clearLbl);
            clearLbl.text = "Clear";
            clearLbl.selectable = false;
        }
    }
}
Saturday, November 17th, 2007

The Proposal

Moses, the maker of FuseKit, is hoping to influence Adobe product lines to include a common base for animation and motion going forward. Currently the AS3 world is very alive and is inspiring developers like myself to build lots of toolkits and really creating reusable code and kits that can make things very easy from going to Flash to Flex. But wouldn’t it be nice if a part of these kits that have to be downloaded every time you have an application use them be part of the native Adobe applications, or a core animation kit that partially standardizes animation basics to build upon further?

Are we just asking for trouble or is this a good idea? I don’t’ think it can hurt to bring this to the surface. I know that common syntax and familiar kits can really help the developers and designers move from Flash to Flex to After Effects to Javascript, it could also help Adobe with usage and usefulness of their entire suite of products. Or further this could be a standard that allows Silverlight to also build upon (open standard) and may the best platform win.

I think it would be very wise for Adobe to:

  • Standardize animation toolkits across their products and
  • Start standardizing some of the basic tools of building motion and filter kits to native but still allowing a flourishing open source and community research and development aspect.

What MosesProposes:

Moses did speak with someone at Adobe about this and it is generally in the plans:

“It was also a pleasure to see Richard Galvan present the upcoming crop of Flash features: the sleek update to the animation timeline (better late than never?), support for columnated flowing text (double finally!) and the big one, native 3D player support for Display Objects as rotatable 2D planes. He ran out of time and didn’t get to a few others shown at Adobe MAX, such as built-in IK (inverse kinematics) and faster pixel-level drawing for texture-mapping and photoshop-like filter effects.

Talking to him after the presentation I learned that Richard has a keen awareness of exactly where each feature is at currently. We chatted about low-level animation mechanics of the Flash Player, and I found out that the holy grail of a time-based player is indeed on the distant horizon, but that each rev will need to be a small step toward this goal. The new Flash timeline features meld After Effects, Premiere and Live Motion, and from what I’ve seen I have to say that they are nailing this long-overdue upgrade with great design decisions and a level of usability we’ve never seen in Flash. Kudos, team!”

The Current Situation

Right now Tweener and TweenLite (and animation package and a few others) have a unique position in that they work the same almost for AS2 and AS3 (Flex or Flash – with minor property changes such as _x to x as that has changed in AS3). But it would be nice if these kits also had a version for After Effects (really bringing that tool into Flash/flex developer worlds) and Javascript and it would be great if Silverlight also were supported (AgTweener anyone?).

Tweener is leading the pack in this aspect of creating a similar experience from AS2 to AS3 in Flash and AS3 in Flex and even JSTweener for Javascript, and a kit for haXe which is becoming my favorite toy and the dark horse with the most upside potential, with haXe on the loose these points may all be moot as haXe can target any platform (except After Effects easily, correct me if I am wrong and Silverlight but it could easily be done so to do it for Silverlight 1.0 which is ES3 based).

I don’t use After Effects as much right now but if I could easily incorporate this into Flash/Flex and script and animate in a similar syntax and way I know After Effects would definitely have a boost in interest.

Also, the forgotten one Director, can we please get an ES4 based language in that application, or an update? Then kits and add-ons are much more possible. I really miss hardware accelerated 3d in browser as a pushed technology, Director is still around but it does not get the focus it needs. Feel the freedom and coolness just in this small test here in director, hardware accelerated 3d is the best, the Director application environment and Lingo and hacked in javascript are not the best. As a long-time Director user, hobbyist and professional I am disappointed in Director’s support at Adobe thus far, but I digress.

The Reality

The reality is right now the only problem with kits like Tweener, TweenLite, Tween, mx.transitions, mx.motion, etc is that the source has to be embedded in movieclips multiple times. Sometimes there are multiple animation kits per compiled SWF that have to be used for more advanced features. This adds bulk that if common might not need to be there (this comes into play still on mobile and large games/apps).

Let’s say you have an application that pulls in many disconnected SWFs and they all have animation in them, well if you have 20 of these let’s say, and you embedded a very small Tweener at 9k per SWF. That is about 200k of duplication of AS code. Due to the kits small sizes this is not a problem really but when animation kits like Animation Package come into play, you are talking 40k per SWF which would leave you with almost a meg of just duplicated animation code. I don’t think this is that major of a problem for kits like Tweener (9k compiled) and Tweenlite (3k compiled) but as projects get bigger and more depth of animation platforms needed this can be a problem. This can also be solved in architecture with a controller and dummy SWFs to animate but there are times when you need animation in the compiled SWFs and then also need it in many others and the controller.

The other reality is the animation kits (mx.transitions.easing, mx.transitions.tween) for Flex and Tween for fl are a little bloated, more difficult than needed to use and as has been seen, much slower than kits currently available in the community. My one fear about this is that if Adobe makes this, possibly like Microsoft’s toolkits and libraries they put out, they are always bloated and slower, then because they are embedded they are untouchable. If it was standard enough as building blocks that are faster because they are native, then this is the best option as embedded script would be hard pressed to beat native code in the players/applications.

The Future Plans

Some of this is underway….

Animation kits for future, Adobe is releasing Flash 10 called ‘Astro’ that has many new improvements in tweening with xml closer to flex or even Silverlight like transitions and storyboards. Aral Balkan, a sponsor of OSFlash, posted on this and even that Diesel Flash CS4 will include more Tween tools for IK/bones. Tweener , TweenLite, Animation Package, Animation System etc these are all helping to define the best way to do animation kits.

Physics toolkits have their own animation kits currently usually to handle the movement according to algorithms. FOAM, APE , Box2DFlashAS3 (just released very recently will be posting more on this after I check it) and Motor Physics (unreleased but heavily demoed at polygonal labs) are great physics toolkits and I like this being part of the community to get refined, maybe one of them or the best performing ones becomes part of the proposed Adobe Animation bundle. These will define the best way to do physics kits.

3d in flash toolkits have also been emerging rapidly in 2007 with Papervision3D, Away3d based on pv3d, Sandy, and even engines starting to get built on top of these platforms.

The general direction is moving towards another platform in there somewhere but I think much work is left to be done to standardized physics systems, 3d and advanced motion filter tweens and bezier, splines (Catmull-Rom), editors, etc. I think it is getting time for basic animation kits to become more standard though and in latest versions of flash this is included in the flex and flash scripts but not the native code.

Right now the standard in syntax and the broadest reach is Tweener and due to the bigger fish syndrome, haXe that can target any platform, it also has a Tweener and can create code for as2, as3 and any target written in if After Effects, Premiere or other apps get more robust and standard animation and motion kits. Tweener has kits made and contributed for AS2, AS3, haXe, Javascript and others.

There is also Hydra and the AIF Toolkit that are standardizing After Effects and Flash shaders and filters into a new shader language like Cg and reminiscent of processing.org.

As humans we trial and error and build new platforms in the market to step on to create better platforms to build cool stuff, it is evolving right now. AS3 is inspiring platforms within platforms of Flash and Adobe kits as well as on Silverlight and in the Javascript world with JSTweener, jquery etc. As these things are refined we build a level standard platform to build more stuff on. Eventually this will be there and whoever does the standard platform for animation will probably reap in users and abilitty to easily add new products and solutions where people already have training. Silverlight is an example with .NET developers. .NET was also an example with C# so similar to Java. ES4 based AS3 has proven it is inspiring all types of new platforms and kits and will continue to do so and it is an interesting time in this industry whichever direction it goes.

Wednesday, October 17th, 2007

Currently working on some motion detection with flash/c# and webcams right now. Here’s a basic overview of some motion detection source files and tricks. Most motion detection is based on snapshots and finding brightness of a pixel with all combined colors, then comparing that to previous snapshots to detect enough variance and thus movement. If you have a webcam hooked up, this sample in Flash AS3 highlights this well showing the camera on the left, then the brightness snapshots on the right. It also has an indicator to the amount of movement due to much brightness.

C#

Here is a nice example of motion detection using various motion detection algorithms in C#. This is built on the very slick AForge.NET Computer Imaging Library.

If you ever wanted your own motion detection or recording it is all possible with the basics of checking brightness and snapshots in the most simple form checking how much change or variance their was to bright pixels or the count of bright pixels compared to previous snapshots.

// Calculate white pixels

private int CalculateWhitePixels( Bitmap image )
{
    int count = 0;
    // lock difference image
    BitmapData data = image.LockBits( new Rectangle( 0, 0, width, height ),
        ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed );
    int offset = data.Stride - width;
    unsafe
    {
        byte * ptr = (byte *) data.Scan0.ToPointer( );
        for ( int y = 0; y < height; y++ )
        {
            for ( int x = 0; x < width; x++, ptr++ )
            {
                count += ( (*ptr) >> 7 );
            }
            ptr += offset;
        }
    }

    // unlock image
    image.UnlockBits( data );
    return count;
}

Flash AS2

In Flash this is also possible here is a good article from Flash8 that explains cycling through each pixel to compare the image data and implement motion detection from a webcam.

For instance the basics here show how you can compare each pixel and the change in the brightness for each pixel:

//accuracy
tolerance=10;

//color of the current pixel in the current snapshot
nc=now.getPixel(x,y);

//red channel
nr=nc>>16&0xff;

//green channel
ng=nc>>8&0xff;

//blue channel
nb=nc&0xff;

//brightness
nl=Math.sqrt(nr*nr + ng*ng + nb*nb)

//color of the same pixel in the previous snapshot
bc=before.getPixel(x,y);

//red channel
br=bc>>16&0xff;

//green channel
bg=bc>>8&0xff;

//blue channel
bb=bc&0xff;

//brightness
bl=Math.sqrt(br*br + bg*bg + bb*bb);

//difference in brightness between now and before
d=Math.round(Math.abs(bl-nl));

if(d>tolerance)
{
//there was a change in this pixel
}

Flash AS3

Here is a link to grab a conversion of the AS2 Flash motion detection above to AS3.

Source of AS3 motion detection here.

Grant Skinner has done some interesting things with motion detection with Flash and webcams in the past in the incomplet gallery.

C# or other hardware accelerated capable kits are faster but AS3 and Flash with the new AVM2 virtual machine  should be about 10 times faster than AS2 as much of the improvement in performance and the virtual machine is on iteration speed increases such as loops (i.e. pixel loop).

Thursday, September 20th, 2007

UPDATE: See a sample of this Preloader that allows many files to be downloaded.

There are some great videos and links on the downloader object from Microsoft for Silverlight.

In Silverlight you can create an object called a downloader that probably stems from the htc ie5.5 downloader that was part of ajax beginnings, it is ver similar to the XMLHTTP object but it has zip capabilities. This downloader can download individual files or zip file packages that you can grab xaml, xml, images, script etc from them and use them or load them in to xaml files. This helps to keep things compact and allows for preloaders and smoother beginnings of your animations where needed.

The zip downloading is attractive and much of this is going on in Flash AS3 with FZip or ASZip AS3 packages. These are made possible with ByteArray and BinarySockets and other fun tools that give no limits. In Silverlight all you have is javascript or the silverlight downloader object to get other data at runtime.

Downloading assets in Silverlight is different than in Flash. Flash compiles libraries to swf files, it can load pngs, it can preload anything in numerous ways. Silverlight does not compile to one file or into compressed binary currently and this offers flexibility but it also makes file sizes at their default size very big. PNGs for instance can’t be zipped any smaller usually but in Flash9 these are sometimes 1/10th the size of the actual file when compressed better in the SWF file. Even if you zip them in Silverlight they are still around the same size. Silverlight XAML files compress well and these can get very large if lots of pathing is used.

Flash has always been the master at smallest file sizes and even the player itself is extremely small, under a MB. Silverlight 1.0 which is just javascript is 1MB, the Silverlight 1.1 plugin with DLR support is going to be 4MB currently (it still is in alpha so it could come down).

Currently in Silverlight 1.0 this is just a javascript release, the DLR and C# version of Silverlight 1.1 Alpha probably has 6 months or a year. So with no byte array binary handling or sockets of any type except javascript ajax, downloader is your only option now. But it is a good tool for now to help with preloaders and bringing files in smaller to make your rich applications and games faster.
Currently the only createObject call that is supported is the “downloader” object. I would like to see more here like a “socket” object but when Silverlight 1.1 launches with C#/python/ruby support things may change in this regard.

To create a downloader you do this in javascript:

[sourcecode language='jscript']
// Event handler for initializing and executing a download request.
function onMouseLeftButtonUp(sender, eventArgs) //sample mouse button click
{
// Retrieve a reference to the plugin.var slPlugin = sender.getHost(); // Create a Downloader object.
var downloader = slPlugin.createObject(“downloader”);

// Add DownloadProgressChanged and Completed events.
downloader.addEventListener(“downloadProgressChanged”, onDownloadProgressChanged);

downloader.addEventListener(“completed”, onCompleted);

// Initialize the Downloader request.
// NOTE: downloader APIs disallow file: scheme
// you must run this sample over localhost: or off a server or the following call will fail
downloader.open(“GET”, “promo.zip”);

// Execute the Downloader request.
downloader.send();
}

// Event handler for updating visual progress indicator
function onDownloadProgressChanged(sender, eventArgs)
{
// Calculate the downloaded percentage.
var percentage = Math.floor(sender.downloadProgress * 100);
// Update the Rectangle and TextBlock objects of the visual progress indicator.
progressText.text = percentage + “%”;
progressRectangle.width = percentage * 2;
}

function onDownloadCompleted(sender, eventArgs)
{
// Retrieve the XAML content from the downloaded package file.
var jacketBrowserXaml = sender.getResponseText(“jacketBrowser.xaml”);

// Create the objects from the XAML content.
var jacketBrowser = plugin.content.createFromXaml(jacketBrowserXaml);

// Add downloaded XAML content to the plugin.
sender.findName(“rootCanvas”).children.insert(0, jacketBrowser);

// Retrieve a reference to the Image object representing the jacket.
var jacketImageSlice = sender.findName(“jacketSlice”);

// Set the Source property of the Image object to the specific jacket image
// within the downloaded Zip package file.

jacketImageSlice.setSource(sender, “rotation01_green.png”);
}[/sourcecode]
This video is the quickest way to understand the whole downloader setup.But this is also a very simple example. The hard part is deciding where to put your preloaders and how to load things in. If you are putting your XAML in a zip file you will need to have a preloader in a container XAML file as the main control and then a Canvas object to load that XAML into. This complicates many things and is really a design approach from the beginning.

In coming posts I will be posting a multifile preloader, a single file preloader, a zip preloader and some sample setups for silverlight projects and Flash projects based on zip and direct preloading.

I will put this sample script to work below for preloading files in Silverlight 1.0:
[sourcecode language='jscript']
///////////////////////////////////////////////////////////////////////////////
//
// PreloaderMultifileClassLibrary.js
// The MIT License
//
// Copyright (c) 2007 Ryan Christensen, drawk llc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////

// setup the namespace objects to be good javascript form
// Ag1 = Silverlight 1.0 library
if(!window.B)
window.B={};
if(!window.B.Ag1)
window.B.Ag1={};

if (!window.Sys)
window.Sys = {};

if (!window.Silverlight)
window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
return function() {
return method.apply(instance, arguments);
}
}

B.Ag1.PreloaderMultifileAssets = function()
{
// TODO alow JSON or Array passed
// loads up the asset arry to cycle through
this.resourceArray = new Array();
}

B.Ag1.PreloaderMultifileAssets.prototype =
{
loadAssetsArray : function()
{
this.resourceArray[0] = “spinner.png”;
// add your images/zips/files to load here
for (var i=1; i<=16; i++)
{
this.resourceArray[i] = “drive_in_files/image” + i + “.png”;
}
this.resourceArray[i++] = “drive_in_files/bg.png”;
this.resourceArray[i++] = “drive_in_files/c1.png”;
this.resourceArray[i++] = “drive_in_files/c2.png”;
this.resourceArray[i++] = “drive_in_files/c3.png”;
this.resourceArray[i++] = “drive_in_files/c4.png”;
}
}

// total pages available
B.Ag1.PreloaderMultifile = function(control)
{
this.plugIn = control; // Store the host plug-in
this.currentDownload = 0; // Current resource to be downloaded
// this.maxNumPages = maxNumPages;
this.preloaderMultifileAssets = new B.Ag1.PreloaderMultifileAssets();
this.preloaderMultifileAssets.loadAssetsArray();
// alert(this.preloaderMultifileAssets.resourceArray);
this.downloadAssets();
}

B.Ag1.PreloaderMultifile.prototype =
{
downloadAssets : function()
{
//alert(this.preloaderMultifileAssets.resourceArray);
var _file = this.preloaderMultifileAssets.resourceArray[this.currentDownload];
if(_file != null && _file != undefined)
{
this.downloader = this.plugIn.createObject(“downloader”);
this.downloader.addEventListener(“downloadProgressChanged”, Silverlight.createDelegate(this, this.downloadProgressChanged));
this.downloader.addEventListener(“completed”, Silverlight.createDelegate(this, this.downloadCompleted));
this.downloader.open(“GET”, this.preloaderMultifileAssets.resourceArray[this.currentDownload]);
this.downloader.send();
}
},
downloadProgressChanged : function(sender, args)
{
try
{
var progressRect = this.plugIn.content.findName(“progressRect”);
progressRect.width = (sender.downloadProgress) * 450;
}
catch(e)
{
}
},
downloadCompleted : function(sender, args)
{
this.currentDownload++;
var progressText = this.plugIn.content.findName(“progressText”);
var progressPercent= this.plugIn.content.findName(“progressPercent”);
if (this.currentDownload < this.preloaderMultifileAssets.resourceArray.length)
{

progressText.text = “Downloading: ” + this.preloaderMultifileAssets.resourceArray[this.currentDownload];
progressPercent.text = parseInt((this.currentDownload/this.preloaderMultifileAssets.resourceArray.length)*100) + “%”;
this.downloader.open(“GET”, this.preloaderMultifileAssets.resourceArray[this.currentDownload]);
this.downloader.send();
}
else
{
// Hide progress UI
var downloadUI = this.plugIn.content.findName(“downloadUI”);
this.plugIn.content.findName(“fadeDownloadUI”).begin();
downloadUI.isHitTestVisible = false;

progressPercent.text = “100%”;
progressText.text = “Downloading Complete”;

// initialize canvas and anmiations
this.plugIn.content.findName(“innerCanvas”).Visibility = ‘Visible’;
this.plugIn.content.findName(“IntroAnimation”).begin();
}
}
}

[/sourcecode]
If you change your Silverlight handleLoad to something like this you can use the class above for list of files before it plays the xaml animations and presentation.
[sourcecode language='jscript']
handleLoad: function(plugin, userContext, sender) { // be sure to add these parameters, by default they are not added
// alert(plugin.id + ” : ” + userContext + ” : ” + sender.toString());
plugin.content.findName(“innerCanvas”).Visibility =’Collapsed’;
plugin.content.findName(“innerCanvas”).Opacity = 0;
var preloader = new B.Ag1.PreloaderMultifile(sender.getHost()); // pass in the silverlight control

[/sourcecode]
I will have a complete tutorial of this soon.