Author Topic: Make SHADOWS work on loaded f3d files  (Read 291 times)

Anton Azarov   «   on: February 06, 2011, 12:19:46 AM »
If we can use shadowplane - logically we can have a shadows. But here is a big limitation - we can't add shadows for not plane mesh.

For example I made landscape with cliffs and landscape have hight and low heights. If we will use shadowplane we can't get correct shadows )

May be in 2.0 this will be fixed and we can use simply feature like

light01.renderShadows = true;

Anton Azarov   «   Reply #1 on: February 06, 2011, 12:20:50 AM »
I know I can using bitmapdata make a shadows. But this is to hard ) Better have this feature that can be applied to any mesh.

Ariel Nehmad   «   Reply #2 on: February 06, 2011, 12:23:20 PM »
hi anton, as you know, doing shadows calculations for a software renderer it is very very heavy, and it requires pixel level calculations...so it will no performance....

this is the reason we called shadow plane and not shadows.

Bruce Hammond   «   Reply #3 on: February 08, 2011, 05:43:21 PM »
Hi, Anton.  I've had the same problem with contoured surfaces and shadows.  Here's an example of a work-around I came up with, without using a ShadowPlane...

http://www.e-nimation.com/Flare/shadows/fakeShadow.html

In this example I have three Pivot3Ds: a simple ball (Sphere from .max), the ground (a displaced Plane), and the shadow, which is a disk with a material set at 0.2 alpha (it's just the top of a cylinder).

I applied a SphereCollision to the ball, and a RayCollision to the shadow, both interacting with the ground.  The SphereCollision is a standard one.  With the shadow, the x and z coordinates are always the same as the ball's x and z coordinates; the RayCollision data is only applied to the shadow's y-coordinate.

Here's the code for the example:

package{
 import flash.events.*;
 import flash.display.*;
 import flash.geom.*;
 
 import flare.basic.*;
 import flare.core.*;
 import flare.collisions.*;
 import flare.system.*;
 import flare.utils.*;
 
 public class FauxShadow extends Sprite{
  private var scene:Scene3D;
  private var ball:Pivot3D;
  private var fShadow:Pivot3D;
  private var ground:Pivot3D;
  private var ballGravity:SphereCollision;
  private var shadowRay:RayCollision;
  
  public function FauxShadow(){
   scene = new Viewer3D(this);
   scene.camera.fieldOfView = 25;
   scene.addEventListener(Scene3D.COMPLETE_EVENT, onLoaded);
   ball = scene.addChildFromFile("ball.f3d");
   ground = scene.addChildFromFile("terrain.f3d");
   fShadow = scene.addChildFromFile("ballShadow.f3d");
   fShadow.setPosition(ball.x,0,ball.z);
   ball.setScale(0.5,0.5,0.5);
   fShadow.setScale(0.5,0.5,0.5);
  }
  
  private function onLoaded(e:Event):void{
   ballGravity = new SphereCollision(ball,12,new Vector3D(0,25,0));
   ballGravity.addCollisionWith(ground);
   shadowRay = new RayCollision();
   shadowRay.addCollisionWith(ground);
   scene.addEventListener(Scene3D.UPDATE_EVENT, update);
   scene.removeEventListener(Scene3D.COMPLETE_EVENT, onLoaded);
  }
  
  private function update(e:Event):void{
   ball.y -= 2;
   ballGravity.slider();
   
   var shadowPos:Vector3D = new Vector3D(fShadow.x,500,fShadow.z);
   if(shadowRay.test(shadowPos, Vector3DUtils.DOWN)){
   var info:CollisionInfo = shadowRay.data[0];
   fShadow.setNormalOrientation(info.normal, 0.3);
   fShadow.setPosition(ball.x,info.point.y,ball.z);
   }
   //control ball movement wih keyboard input   
   if(Input3D.keyDown(Input3D.UP)) ball.translateZ(2);
   if(Input3D.keyDown(Input3D.DOWN)) ball.translateZ(-3);
   if(Input3D.keyDown(Input3D.LEFT)) ball.translateX(-3);
   if(Input3D.keyDown(Input3D.RIGHT)) ball.translateX(3);
   if(Input3D.keyDown(Input3D.SPACE)) ball.translateY(8);
  }
 }
}

This approach has obvious limitations. For instance, the shadow Pivot3D is static, so if you have a character waving his arms, the shadow will not reflect the waving.  On the other hand, I find this a relatively simple alternative, and it puts MUCH less demand on the computer's processor than even a ShadowPlane.

I hope this is of some help.

- bruce