Sometimes you need to add mouse interaction to the mesh components, such as the mouse moved to display some information, the mouse moved out of the information disappeared. When the mouse moves into the move out, if you can change the style of the mesh at the same time will play a better display effect.
1. Show border when mouse moves
The realization way is listens to the mouse to move to move out the event, sets the Mesh the Showbounds property dynamically. The effect chart is as follows:
package{
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import Away3d.entities.Mesh;
Import Away3d.events.MouseEvent3D;
Import away3d.materials.TextureMaterial;
Import Away3d.primitives.CubeGeometry;
Import Away3d.utils.Cast;
[SWF (Framerate=, backgroundcolor= "#FFFFFF")]
public class S4 extends Sprite {
private Var _view3d:view3d;
Private var cameracontroller:hovercontroller;//360 panoramic display camera controller
[Embed (source= "assets/cubetexture3.jpg")]
private Var Cubetextureclass:class;
private Var cubetexturematerial:texturematerial;
private var _lastx:number = 0;
private var _lasty:number = 0;
Public Function S4 () {
Initengine ();
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.tiltangle = 15;
AddChild (_VIEW3D);
}
/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
cubetexturematerial = new Texturematerial (Cast.bitmaptexture (Cubetextureclass));
}
/**
* Initialize Object
*/
Private Function initobjects (): void
{
Create a square in a three-dimensional stage
var Cube1:mesh = new Mesh (new Cubegeometry (1, 1, 1, false),
Cubetexturematerial);
_view3d.scene.addchild (CUBE1);
Turn on Mouse event support
Cube1.mouseenabled = true;
Add listening
Cube1.addeventlistener (Mouseevent3d.mouse_over, cubemouseover);
Cube1.addeventlistener (Mouseevent3d.mouse_out, cubemouseout);
}
Mouse move into
Private Function Cubemouseover (Event:mouseevent3d): void{
var Mesh:mesh = event.object as mesh;
Mesh.showbounds = true;
}
Mouse move out
Private Function Cubemouseout (Event:mouseevent3d): void{
var Mesh:mesh = event.object as mesh;
Mesh.showbounds = false;
}
/**
* 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
{
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
{
_view3d.stage.addeventlistener (Mouseevent.mouse_move, Mousemovehandler);
_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
/**
* Mouse Bounce Event
*/
Private Function OnMouseUp (event:mouseevent): void
{
_view3d.stage.removeeventlistener (Mouseevent.mouse_move, Mousemovehandler);
}
/**
* Mouse Movement Events
*/
Private Function Mousemovehandler (event:mouseevent): void
{
Mobile camera
var dx:number = _view3d.mousex-_lastx;
var dy:number = _view3d.mousey-_lasty;
Cameracontroller.panangle = + dx;
Cameracontroller.tiltangle + dy;
_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
}
}
2, change the brightness when the mouse moves
It also listens for the mouse to move in and out of events. Except for the original texture map, we have to prepare a highlighted map (adjust the original texture to the brightness). Dynamically change mesh.material when mouse moves in and out
package{
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import Away3d.entities.Mesh;
Import Away3d.events.MouseEvent3D;
Import away3d.materials.TextureMaterial;
Import Away3d.primitives.CubeGeometry;
Import Away3d.utils.Cast;
[SWF (Framerate=, backgroundcolor= "#FFFFFF")]
public class S5 extends Sprite {
private Var _view3d:view3d;
Private var cameracontroller:hovercontroller;//360 panoramic display camera controller
Normal map
[Embed (source= "assets/cubetexture3.jpg")]
private Var Cubetextureclass:class;
Highlight Map
[Embed (source= "assets/cubetexture3_light.jpg")]
private Var Cubetexturelightclass:class;
private Var cubetexturematerial:texturematerial;
private Var cubetexturelightmaterial:texturematerial;
private var _lastx:number = 0;
private var _lasty:number = 0;
Public Function S5 () {
Initengine ();
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.tiltangle = 15;
AddChild (_VIEW3D);
}
/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
cubetexturematerial = new Texturematerial (Cast.bitmaptexture (Cubetextureclass));
Highlight Skin
cubetexturelightmaterial = new Texturematerial (Cast.bitmaptexture (Cubetexturelightclass));
}
/**
* Initialize Object
*/
Private Function initobjects (): void
{
Create a square in a three-dimensional stage
var Cube1:mesh = new Mesh (new Cubegeometry (1, 1, 1, false),
Cubetexturematerial);
_view3d.scene.addchild (CUBE1);
Turn on Mouse event support
Cube1.mouseenabled = true;
Add listening
Cube1.addeventlistener (Mouseevent3d.mouse_over, cubemouseover);
Cube1.addeventlistener (Mouseevent3d.mouse_out, cubemouseout);
}
Mouse move into
Private Function Cubemouseover (Event:mouseevent3d): void{
var Mesh:mesh = event.object as mesh;
Mesh.material = cubetexturelightmaterial;
}
Mouse move out
Private Function Cubemouseout (Event:mouseevent3d): void{
var Mesh:mesh = event.object as mesh;
Mesh.material = cubetexturematerial;
}
/**
* 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
{
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
{
_view3d.stage.addeventlistener (Mouseevent.mouse_move, Mousemovehandler);
_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
/**
* Mouse Bounce Event
*/
Private Function OnMouseUp (event:mouseevent): void
{
_view3d.stage.removeeventlistener (Mouseevent.mouse_move, Mousemovehandler);
}
/**
* Mouse Movement Events
*/
Private Function Mousemovehandler (event:mouseevent): void
{
Mobile camera
var dx:number = _view3d.mousex-_lastx;
var dy:number = _view3d.mousey-_lasty;
Cameracontroller.panangle = + dx;
Cameracontroller.tiltangle + dy;
_LASTX = _view3d.mousex;
_lasty = _view3d.mousey;
}
}
}