3D Silverlight 1.0: Simple Collada parser
Posted by Dennis on Feb 20, 2008 in 3D, Silverlight • 5 comments
A while ago I made a very simple Collada parser that was written in JavaScript and drew on a HTML 5 <canvas>.
Silverlight 1.0 also makes use of JavaScript so converting it seemed like a good excercise. You can see it here. Fortunately I didn’t have to change much. Only the way in which the lines are drawn and filled.
Compare the JavaScript/HTML 5 syntax (this.ctx is the drawing context):
this.ctx.beginPath();
this.ctx.moveTo( point1.x, point1.y + 500 );
this.ctx.lineTo( point2.x, point2.y + 500 );
this.ctx.lineTo( point3.x, point3.y + 500 );
this.ctx.closePath();
this.ctx.stroke();
if( this.enableZFlatShading || this.enableFlatShading ) this.ctx.fill();
To the way it’s done in Silverlight 1.0 (if there’s another way to do this, please tell me, because I haven’t found out yet):
this.drawLine = function( fillColor, strokeWidth, strokeColor, point1, point2, point3 )
{
var xamlString = '<Path Stroke="' +strokeColor+ '" StrokeThickness="' +strokeWidth+ '" StrokeLineJoin="Miter" StrokeMiterLimit="0" '
if( fillColor != null )
xamlString += 'Fill="' +fillColor+ '" ';
xamlString += 'Data="M' + point1.x +','+ ( point1.y + 500 ) +
' L' + point2.x +','+ ( point2.y + 500 ) +
' ' + point3.x +','+ ( point3.y + 500 ) +
' z"/>';
var line = this.control.content.CreateFromXaml( xamlString );
this.rootElem.children.add( line );
};
A bit more elaborate and less structured as you can see.
The Silverlight Downloader Object
Another thing I changed is the asynchronous loading of the collada document. Instead of using a JavaScript XMLHTTP request I use Silverlight’s Downloader object:
var plugin = self.rootElement.getHost();
var downloader = plugin.createObject( "downloader" );
downloader.addEventListener( "completed", self.parseCollada );
downloader.addEventListener( "downloadfailed", self.showErrorMessage );
downloader.open( "GET", colladaFile );
downloader.send();
There’s still a little cross browser stuff to do in the handler for the completed event:
var responseXML;
if ( window.ActiveXObject )
{
responseXML = new ActiveXObject( "Microsoft.XMLDOM" );
responseXML.async = false;
responseXML.loadXML( sender.responseText );
}
else
{
var parser = new DOMParser();
responseXML = parser.parseFromString( sender.responseText, "text/xml" );
}




cool. can you have the objects rotate?
Hey Felix,
That can be done but I think the performance will be terrible. JavaScript is just too slow for this. As an example, check out this 3D engine for Silverlight: http://www.markdawson.org/kit3d/
It uses low poly cubes and is already slow. Can’t wait for C# support …
Kit3D now does have a C# version, check out these demos that were created usng it, the Sphere has 2000 polygons: http://www.sildev.net/3DDemo/index.html
Mark.
Thank you from Poland
Was wondering if you are aware of some progress relative to Silverlight loading COLLADA models ?
I understand Silverlight is now at rev 3 and offering 3D?
Is there a link between silverlight and microsoft robotic studio ?