Thursday, August 25, 2011

Simple Ray Tracing Voxel Demo

Just a simple demo using Ray Tracing to render Voxels.
I use a 1D array - voxelData[] to represent voxels/3D points with colors. It's easier for understanding if I use a 3D array, voxelData[x][y][z], where x, y, z is the position of the voxel in 3D space and the value of the array is color for that voxel. Note that voxelData[x][y][z] = voxelData[x<<16|y<<8|z] if the dimension is set to 256 at max.

The rendering idea is simple, since we want to fill the screen using colors, for each pixel in the screen, trace a ray, and march the ray in the voxel space, until the ray hit a voxel, then we get the color and write it to the screen. This is the so-called "first-hit" algorithm.

Ray tracing is slow,  a fast way to render voxel is ray casting, such as the algorithm used in Bengine. But using ray tracing, you can easily add some reflection/shadow effect after calculate normal vectors of the voxels.

Source code:
(similar to the source code of the voxel ray tracing demo in my earlier post)

 Update: November, 27, 2011

Ray Tracing Voxel Demo using 3D DDA/Bresenham's line algorithm

Source code:
More about Bresenham's line algorithm:;topicseen#msg9213

Friday, August 19, 2011

Use vLib.swc to manage flash project resources and protect swf assets

Update: 2012/12/21
vLib v0.15 released!
What's new:
Fixed several bugs, disable useless traces.
Add new sound API of Flash 11.

myLib.playSound("MP3SoundName");//Play the mp3 sound from mp3 file directly! 

vLib - the virtual library project, is a AS3 swc lib to handle your flash project resources, such as bitmaps, swfs, mp3 sounds, texts, xmls and binary files.
It will ease you from importing assets into your .fla's library, writing lots of Embed stuffs for the Flex project, and save your efforts of using lots of URLLoader and Loader codes.
Only need to write a XML file and markup the name and URL address for all flash resources, you can then access them using simple AS3 api provided by vLib.swc.

Tutorial of using vLib.swc, Step by Step:

Step 1[Prepare the test resources files]:
Create a folder and put all the resources(such as Binary files, mp3 sounds, swfs, text files, xml files, .JPG and .PNG pictures) into it.

Step 2[Write the description xml]:
Write a xml file of the resources' names and URLs.
The XML must begin and end with

There're 6 kinds of resources, each kind has a tag, they're:
 for all text files
 for all picture files
 for all swf files
 for all mp3 files
 for all binary files
All resource files should be written into the corresponding tag of the format
The details of the description XML's format are showed in the example.
Example["vLib.xml" from vLib\examples\bin\DataFiles]:





Note: Due to formatting issues, the above xmls don't display correctly, all tag names should be in upper case in the first character, for details, please see the examples in the svn.

Step 3[Add the vLib.swc to library]:
Add the "vLib.swc" to your flash/flex project, so you can use it in your AS3 code.

Step 4[Initialize the virtual library]:

First declare an instance and provide the the description xml file "vLib.xml" to it.
import Lib.*;
var myLib:Lib = new Lib();
myLib.addEventListener(Event.COMPLETE, Libloaded);
function Libloaded(event:Event):void
//All Resources Loaded!

Step 5[Access resources from the virtual library]:
As showed in the code, once all resources are loaded, it will triger the callback function "Libloaded", that's the time you can use them.
function Libloaded(event:Event):void
//All Resources Loaded!
function test():void
myLib.load(new DataFiles_Class());
var test = myLib.getSwf("enemydead");
test.x = 100;

addChild(new Bitmap(myLib.getBitmapData("PNGfile")));
}//end of test

Step 6[Export Data files as compressed binary package]:
Export all resources as a single compressed binary file and protect your swf assets.
Add this line:"myLib.vLib"); 
in function "Libloaded".
You will get a binary file named "myLib.vLib" which contains all the resources.

Step 7[Use the compressed data file package]:
myLib.addEventListener(Event.COMPLETE, Libloaded);
function Libloaded(event:Event):void
//All Resources Loaded!
Embed the file "myLib.vLib" as a binary asset:
private static const DataFiles_Class:Class;
myLib.load(new DataFiles_Class());
You can use the data files directly after calling load().
If you Embed all resources using Embed tag or import them into .fla's library, all resources will be compiled into SWF tags. SWF decompilers can read those tags and extract the resources directly.
But in this step, because all resources are encoded into a single binary file, it's a good way to protect the swf assets.

Step 8[Export Data files as a .as class]:
Export .as Class with Embeds:
myLib.XML2AS("./DataFiles/Lib.xml", "myLibT" /*, "myPackageT"*/);
You will get a .as file named "" as follows:
     import flash.display.*;
     import flash.text.*;
     import flash.utils.*;
//Export from vLib-v0.1 by Bruce Jawn (

     public class myLibT
     [Embed(source="DataFiles/texts/Textfile_0.txt", mimeType="application/octet-stream")]
     private static const Textfile_0:Class;
     [Embed(source="DataFiles/texts/Textfile_1.txt", mimeType="application/octet-stream")]
     private static const Textfile_1:Class;
     [Embed(source="DataFiles/xmls/Xmlfile_0.xml", mimeType="application/octet-stream")]
     private static const Xmlfile_0:Class;
     [Embed(source="DataFiles/xmls/Xmlfile_1.xml", mimeType="application/octet-stream")]
     private static const Xmlfile_1:Class;
     private static const JPGfile:Class;
     private static const PNGfile:Class;
     private static const enemydead:Class;
     private static const enemyfire:Class;
     private static const S0:Class;
     private static const S1:Class;
     private static const S2:Class;
     [Embed(source="DataFiles/bins/Binaryfile_0.bin", mimeType="application/octet-stream")]
     private static const Binaryfile_0:Class;
     [Embed(source="DataFiles/bins/Binaryfile_1.bin", mimeType="application/octet-stream")]
     private static const Binaryfile_1:Class;

        private function getClass(name:String):Class
            var ClassReference:Class = getDefinitionByName("myLibT_"+name) as Class;
            return ClassReference;

  private function getInstance(name:String):Object
   var ClassReference:Class = getClass(name);
   return new ClassReference();
  public function getBin (name:String) : ByteArray
   return ByteArray(getInstance(name));
  public function getBitmapData (name:String) : BitmapData
   return Bitmap(getInstance(name)).bitmapData;

        public function getFont (name:String) : Class
   return getClass(name);
   //return Font(getInstance(name));
  public function getSound (name:String) : Sound
   return Sound(getInstance(name));

  public function getSwf (name:String) : Sprite
   return Sprite(getInstance(name));

  public function getText (name:String) : String
   return String(getInstance(name));

  public function getXML (name:String) : XML
   return XML(getInstance(name));
 }//end of class 
}//end of package
Then you can import this class into your AS3 projects and compile everything into the final single SWF.

vLib.swc API references:
vLib.swc v0.1 download:
Source code of vLib.swc examples: