Phong Shading on loaded materials and Environment Mapping in Papervision3D

Using lights in Papervision3D to create objects having a Phong Shading on flat coloured materials are easy enough, but what about when you want dynamic highlights on jpg/png materials that you load in?

After making a light like below…

var light:PointLight3D = new PointLight3D(false, false); //(showlight, flipped)
light.x = 0;
light.y = 0;
light.z = -500;
default_scene.addChild(light);

You can use the phong shader to your loaded material file (in this case, embedded material file as we’re continuing on from my previous post).

var bitmap1:Bitmap = new matBlueBall() as Bitmap;
var bitmapMaterial1:BitmapMaterial = new BitmapMaterial(bitmap1.bitmapData, true);
var shader:PhongShader = new PhongShader(light,0xffffff,0x333333,200);
var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial1,shader);
_model_balls_blue.materials.addMaterial(shadedMaterial, "blue_ball");

The phong shaded material om the spheres looked like this in Flash – it looks very dark because there’s only one point light.

For greater control over “ambient” lighting, you can use an environment map instead.


Embed an environment map to your class. In this exmaple, the envionment map looked like the image on the left – the light grey for the environment (ambient) and the white spot for the specular highlight.

public class KakesPapervision3DTest extends PaperBase
{
[Embed(source="mat/env.jpg")] private var EnvImage:Class;
private var envMap:Bitmap = new EnvImage();
[Embed(source="dae/blue_ball.dae", mimeType = "application/octet-stream")]
private var daeBlueBall:Class;
[Embed(source="mat/blue_ball.jpg")]
private var matBlueBall:Class;
...

…and replace PhontShader with EnvMapShader…

private function initMaterials():void
{
var bitmap1:Bitmap = new matBlueBall() as Bitmap;
var bitmapMaterial1:BitmapMaterial = new BitmapMaterial(bitmap1.bitmapData, true);
var shader = new EnvMapShader(light, envMap.bitmapData, envMap.bitmapData, 0x333333);
var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial1,shader);
_model_balls_blue.materials.addMaterial(shadedMaterial, "blue_ball");
...

The specular highlight still follows the light as you rotate the scene, but now there’s enough “ambient light” that lights the spheres nicely.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s