This Part of the Shaders Tutorial series will show you how to add an Environmental Material Shader to your objects.

What this basically does is take a texture imported from the background and use it to reflect off the object, giving the object a highly reflective coating which is faked. This keeps processing power down, while giving a nice effect.

For this tutorial we will be creating an effect that looks something like this: (please be patient while the video loads)

 

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.

To begin with, we must begin our new package and import our classes.

package environmentMat{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;

	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.EnvMapMaterial;
	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;
	//Ability to add materials
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.MaterialsList;

Then initialise our class and create the variables that will be using

public class environmentMat extends Sprite {
		private var view:BasicView;
		private var img:Loader;
		private var environment:EnvMapMaterial;
		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;

Finally we must create our functions that will load the .dae file, add the text, add the env map texture, add the light, and animate the object and/or the light. The example below shows both methods.

public function environmentMat() {
	view = new BasicView(300, 300, false, false, CameraType.FREE);
	view.renderer = new BasicRenderEngine();

	addChild(view);

	img = new Loader();
//this is where you specify your texture file and directory, to make things easy
//make sure it is in the same route directory as your .as file
	img.load(new URLRequest("assets/gold.jpg"));
	img.contentLoaderInfo.addEventListener(Event.COMPLETE, loaddae);
		}

private function loaddae(e:Event):void {
	var envMap:BitmapData = e.target.content.bitmapData;
	light = new PointLight3D(true);
	light.z = -400;
	light.y = 300;
	environment = new EnvMapMaterial(light, envMap, envMap, 0x555555);

	daeFile = "YOUR_DAE_FILE_NAME.dae";
	dae = new DAE();
	dae.load(daeFile,  new MaterialsList ( { all:environment } ));
	dae.scale = 100;

	format = new TextFormat("Arial", 10, 0xFFFFFF);
	tf = new TextField();
	tf.width = 200;
	tf.text = "Environment Map Material Shader";
	tf.x = 10;
	tf.y = 10;
	tf.setTextFormat(format);
	addChild(tf);

	view.scene.addChild(dae);
	addEventListener(Event.ENTER_FRAME, onRenderViewport);
	}
	private function onRenderViewport(e:Event):void {
	animateLight();
			view.singleRender();
			dae.rotationY  ;
		}
		private function animateLight():void {
			angle  = 2 * (Math.PI/180);
			light.x = Math.cos(angle) * 200;
			light.z = Math.sin(angle) * 200;
		}
	}
}

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 environmentMat.environmentMat;
	import flash.display.Sprite;

	public class MainShader extends Sprite {
		private var environment:environmentMat;

		public function MainShader() {
			environment = new environmentMat();
			environment.x = 500; //this is the position of the object
			//you may want to change this number to place the object
			//in your desired place. environment.x move in the x axis
			//change x to y for y axis and to z to z axis

			addChild(environment);
		}
	}
}

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!

2 Responses to “Environmental Material Shader with dae objects”

  • jonesNo Gravatar says:

    Hi,
    very nice tut…helped me a lot…anyway I´m not sure but is there a typo in the animateLight function?
    The way it is here the light will never move as there is no variable number in the function.
    I followed your example with my collada model an it looks great except the shading does not change as the model turns.

  • adminNo Gravatar says:

    Hi,
    No this is not a typo, the light in this tutorial has been made static for a reason, as it was not necessary with the texture being used.
    However if you want the light to rotate have a look at one of the other tutorials for example the Phong Shader: http://thierryzoghbi.co.uk/blog/2009/05/phongshader/

    near the bottom you will find the Function “animateLight”

    Hope that helps.

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.