Friday, April 16, 2010

PV3D use two different Material in one plane



If you want to do some Poker game,

you may want to know how to use two different Material in one plane,


You need to download this Class below

and import that class...

DoubleSidedCompositeMater.as
//=========================================== 
/**
 * copy right By http://www.nabble.com/attachment/16002163/0/DoubleSidedCompositeMater...
 *
 * fix to Papervision3D ActionScript 3.0 Library .ZIP rev 839
 * http://papervision3d.googlecode.com/files/Papervision3D_rev839.zip
 *
 * Mark in 2008.12.28 By tenchiwang
 *
 */
package fly.papervision3D.materials.special {
 import flash.display.BitmapData;
 import flash.display.Graphics;
 import flash.geom.Matrix;
 import org.papervision3d.core.render.command.RenderTriangle;
//      import org.papervision3d.core.geom.renderables.Triangle3D;   
 import org.papervision3d.core.geom.renderables.Vertex3DInstance;
 import org.papervision3d.core.material.TriangleMaterial;
 import org.papervision3d.core.proto.MaterialObject3D;
 import org.papervision3d.core.render.data.RenderSessionData;
 import org.papervision3d.core.render.draw.ITriangleDrawer;

 public class DoubleSidedCompositeMaterial extends TriangleMaterial implements ITriangleDrawer {
  protected var materials:Array;

  public function DoubleSidedCompositeMaterial(){
   _initialize();
  }
  ;

  private function _initialize():void {
   materials = new Array();
  }
  ;

  public function addMaterial(material:MaterialObject3D):void {
   materials.push(material);
  }
  ;

  public function removeMaterial(material:MaterialObject3D):void {
   materials.splice(materials.indexOf(material), 1);
  }
  ;

  public function removeAllMaterials(material:MaterialObject3D):void {
   materials.splice(0);
  }
  ;

  override public function drawTriangle(face3D:RenderTriangle, graphics:Graphics, renderSessionData:RenderSessionData, altBitmap:BitmapData = null, altUV:Matrix = null):void {
   var vertex0:Vertex3DInstance;
   var vertex1:Vertex3DInstance;
   var vertex2:Vertex3DInstance;
   var x0:Number;
   var y0:Number;
   var x1:Number;
   var y1:Number;
   var x2:Number;
   var y2:Number;
   var draw:Boolean;
   var material:MaterialObject3D;
   for each (material in materials){
    draw = true;
//                              vertex0 = face3D.v0.Vertex3DInstance(); 
//                              vertex1 = face3D.v1.Vertex3DInstance(); 
//                              vertex2 = face3D.v2.Vertex3DInstance(); 
    vertex0 = face3D.v0.clone();
    vertex1 = face3D.v1.clone();
    vertex2 = face3D.v2.clone();
    x0 = vertex0.x;
    y0 = vertex0.y;
    x1 = vertex1.x;
    y1 = vertex1.y;
    x2 = vertex2.x;
    y2 = vertex2.y;
    if (material.opposite){
     if ((x2 - x0) * (y1 - y0) - (y2 - y0) * (x1 - x0) > 0){
      draw = false;
     }
     ;
    } else {
     if ((x2 - x0) * (y1 - y0) - (y2 - y0) * (x1 - x0) < 0){
      draw = false;
     }
     ;
    }
    ;
    if (draw){
     material.drawTriangle(face3D, graphics, renderSessionData);
    }
    ;
   }
   ;
  }
  ;
 }
 ;
This is the sample file code:
package {
import flash.display.*;
import flash.events.*;
import org.papervision3d.materials.BitmapAssetMaterial;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.objects.DisplayObject3D;

import fly.papervision3D.materials.special.DoubleSidedCompositeMaterial;

public class card extends MovieClip {
private var scene:Scene3D;
private var viewport:Viewport3D; 
private var camera:Camera3D; 
private var renderer:BasicRenderEngine; 
private var plane:Plane;



public function card():void {
viewport = new Viewport3D(550, 400, true, false);
this.addChild(viewport);
renderer = new BasicRenderEngine();
camera = new Camera3D();
scene = new Scene3D();
initObject();
this.addEventListener(Event.ENTER_FRAME, onEventRender3D);
}

private function initObject():void {
var CardMat1:BitmapAssetMaterial = new BitmapAssetMaterial("Card1", true);
var backMat:BitmapAssetMaterial = new BitmapAssetMaterial("CardBack", true);
backMat.precise = true;
backMat.opposite = true; 
//Let the other side of the material reverse
var _doubleMaterial:DoubleSidedCompositeMaterial = new DoubleSidedCompositeMaterial();
_doubleMaterial.addMaterial(CardMat1);
_doubleMaterial.addMaterial(backMat);
_doubleMaterial.doubleSided = true;
plane = new Plane(_doubleMaterial, 225, 400);
scene.addChild(plane);
}

private function onEventRender3D(e:Event):void {
plane.rotationY += 3
renderer.renderScene(scene, camera, viewport);
}
}
}

1 comment:

  1. Woot, that's brilliant, pv3d needed this kind of possibilities !

    Sadly the file is'nt hosted. Would you host it again please ?

    ReplyDelete