package org.papervision3d.materials.shadematerials
{
import fl.motion.Color;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.core.proto.CameraObject3D;
import org.papervision3d.core.geom.renderables.Triangle3D;
import org.papervision3d.core.geom.renderables.Vertex3DInstance;
import org.papervision3d.core.material.AbstractSmoothShadeMaterial;
import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.core.proto.LightObject3D;
import org.papervision3d.core.render.data.RenderSessionData;
import org.papervision3d.core.render.draw.ITriangleDrawer;
/**
* @Author Buzzler
*/
public class NormalMaterial extends AbstractSmoothShadeMaterial implements ITriangleDrawer
{
private static var x1:Number;
private static var x0:Number;
private static var x2:Number;
private static var y0:Number;
private static var y1:Number;
private static var y2:Number;
private var camera:CameraObject3D;
private var tmpN:Number3D;
private var tmpL:Number3D;
private var tmpR:uint;
private var tmpG:uint;
private var tmpB:uint;
private var tmpRGB:uint;
public function NormalMaterial(light:LightObject3D, camera:CameraObject3D)
{
super();
this.light = light;
this.camera = camera;
}
private static var useMap:BitmapData;
override public function drawTriangle(face3D:Triangle3D, graphics:Graphics, renderSessionData:RenderSessionData, altBitmap:BitmapData = null, altUV:Matrix = null):void
{
x0 = face3D.v0.vertex3DInstance.x;
y0 = face3D.v0.vertex3DInstance.y;
x1 = face3D.v1.vertex3DInstance.x;
y1 = face3D.v1.vertex3DInstance.y;
x2 = face3D.v2.vertex3DInstance.x;
y2 = face3D.v2.vertex3DInstance.y;
triMatrix.a = x1 - x0;
triMatrix.b = y1 - y0;
triMatrix.c = x2 - x0;
triMatrix.d = y2 - y0;
triMatrix.tx = x0;
triMatrix.ty = y0;
tmpN = face3D.faceNormal.clone();
tmpL = new Number3D(-light.x,-light.y,-light.z);
tmpR = uint((tmpN.x + 1.0) * 127.5);
tmpG = uint((tmpN.y + 1.0) * 127.5);
tmpB = uint((tmpN.z + 1.0) * 127.5);
tmpRGB = (tmpR << 16) | (tmpG << 8) | (tmpB);
graphics.beginFill(tmpRGB, brightness(tmpN, tmpL));
graphics.moveTo( x0, y0 );
graphics.lineTo( x1, y1 );
graphics.lineTo( x2, y2 );
graphics.lineTo( x0, y0 );
graphics.endFill();
renderSessionData.renderStatistics.shadedTriangles++;
}
private function brightness(n1:Number3D, n2:Number3D):Number
{
n1.normalize();
n2.normalize();
var result:Number = Math.acos(Number3D.dot(n1, n2)) - Math.PI / 2;
result = Math.max(result, 0) / (Math.PI / 2);
return 1 - result;
}
}
}