Add and use of Away3d light source (pointlight,directionallight)

Source: Internet
Author: User
Tags addchild

In order to make the three-dimensional scene more realistic, it is usually added to the stage lighting for light and shadow rendering. At present, Away3d provides two kinds of light sources: Point light source and parallel projection light source.

1, point light source and parallel projection light source

(1) Pointlight: Point Light source. No directionality, usually used to simulate the sun, the moon, or the lamp cannon.
(2) DirectionalLight: A parallel projection light source, or called a direct light source. The origin simulates the searchlight, or the Moonlight through the window and so on.

2, lighting containers

We can add multiple light sources, such as a parallel projection light that creates multiple orientations. Then put these light sources into the staticlightpicker this light container.

3, the use of light source

The need to use light in this world, the map of the model can be affected by the light, whether texturematerial or colormaterial have lightpicker attributes. We just have to give it to the Staticlightpicker type object.

4, ground Shadow

A parallel projection of the light source to the object can leave a shadow on the ground. For example, we use plane to do the ground, to achieve projection shadow can be the following operations

Planematerial.shadowmethod = new Filteredshadowmapmethod (LIGHT1);

5, complete sample

(1) Use plane to do ground. The stage adds two squares, a pure white one using a map.
(2) The stage adds a point light source, a direct light source. Direct light is projected from the lower right of the upper left.

(3) The square can see the difference between the light and dark surface, there are shadows on the ground.

package{
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Flash.geom.Vector3D;

Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import Away3d.entities.Mesh;
Import Away3d.lights.DirectionalLight;
Import Away3d.lights.PointLight;
Import away3d.materials.ColorMaterial;
Import away3d.materials.TextureMaterial;
Import Away3d.materials.lightpickers.StaticLightPicker;
Import Away3d.materials.methods.FilteredShadowMapMethod;
Import Away3d.primitives.CubeGeometry;
Import Away3d.primitives.PlaneGeometry;
Import Away3d.utils.Cast;

[SWF (Framerate=, backgroundcolor= "#FFFFFF")]
public class S2 extends Sprite {

private Var _view3d:view3d;
Private var cameracontroller:hovercontroller;//360 panoramic display camera controller

[Embed (source= "assets/floor_diffuse.jpg")]
public static Var Floordiffuse:class;

[Embed (source= "assets/cubetexture3.jpg")]
private Var Cube2textureclass:class;

Material
private Var planematerial:texturematerial;
private Var cube1material:colormaterial;
private Var cube2material:texturematerial;

Lighting objects
private Var light0:pointlight; Point Light source
private Var light1:directionallight; Parallel projection light source
private Var Lightpicker:staticlightpicker; Lighting containers

private Var Lastpanangle:number;
private Var Lasttiltangle:number;
private Var Lastmousex:number;
private Var Lastmousey:number;
private Var Move:boolean;

Public Function S2 () {
Initengine ();
Initlights ();
Initmaterials ();
Initobjects ();
Initlisteners ();
}

/**
* Initialize engine
*/
Private Function Initengine (): void
{
Stage.scalemode = Stagescalemode.no_scale;
Stage.align = Stagealign.top_left;

Create a viewport
_view3d = new View3D ();
_view3d.antialias = 4; Set anti-aliasing level

Initializing the camera
Cameracontroller = new Hovercontroller (_view3d.camera);
/*cameracontroller.distance = 1000;
Cameracontroller.mintiltangle = 0;
Cameracontroller.maxtiltangle = 90;
Cameracontroller.panangle = 45;*/
Cameracontroller.tiltangle = 30;

AddChild (_VIEW3D);
}

/**
* Initialize Lighting
*/
Private Function initlights (): void
{
Point Light source
Light0 = new Pointlight ();
Light0.ambient = 0.4;

Parallel projection light source (projection on top left to right)
light1 = new DirectionalLight ();
Light1.direction = new Vector3D (-1,-1, 0);
Light1.ambient = 0.3;
Light1.diffuse = 0.8;
Light1.castsshadows = true;
_view3d.scene.addchild (LIGHT1);

Add to light container
Lightpicker = new Staticlightpicker ([light0,light1]);
}

/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
Ground material
planematerial = new Texturematerial (Cast.bitmaptexture (floordiffuse));
Planematerial.repeat = true;
Planematerial.lightpicker = Lightpicker;
Add object shadow to ground
Planematerial.shadowmethod = new Filteredshadowmapmethod (LIGHT1);

Block 1 Material
cube1material = new Colormaterial (0XFFFFFF);
Cube1material.lightpicker = Lightpicker;

Block 2 material
cube2material = new Texturematerial (Cast.bitmaptexture (Cube2textureclass));
Cube2material.lightpicker = Lightpicker;
}

/**
* Initialize Object
*/
Private Function initobjects (): void
{
Ground
var Plane:mesh = new Mesh (new Planegeometry (900, 900), planematerial);
Plane.geometry.scaleUV (3, 3);
(Plane.geometry as Planegeometry). Doublesided = true; Two-sided map
_view3d.scene.addchild (plane);

Solid Color Square
var Cube1:mesh = new Mesh (new Cubegeometry (140, 140, 140), cube1material);
Cube1.position = new Vector3D (0,70,0);
_view3d.scene.addchild (CUBE1);

Post Map box
var Cube2:mesh = new Mesh (new Cubegeometry (140, 140, 140, 1, 1, 1, false), cube2material);
Cube2.position = new Vector3D (0,70,200);
_view3d.scene.addchild (CUBE2);
}

/**
* Initialization Monitoring
*/
Private Function Initlisteners (): void
{
AddEventListener (Event.enter_frame, _onenterframe);
Mouse Event Monitoring
Stage.addeventlistener (Mouseevent.mouse_down, OnMouseDown);
Stage.addeventlistener (mouseevent.mouse_up, onMouseUp);
Stage.addeventlistener (Mouseevent.mouse_wheel,onwheel);
Stage.addeventlistener (Event.resize, onResize);
OnResize ();
}

/**
* Render View
*/
Private Function _onenterframe (e:event): void
{

Moving perspective
if (move) {
Cameracontroller.panangle = 0.3 * (stage.mousex-lastmousex) + lastpanangle;
Cameracontroller.tiltangle = 0.3 * (Stage.mousey-lastmousey) + lasttiltangle;
}
Render View
_view3d.render ();
}

/**
* Use stage size always full screen
*/
Private Function onResize (event:event = null): void
{
_view3d.width = Stage.stagewidth;
_view3d.height = Stage.stageheight;
}

/**
* Mouse Wheel Event
*/
Private Function Onwheel (e:mouseevent): void
{
if (E.delta > 0) {
if (Cameracontroller.distance < 1000)
Cameracontroller.distance + 100;
}else{
if (Cameracontroller.distance > 600)
Cameracontroller.distance-= 100;
}
}

/**
* Mouse Down Event
*/
Private Function OnMouseDown (event:mouseevent): void
{
Lastpanangle = Cameracontroller.panangle;
Lasttiltangle = Cameracontroller.tiltangle;
Lastmousex = Stage.mousex;
Lastmousey = Stage.mousey;
move = true;
}

/**
* Mouse Bounce Event
*/
Private Function OnMouseUp (event:mouseevent): void
{
move = false;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.