This Part of the Shaders Tutorial series will show you how to add Cell shader to either a dae imported object or to a basic primitive.

If you are a novice, it is advised that you follow this tutorial from the initial section that can be found here, which explains how to set up the files and the scene. Otherwise please read on.

For those following on the shaders tutorial series, i will be calling this .as file cellMat

package cellMat{

we then need to import our classes

import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;

	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.CellMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.view.BasicView;
        import org.papervision3d.objects.parsers.DAE;
        import org.papervision3d.objects.DisplayObject3D;

now we must initiate our class, and set our variables

public class cellMat extends Sprite
	{
		private var view:BasicView;
		private var cell:CellMaterial;
		private var light:PointLight3D;
		private var sphere:Sphere;
		private var angle:Number = new Number(0);

		private var tf:TextField;
		private var format:TextFormat;

		 private var dae:DAE;
                private var daeFile:String;
                private var daeMaterialName:String;

now we must enter in our function, this is where all the fun begins, where we import our dae object file, apply our cell shader, add the light source etc.

public function cellMat()
	{
//we must overwrite basic view method, in order to controll our camera
//initiate our Rendering Engine
	view = new BasicView(300, 300, false, false, CameraType.FREE);
	view.renderer = new BasicRenderEngine();
        addChild(view);
//We now add a Point Light, specify its position
	light = new PointLight3D(true); light.z = -800; light.y = 300;
//create a new cellMaterial, and specify its colour, and reflection colour
	cell = new CellMaterial(light, 0xFFFFFF, 0xEEC900, 12);
//import our dae file - make sure its in the same root directory
//as you .as file
       daeFile = "newscale.dae";
       dae = new DAE();
//apply the material shader to the dae object
       dae.load(daeFile, new MaterialsList ( { all:cell } ));
//sometime the object needs rotating and/or scaling
//rotationX, rotationZ can also be used
	dae.rotationY = 90;
        dae.scale = 100;
	view.scene.addChild(dae);
//adding the title text and formating it
	format = new TextFormat("Arial", 10, 0xFFFFFF);
	tf = new TextField(); tf.width = 200; tf.text = "Cell Material Shader";
	tf.x = 10; tf.y = 10; tf.setTextFormat(format);
	addChild(tf);
//adding an event listener so that we can controll the object and light
	addEventListener(Event.ENTER_FRAME, onRenderViewport);
	}

private function onRenderViewport(e:Event):void
	{
//you can choose to either animate the object itself,
//or to animate the light
		animateLight();
		//dae.rotationY++;
		view.singleRender();
	}

private function animateLight():void
	{
	angle += 4 * (Math.PI/180);
	light.x = Math.cos(angle) * 800;
	light.z = Math.sin(angle) * 800;
	}
    }
}

If you have been following this tutorial series from the beginning, you will want to add the following code to your MainShader.as file that you created, in order to run your movie.

package {
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.view.BasicView;
	import cellMat.cellMat;

	public class Mainscale extends Sprite {
		private var cell:cellMat;

		public function Mainscale() {
			cell = new cellMat();
			cell.x = 250;
			cell.y = 300;
			addChild(cell);
		}
	}
}

If you like this tutorial, why not sign up to our RSS feeds and recieve the latest updates straight to your inbox/reader!

Feel free to leave a comment if you require an help/suggestions, and I will get back to you ASAP. 😀

Be Sociable, Share!

Leave a Reply

This site is using OpenAvatar based on

Subscribe
Follow me on Twitter
free counters
PayPal Donation
If you like this blog and the help it has given you, why not donate for this good cause? Click on the button below to pay Safely and Securely through PayPal.
Thanks.