import { Laya } from "Laya" ; 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 { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; 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 { HitResult } from "laya/d3/physics/HitResult" ; import { PhysicsCollider } from "laya/d3/physics/PhysicsCollider" ; import { Rigidbody3D } from "laya/d3/physics/Rigidbody3D" ; import { BoxColliderShape } from "laya/d3/physics/shape/BoxColliderShape" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { Stage } from "laya/display/Stage" ; import { Text } from "laya/display/Text" ; import { Event } from "laya/events/Event" ; import { MouseManager } from "laya/events/MouseManager" ; import { Texture2D } from "laya/resource/Texture2D" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { Utils } from "laya/utils/Utils" ; import { Laya3D } from "Laya3D" ; import Client from "../../Client" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; author zqx
  export class CameraRay { private scene : Scene3D ; private camera : Camera ; private _ray : Ray ; private _outHitResult : HitResult = new HitResult ( ) ; private outs : HitResult [ ] = [ ] ; private posX : number = 0.0 ; private posY : number = 0.0 ; private point : Vector2 = new Vector2 ( ) ; private _text : Text = new Text ( ) ; private _translate : Vector3 = new Vector3 ( 0 , 6 , 9.5 ) ; private _rotation : Vector3 = new Vector3 ( - 15 , 0 , 0 ) ; private _forward : Vector3 = new Vector3 ( - 1.0 , - 1.0 , - 1.0 ) ; private _tilingOffset : Vector4 = new Vector4 ( 10 , 10 , 0 , 0 ) ; private tmpVector : Vector3 = new Vector3 ( 0 , 0 , 0 ) ; private tmpVector2 : Vector3 = new Vector3 ( 0 , 0 , 0 ) ; /**实例类型*/ private btype : any = "CameraRay" ; /**场景内按钮类型*/ private stype : any = 0 ; constructor ( ) { Laya3D . init ( 0 , 0 , null , Handler . create ( this , ( ) => { 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 ( this . _translate ) ; this . camera . transform . rotate ( this . _rotation , true , false ) ; this . camera . addComponent ( CameraMoveScript ) ; this . camera . clearColor = null ; var directionLight = ( < DirectionLight > this . scene . addChild ( new DirectionLight ( ) ) ) ; directionLight . color . setValue ( 0.6 , 0.6 , 0.6 ) ; var mat = directionLight . transform . worldMatrix ; mat . setForward ( this . _forward ) ; directionLight . transform . worldMatrix = mat ; var plane = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createPlane ( 10 , 10 , 10 , 10 ) ) ) ) ; var planeMat = new BlinnPhongMaterial ( ) ; Texture2D . load ( "res/threeDimen/Physics/grass.png" , Handler . create ( this , function ( tex ) : void { planeMat . albedoTexture = tex ; } ) ) ; planeMat . tilingOffset = this . _tilingOffset ; plane . meshRenderer . material = planeMat ; var planeStaticCollider = plane . addComponent ( PhysicsCollider ) ; var planeShape = new BoxColliderShape ( 10 , 0 , 10 ) ; planeStaticCollider . colliderShape = planeShape ; planeStaticCollider . friction = 2 ; planeStaticCollider . restitution = 0.3 ; this . addMouseEvent ( ) ; this . _ray = new Ray ( new Vector3 ( 0 , 0 , 0 ) , new Vector3 ( 0 , 0 , 0 ) ) ; } ) ) ; } addBoxXYZ ( x , y , z ) { var mat1 = new BlinnPhongMaterial ( ) ; Texture2D . load ( "res/threeDimen/Physics/rocks.jpg" , Handler . create ( this , function ( tex ) : void { mat1 . albedoTexture = tex ; } ) ) ; var sX = Math . random ( ) * 0.75 + 0.25 ; var sY = Math . random ( ) * 0.75 + 0.25 ; var sZ = Math . random ( ) * 0.75 + 0.25 ; var box = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( sX , sY , sZ ) ) ) ) ; box . meshRenderer . material = mat1 ; this . tmpVector . setValue ( x , y , z ) ; box . transform . position = this . tmpVector ; this . tmpVector2 . setValue ( Math . random ( ) * 360 , Math . random ( ) * 360 , Math . random ( ) * 360 ) ; box . transform . rotationEuler = this . tmpVector2 ; var rigidBody = box . addComponent ( Rigidbody3D ) ; var boxShape = new BoxColliderShape ( sX , sY , sZ ) ; rigidBody . colliderShape = boxShape ; rigidBody . mass = 10 ; } private addMouseEvent ( ) { Laya . stage . on ( Event . MOUSE_DOWN , this , this . onMouseDown ) ; } 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 . rayCastAll ( this . _ray , this . outs ) ; if ( this . outs . length != 0 ) { for ( var i = 0 ; i < this . outs . length ; i ++ ) { this . addBoxXYZ ( this . outs [ i ] . point . x , this . outs [ i ] . point . y , this . outs [ i ] . point . z ) ; } } Client . instance . send ( { type : "next" , btype : this . btype , stype : 0 } ) ; } }