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 { PixelLineSprite3D } from "laya/d3/core/pixelLine/PixelLineSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Sprite3D } from "laya/d3/core/Sprite3D" ; import { Transform3D } from "laya/d3/core/Transform3D" ; import { Color } from "laya/d3/math/Color" ; import { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; import { Ray } from "laya/d3/math/Ray" ; 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 { CapsuleColliderShape } from "laya/d3/physics/shape/CapsuleColliderShape" ; import { SphereColliderShape } from "laya/d3/physics/shape/SphereColliderShape" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { Stage } from "laya/display/Stage" ; import { Event } from "laya/events/Event" ; import { Texture2D } from "laya/resource/Texture2D" ; import { Button } from "laya/ui/Button" ; import { Browser } from "laya/utils/Browser" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { Laya3D } from "Laya3D" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; import { Config3D } from "Config3D" ; import Client from "../../Client" ; import { Utils } from "laya/utils/Utils" ; export class PhysicsWorld_RayShapeCast { private castType : number = 0 ; private castAll : boolean = false ; private ray : Ray = new Ray ( new Vector3 ( 0 , 0 , 0 ) , new Vector3 ( 0 , 0 , 0 ) ) ; private scene : Scene3D ; private hitResult : HitResult = new HitResult ( ) ; private hitResults : HitResult [ ] = [ ] ; private debugSprites : Sprite3D [ ] = [ ] ; private from : Vector3 = new Vector3 ( 0 , 1 , 10 ) ; private to : Vector3 = new Vector3 ( 0 , 1 , - 5 ) ; private _albedoColor : Vector4 = new Vector4 ( 1.0 , 1.0 , 1.0 , 0.5 ) ; private _position : Vector3 = new Vector3 ( 0 , 0 , 0 ) ; private tex : Texture2D ; private tex1 : Texture2D ; private changeActionButton0 : Button ; private changeActionButton1 : Button ; private changeActionButton2 : Button ; /**实例类型*/ private btype : any = "PhysicsWorld_RayShapeCast" ; /**场景内按钮类型*/ private stype : any = 0 ; constructor ( ) { Laya3D . init ( 0 , 0 , null , Handler . create ( null , ( ) => { Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; this . scene = ( < Scene3D > Laya . stage . addChild ( new Scene3D ( ) ) ) ; Config3D . useCannonPhysics = false ; var camera = ( < Camera > this . scene . addChild ( new Camera ( 0 , 0.1 , 100 ) ) ) ; camera . transform . translate ( new Vector3 ( 0 , 8 , 20 ) ) ; camera . transform . rotate ( new Vector3 ( - 30 , 0 , 0 ) , true , false ) ; camera . addComponent ( CameraMoveScript ) ; var directionLight = ( < DirectionLight > this . scene . addChild ( new DirectionLight ( ) ) ) ; directionLight . color = new Vector3 ( 1 , 1 , 1 ) ; var mat = directionLight . transform . worldMatrix ; mat . setForward ( new Vector3 ( - 1.0 , - 1.0 , 1.0 ) ) ; directionLight . transform . worldMatrix = mat ; var plane = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createPlane ( 20 , 20 , 10 , 10 ) ) ) ) ; var planeMat = new BlinnPhongMaterial ( ) ; Texture2D . load ( "res/threeDimen/Physics/wood.jpg" , Handler . create ( this , function ( tex ) : void { planeMat . albedoTexture = tex ; } ) ) ; var tilingOffset = planeMat . tilingOffset ; tilingOffset . setValue ( 2 , 2 , 0 , 0 ) ; planeMat . tilingOffset = tilingOffset ; plane . meshRenderer . material = planeMat ; Texture2D . load ( "res/threeDimen/Physics/rocks.jpg" , Handler . create ( this , function ( tex ) : void { this . tex = tex ; } ) ) ; Texture2D . load ( "res/threeDimen/Physics/wood.jpg" , Handler . create ( this , function ( tex ) : void { this . tex1 = tex ; } ) ) ; var planeBody = ( < PhysicsCollider > plane . addComponent ( PhysicsCollider ) ) ; var boxCollider = new BoxColliderShape ( 20 , 0 , 20 ) ; planeBody . colliderShape = boxCollider ; for ( var i = 0 ; i < 60 ; i ++ ) { this . addBox ( ) ; this . addCapsule ( ) ; } } ) ) ; this . loadUI ( ) ; } private loadUI ( ) { Laya . loader . load ( [ "res/threeDimen/ui/button.png" ] , Handler . create ( this , function ( ) : void { this . changeActionButton0 = ( < Button > Laya . stage . addChild ( new Button ( "res/threeDimen/ui/button.png" , "射线模式" ) ) ) ; this . changeActionButton0 . size ( 160 , 40 ) ; this . changeActionButton0 . labelBold = true ; this . changeActionButton0 . labelSize = 30 ; this . changeActionButton0 . sizeGrid = "4,4,4,4" ; this . changeActionButton0 . scale ( Browser . pixelRatio , Browser . pixelRatio ) ; this . changeActionButton0 . pos ( 200 , 200 ) ; this . changeActionButton0 . on ( Event . CLICK , this , this . stypeFun0 ) ; this . changeActionButton1 = ( < Button > Laya . stage . addChild ( new Button ( "res/threeDimen/ui/button.png" , "不穿透" ) ) ) ; this . changeActionButton1 . size ( 160 , 40 ) ; this . changeActionButton1 . labelBold = true ; this . changeActionButton1 . labelSize = 30 ; this . changeActionButton1 . sizeGrid = "4,4,4,4" ; this . changeActionButton1 . scale ( Browser . pixelRatio , Browser . pixelRatio ) ; this . changeActionButton1 . pos ( 200 , 300 ) ; this . changeActionButton1 . on ( Event . CLICK , this , this . stypeFun1 ) ; this . changeActionButton2 = ( < Button > Laya . stage . addChild ( new Button ( "res/threeDimen/ui/button.png" , "检测" ) ) ) ; this . changeActionButton2 . size ( 160 , 40 ) ; this . changeActionButton2 . labelBold = true ; this . changeActionButton2 . labelSize = 30 ; this . changeActionButton2 . sizeGrid = "4,4,4,4" ; this . changeActionButton2 . scale ( Browser . pixelRatio , Browser . pixelRatio ) ; this . changeActionButton2 . pos ( 200 , 400 ) ; this . changeActionButton2 . on ( Event . CLICK , this , this . stypeFun2 ) ; } ) ) ; } stypeFun0 ( label = "射线模式" ) { this . castType ++ ; this . castType %= 4 ; switch ( this . castType ) { case 0 : this . changeActionButton0 . label = "射线模式" ; break ; case 1 : this . changeActionButton0 . label = "盒子模式" ; break ; case 2 : this . changeActionButton0 . label = "球模式" ; break ; case 3 : this . changeActionButton0 . label = "胶囊模式" ; break ; } label = this . changeActionButton0 . label ; Client . instance . send ( { type : "next" , btype : this . btype , stype : 0 , value : label } ) ; } stypeFun1 ( label = "不穿透" ) { if ( this . castAll ) { this . changeActionButton1 . label = "不穿透" ; this . castAll = false ; } else { this . changeActionButton1 . label = "穿透" ; this . castAll = true ; } label = this . changeActionButton1 . label ; Client . instance . send ( { type : "next" , btype : this . btype , stype : 1 , value : label } ) ; } stypeFun2 ( castType = 0 ) { if ( this . hitResult . succeeded ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResult . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ; if ( this . hitResults . length > 0 ) { for ( var i = 0 , n = this . hitResults . length ; i < n ; i ++ ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResults [ i ] . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ; this . hitResults . length = 0 ; } if ( this . debugSprites . length > 0 ) { for ( i = 0 , n = this . debugSprites . length ; i < n ; i ++ ) this . debugSprites [ i ] . destroy ( ) ; this . debugSprites . length = 0 ; } switch ( this . castType ) { case 0 : var lineSprite = ( < PixelLineSprite3D > this . scene . addChild ( new PixelLineSprite3D ( 1 ) ) ) ; lineSprite . addLine ( this . from , this . to , Color . RED , Color . RED ) ; this . debugSprites . push ( lineSprite ) ; if ( this . castAll ) { this . scene . physicsSimulation . raycastAllFromTo ( this . from , this . to , this . hitResults ) ; for ( i = 0 , n = this . hitResults . length ; i < n ; i ++ ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResults [ i ] . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } else { this . scene . physicsSimulation . raycastFromTo ( this . from , this . to , this . hitResult ) ; ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResult . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } break ; case 1 : var boxCollider = new BoxColliderShape ( 1.0 , 1.0 , 1.0 ) ; for ( i = 0 ; i < 21 ; i ++ ) { var boxSprite = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( 1.0 , 1.0 , 1.0 ) ) ) ) ; var mat = new BlinnPhongMaterial ( ) ; mat . albedoColor = this . _albedoColor ; mat . renderMode = BlinnPhongMaterial . RENDERMODE_TRANSPARENT ; boxSprite . meshRenderer . material = mat ; Vector3 . lerp ( this . from , this . to , i / 20 , this . _position ) ; boxSprite . transform . localPosition = this . _position ; this . debugSprites . push ( boxSprite ) ; } if ( this . castAll ) { this . scene . physicsSimulation . shapeCastAll ( boxCollider , this . from , this . to , this . hitResults ) ; for ( i = 0 , n = this . hitResults . length ; i < n ; i ++ ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResults [ i ] . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } else { if ( this . scene . physicsSimulation . shapeCast ( boxCollider , this . from , this . to , this . hitResult ) ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResult . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } break ; case 2 : var sphereCollider = new SphereColliderShape ( 0.5 ) ; for ( i = 0 ; i < 41 ; i ++ ) { var sphereSprite = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createSphere ( 0.5 ) ) ) ) ; var mat = new BlinnPhongMaterial ( ) ; mat . albedoColor = this . _albedoColor ; mat . renderMode = BlinnPhongMaterial . RENDERMODE_TRANSPARENT ; sphereSprite . meshRenderer . material = mat ; Vector3 . lerp ( this . from , this . to , i / 40 , this . _position ) ; sphereSprite . transform . localPosition = this . _position ; this . debugSprites . push ( sphereSprite ) ; } if ( this . castAll ) { this . scene . physicsSimulation . shapeCastAll ( sphereCollider , this . from , this . to , this . hitResults ) ; for ( i = 0 , n = this . hitResults . length ; i < n ; i ++ ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResults [ i ] . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } else { if ( this . scene . physicsSimulation . shapeCast ( sphereCollider , this . from , this . to , this . hitResult ) ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResult . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } break ; case 3 : var capsuleCollider = new CapsuleColliderShape ( 0.25 , 1.0 ) ; for ( i = 0 ; i < 41 ; i ++ ) { var capsuleSprite = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createCapsule ( 0.25 , 1.0 ) ) ) ) ; var mat = new BlinnPhongMaterial ( ) ; mat . albedoColor = this . _albedoColor ; mat . renderMode = BlinnPhongMaterial . RENDERMODE_TRANSPARENT ; capsuleSprite . meshRenderer . material = mat ; Vector3 . lerp ( this . from , this . to , i / 40 , this . _position ) ; capsuleSprite . transform . localPosition = this . _position ; this . debugSprites . push ( capsuleSprite ) ; } if ( this . castAll ) { this . scene . physicsSimulation . shapeCastAll ( capsuleCollider , this . from , this . to , this . hitResults ) ; for ( i = 0 , n = this . hitResults . length ; i < n ; i ++ ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResults [ i ] . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } else { if ( this . scene . physicsSimulation . shapeCast ( capsuleCollider , this . from , this . to , this . hitResult ) ) ( ( < BlinnPhongMaterial > ( ( < MeshSprite3D > this . hitResult . collider . owner ) ) . meshRenderer . sharedMaterial ) ) . albedoColor = new Vector4 ( 1.0 , 0.0 , 0.0 , 1.0 ) ; } break ; } castType = this . castType ; Client . instance . send ( { type : "next" , btype : this . btype , stype : 2 , value : castType } ) ; } addBox ( ) { 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 ; var transform = box . transform ; var pos = transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 2 , Math . random ( ) * 4 - 2 ) ; transform . position = pos ; var rotationEuler = transform . rotationEuler ; rotationEuler . setValue ( Math . random ( ) * 360 , Math . random ( ) * 360 , Math . random ( ) * 360 ) ; transform . rotationEuler = rotationEuler ; var rigidBody = box . addComponent ( Rigidbody3D ) ; var boxShape = new BoxColliderShape ( sX , sY , sZ ) ; rigidBody . colliderShape = boxShape ; rigidBody . mass = 10 ; } addCapsule ( ) { var mat = new BlinnPhongMaterial ( ) ; Texture2D . load ( "res/threeDimen/Physics/wood.jpg" , Handler . create ( this , function ( tex ) : void { mat . albedoTexture = tex ; } ) ) ; var raidius = Math . random ( ) * 0.2 + 0.2 ; var height = Math . random ( ) * 0.5 + 0.8 ; var capsule = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createCapsule ( raidius , height ) ) ) ) ; capsule . meshRenderer . material = mat ; var transform = capsule . transform ; var pos = transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 2 , Math . random ( ) * 4 - 2 ) ; transform . position = pos ; var rotationEuler = transform . rotationEuler ; rotationEuler . setValue ( Math . random ( ) * 360 , Math . random ( ) * 360 , Math . random ( ) * 360 ) ; transform . rotationEuler = rotationEuler ; var rigidBody = capsule . addComponent ( Rigidbody3D ) ; var sphereShape = new CapsuleColliderShape ( raidius , height ) ; rigidBody . colliderShape = sphereShape ; rigidBody . mass = 10 ; } }