import { Laya } from "Laya" ; import { Script3D } from "laya/d3/component/Script3D" ; import { Camera } from "laya/d3/core/Camera" ; import { DirectionLight } from "laya/d3/core/light/DirectionLight" ; import { BlinnPhongMaterial } from "laya/d3/core/material/BlinnPhongMaterial" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Sprite3D } from "laya/d3/core/Sprite3D" ; import { Quaternion } from "laya/d3/math/Quaternion" ; import { Ray } from "laya/d3/math/Ray" ; import { Vector2 } from "laya/d3/math/Vector2" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { Collision } from "laya/d3/physics/Collision" ; import { HitResult } from "laya/d3/physics/HitResult" ; import { PhysicsCollider } from "laya/d3/physics/PhysicsCollider" ; import { MeshColliderShape } from "laya/d3/physics/shape/MeshColliderShape" ; import { Mesh } from "laya/d3/resource/models/Mesh" ; import { Stage } from "laya/display/Stage" ; import { Text } from "laya/display/Text" ; import { MouseManager } from "laya/events/MouseManager" ; import { Loader } from "laya/net/Loader" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { Laya3D } from "Laya3D" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; author ...
  export class MouseInteraction { private _scene : Scene3D ; private _camera : Camera ; private _ray : Ray ; private _outHitResult : HitResult = new HitResult ( ) ; private posX : number = 0.0 ; private posY : number = 0.0 ; private point : Vector2 = new Vector2 ( ) ; private text : Text = new Text ( ) ; private tmpVector : Vector3 = new Vector3 ( 0 , 0 , 0 ) ; constructor ( ) { Laya3D . init ( 0 , 0 ) ; Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; this . _scene = ( < Scene3D > Laya . stage . addChild ( new Scene3D ( ) ) ) ; this . _camera = ( < Camera > ( this . _scene . addChild ( new Camera ( 0 , 0.1 , 100 ) ) ) ) ; this . _camera . transform . translate ( new Vector3 ( 0 , 0.7 , 5 ) ) ; this . _camera . transform . rotate ( new Vector3 ( - 15 , 0 , 0 ) , true , false ) ; this . _camera . addComponent ( CameraMoveScript ) ; var directionLight = ( < DirectionLight > this . _scene . addChild ( new DirectionLight ( ) ) ) ; directionLight . color = new Vector3 ( 1 , 1 , 1 ) ; directionLight . transform . rotate ( new Vector3 ( - 3.14 / 3 , 0 , 0 ) ) ; Laya . loader . create ( [ "res/threeDimen/staticModel/grid/plane.lh" , "res/threeDimen/skinModel/LayaMonkey/LayaMonkey.lh" ] , Handler . create ( this , this . onComplete ) ) ; } private onComplete ( ) { var grid = ( < Sprite3D > this . _scene . addChild ( Loader . getRes ( "res/threeDimen/staticModel/grid/plane.lh" ) ) ) ; grid . layer = 10 ; var staticLayaMonkey = ( < MeshSprite3D > this . _scene . addChild ( new MeshSprite3D ( Loader . getRes ( "res/threeDimen/skinModel/LayaMonkey/Assets/LayaMonkey/LayaMonkey-LayaMonkey.lm" ) ) ) ) ; staticLayaMonkey . meshRenderer . material = Loader . getRes ( "res/threeDimen/skinModel/LayaMonkey/Assets/LayaMonkey/Materials/T_Diffuse.lmat" ) ; staticLayaMonkey . transform . position = new Vector3 ( 0 , 0 , 0.5 ) ; staticLayaMonkey . transform . localScale = new Vector3 ( 0.3 , 0.3 , 0.3 ) ; staticLayaMonkey . transform . rotation = new Quaternion ( 0.7071068 , 0 , 0 , - 0.7071067 ) ; this . tmpVector . setValue ( 0.0 , 0 , 0.5 ) ; var layaMonkey_clone1 = ( < MeshSprite3D > Sprite3D . instantiate ( staticLayaMonkey , this . _scene , false , this . tmpVector ) ) ; var layaMonkey_clone2 = ( < MeshSprite3D > Sprite3D . instantiate ( staticLayaMonkey , this . _scene , false , this . tmpVector ) ) ; var layaMonkey_clone3 = ( < MeshSprite3D > Sprite3D . instantiate ( staticLayaMonkey , this . _scene , false , this . tmpVector ) ) ; staticLayaMonkey . name = "大熊" ; layaMonkey_clone1 . name = "二熊" ; layaMonkey_clone2 . name = "三熊" ; layaMonkey_clone3 . name = "小小熊" ; this . tmpVector . setValue ( 1.5 , 0 , 0.0 ) ; layaMonkey_clone1 . transform . translate ( this . tmpVector ) ; this . tmpVector . setValue ( - 1.5 , 0 , 0.0 ) ; layaMonkey_clone2 . transform . translate ( this . tmpVector ) ; this . tmpVector . setValue ( 2.5 , 0 , 0.0 ) ; layaMonkey_clone3 . transform . translate ( this . tmpVector ) ; this . tmpVector . setValue ( 0 , 60 , 0 ) ; layaMonkey_clone2 . transform . rotate ( this . tmpVector , false , false ) ; this . tmpVector . setValue ( 0.1 , 0.1 , 0.1 ) ; var scale = new Vector3 ( 0.1 , 0.1 , 0.1 ) ; layaMonkey_clone3 . transform . localScale = this . tmpVector ; var meshCollider = staticLayaMonkey . addComponent ( PhysicsCollider ) ; var meshShape = new MeshColliderShape ( ) ; meshShape . mesh = ( < Mesh > staticLayaMonkey . meshFilter . sharedMesh ) ; meshCollider . colliderShape = meshShape ; var meshCollider1 = layaMonkey_clone1 . addComponent ( PhysicsCollider ) ; var meshShape1 = new MeshColliderShape ( ) ; meshShape1 . mesh = ( < Mesh > layaMonkey_clone1 . meshFilter . sharedMesh ) ; meshCollider1 . colliderShape = meshShape1 ; var meshCollider2 = layaMonkey_clone2 . addComponent ( PhysicsCollider ) ; var meshShape2 = new MeshColliderShape ( ) ; meshShape2 . mesh = ( < Mesh > layaMonkey_clone2 . meshFilter . sharedMesh ) ; meshCollider2 . colliderShape = meshShape2 ; var meshCollider3 = layaMonkey_clone3 . addComponent ( PhysicsCollider ) ; var meshShape3 = new MeshColliderShape ( ) ; meshShape3 . mesh = ( < Mesh > layaMonkey_clone3 . meshFilter . sharedMesh ) ; meshCollider3 . colliderShape = meshShape3 ; this . text . x = Laya . stage . width / 2 - 50 ; this . text . y = 50 ; this . _ray = new Ray ( new Vector3 ( 0 , 0 , 0 ) , new Vector3 ( 0 , 0 , 0 ) ) ; this . addMouseEvent ( ) ; this . text . name = "text" ; this . text . overflow = Text . HIDDEN ; this . text . color = "#FFFFFF" ; this . text . font = "Impact" ; this . text . fontSize = 20 ; this . text . x = Laya . stage . width / 2 ; Laya . stage . addChild ( this . text ) ; staticLayaMonkey . addComponent ( SceneScript ) ; layaMonkey_clone1 . addComponent ( SceneScript ) ; layaMonkey_clone2 . addComponent ( SceneScript ) ; layaMonkey_clone3 . addComponent ( SceneScript ) ; } private addMouseEvent ( ) { } private onMouseDown ( ) { this . posX = this . point . x = MouseManager . instance . mouseX ; this . posY = this . point . y = MouseManager . instance . mouseY ; this . _camera . viewportPointToRay ( this . point , this . _ray ) ; this . _scene . physicsSimulation . rayCast ( this . _ray , this . _outHitResult ) ; if ( this . _outHitResult . succeeded ) { this . text . text = "碰撞到了" + this . _outHitResult . collider . owner . name ; console . log ( "碰撞到物体！！" ) ; } } } class SceneScript extends Script3D { private meshSprite : MeshSprite3D ; private text : Text ; private _albedoColor : Vector4 = new Vector4 ( 0.0 , 0.0 , 0.0 , 1.0 ) ; constructor ( ) { super ( ) ; } /**
     * 覆写3D对象组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
     */ onAwake ( ) { this . meshSprite = ( < MeshSprite3D > this . owner ) ; this . text = ( < Text > Laya . stage . getChildByName ( "text" ) ) ; } /**
     * 覆写组件更新方法（相当于帧循环）
     */ onUpdate ( ) { } onMouseDown ( ) { this . text . text = "碰撞到了" + this . owner . name ; } onCollisionEnter ( collision ) { ( ( < BlinnPhongMaterial > this . meshSprite . meshRenderer . sharedMaterial ) ) . albedoColor = this . _albedoColor ; } }