homeへのリンクです。

Sandy 6

2007年07月09日

前回のサンプルのボックスをアニメーションでローテーションさせるには、以下のようにすればよいらしい。
ここのチュートリアルがすごい。

import sandy.core.data.*;
import sandy.core.group.*;
import sandy.primitive.*;
import sandy.view.*;
import sandy.core.*;
import sandy.skin.*;
import sandy.util.*;
import sandy.core.transform.*;
import sandy.events.*;
import sandy.core.light.*;
 
function init( Void ):Void
{
   // we create our camera
   var cam:Camera3D = new Camera3D( 600, 600 );
   // we add the camera to the world
   var world:World3D=World3D.getInstance();
   world.addCamera( cam );
   // we move the camera backward to be able to see the object placed at 0,0,0
	cam.setPosition(0, 0, -500);
	//We create the Light coming from the left with a low intensity (20)
	var l:Light3D = new Light3D (new Vector (1, 3, -3), 50);
	// set the world light 
	world.setLight (l);
   // we create the root node.
   var bg:Group = new Group();
   // and set it as the root node of the world.
   world.setRootGroup( bg );
   // and we lauch the scene creation
   createScene( bg );
   // On the render event call rotate()
	world.addEventListener (World3D.onRenderEVENT, this, rotate);
	// and now everything is created, we can launch the world rendering.
   world.render();
}
var rotation:Transform3D;
function createScene( bg:Group ):Void
{
   // We create our object. It is a cube of 50 pixels, and with the quad mode, so here four points per face.
	var o:Object3D = new Box (50, 50, 50, 'quad');
	// we create a skin to "dress up" the object a bit, and make it visually better.
	//  SimpleColor skin is used here, it needs the fill color, alpha, and thickness
	var skin:Skin = new MixedSkin (0x00FF00, 100, 0, 100, 1);
	// NEW PART, we enable the light
	skin.setLightingEnable(true);
	// We apply the skin to the object
	o.setSkin (skin);
	// We create two TransformGroup instances. Each one corresponds to a node of the tree we are going to create.transformations
	var tg1:TransformGroup = new TransformGroup ();
	var tg2:TransformGroup = new TransformGroup ();
	// We also create two instances of Translation objects.
	// Give them an appropriate name, this becomes important very rapidly.
	var translation:Transform3D = new Transform3D ();
	rotation = new Transform3D ();
	// we translate the object by 500 pixels on the Z axis.
	translation.translate (0, 0, -300);
	// We rotate the object by 30 degrees on an axis oriented from the origin to the point (1, 1, 1).
	rotation.rotAxis (new Vector (1, 1, 1), 30);
	// We apply those transformations to the nodes.
	tg1.setTransform (translation);
	tg2.setTransform (rotation);
	// We add the rotation transformation to the node which represents the rotation by setting it as a child.
	// The rotation will be applied first, translation later (higher in the tree)
	tg2.addChild (o);
	// Now we link the two nodes
	// The order here is very important.
	tg1.addChild (tg2);
	// Now we simply link the translation node to the root node, thus completing the tree creation
	bg.addChild (tg1);
}
var degree:Number=30;
function rotate(){
	var tempdegree:Number=(degree++)%360;
	rotation.rotAxis (new Vector (1, 1, 1), tempdegree);
}
// We lauch the animation creation.
init();

swfはこんな感じ。
フレームレートは24fpsにしている。