import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { BlinnPhongMaterial } from "laya/d3/core/material/BlinnPhongMaterial" ; import { Laya3D } from "Laya3D" ; import { Handler } from "laya/utils/Handler" ; import { Stage } from "laya/display/Stage" ; import { Laya } from "Laya" ; import { Stat } from "laya/utils/Stat" ; import { Camera } from "laya/d3/core/Camera" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; import { DirectionLight } from "laya/d3/core/light/DirectionLight" ; import { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Texture2D } from "laya/resource/Texture2D" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { CannonPhysicsCollider } from "laya/d3/physicsCannon/CannonPhysicsCollider" ; import { CannonBoxColliderShape } from "laya/d3/physicsCannon/shape/CannonBoxColliderShape" ; import { Transform3D } from "laya/d3/core/Transform3D" ; import { CannonRigidbody3D } from "laya/d3/physicsCannon/CannonRigidbody3D" ; import { CannonSphereColliderShape } from "laya/d3/physicsCannon/shape/CannonSphereColliderShape" ; import { CannonCompoundColliderShape } from "laya/d3/physicsCannon/shape/CannonCompoundColliderShape" ; import { Event } from "laya/events/Event" ; import { Script3D } from "laya/d3/component/Script3D" ; import { PhysicsComponent } from "laya/d3/physics/PhysicsComponent" ; import { Collision } from "laya/d3/physics/Collision" ; import { Config3D } from "Config3D" ; /**
 * 用键盘WASD来控制小盒子的移动，碰到的会变成红色
 */ export class CannonPhysicsWorld_ColliderEvent { private scene : Scene3D ; private mat1 : BlinnPhongMaterial ; private mat2 : BlinnPhongMaterial ; private mat3 : BlinnPhongMaterial ; private camera : Camera ; private Kinematic : MeshSprite3D ; private speed : number = 0.1 ; private tempSpeed : Vector3 = new Vector3 ( ) ; constructor ( ) { Laya3D . init ( 0 , 0 , null , Handler . create ( null , ( ) => { Config3D . useCannonPhysics = true ; 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 , 6 , 15 ) ) ; this . camera . transform . rotate ( new Vector3 ( - 15 , 0 , 0 ) , true , false ) ; this . camera . addComponent ( CameraMoveScript ) ; this . camera . clearColor = null ; var directionLight = ( < DirectionLight > this . scene . addChild ( new DirectionLight ( ) ) ) ; directionLight . color = new Vector3 ( 0.6 , 0.6 , 0.6 ) ; 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/grass.png" , Handler . create ( this , function ( tex ) : void { planeMat . albedoTexture = tex ; } ) ) ; var tilingOffset = planeMat . tilingOffset ; tilingOffset . setValue ( 5 , 5 , 0 , 0 ) ; planeMat . tilingOffset = tilingOffset ; plane . meshRenderer . material = planeMat ; var planeCollider = plane . addComponent ( CannonPhysicsCollider ) ; var planeShape = new CannonBoxColliderShape ( 20 , 0.01 , 20 ) ; planeCollider . colliderShape = planeShape ; planeCollider . friction = 2 ; planeCollider . restitution = 0.3 ; this . mat1 = new BlinnPhongMaterial ( ) ; this . mat2 = new BlinnPhongMaterial ( ) ; this . mat3 = new BlinnPhongMaterial ( ) ; Texture2D . load ( "res/threeDimen/Physics/rocks.jpg" , Handler . create ( this , function ( tex ) : void { this . mat1 . albedoTexture = tex ; } ) ) ; Texture2D . load ( "res/threeDimen/Physics/plywood.jpg" , Handler . create ( this , function ( tex ) : void { this . mat2 . albedoTexture = tex ; } ) ) ; Texture2D . load ( "res/threeDimen/Physics/wood.jpg" , Handler . create ( this , function ( tex ) : void { this . mat3 . albedoTexture = tex ; } ) ) ; this . addBox ( false ) ; this . addSphere ( ) ; this . addCompoundColliderShape ( ) ; this . Kinematic = this . addBox ( true ) ; this . Kinematic . transform . position = new Vector3 ( 0 , 0.5 , 5 ) ; this . Kinematic . addComponent ( colliderCheck ) ; Laya . stage . on ( Event . KEY_DOWN , this , this . keyDown ) ; Laya . stage . on ( Event . KEY_PRESS , this , this . keyDown ) ; } ) ) ; } addBox ( isKinematic ) { var sX = 1 ; var sY = 1 ; var sZ = 1 ; var box = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( sX , sY , sZ ) ) ) ) ; box . name = "box" ; box . meshRenderer . material = this . mat1 ; var transform = box . transform ; var pos = transform . position ; pos . setValue ( - 5 , 5 , 0 ) ; transform . position = pos ; var rigidBody = box . addComponent ( CannonRigidbody3D ) ; var boxShape = new CannonBoxColliderShape ( sX , sY , sZ ) ; rigidBody . colliderShape = boxShape ; rigidBody . mass = 10 ; rigidBody . isKinematic = isKinematic ; return box ; } addSphere ( ) { var radius = 1 ; var sphere = < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createSphere ( 1 ) ) ) ; sphere . name = "sphere" ; sphere . meshRenderer . material = this . mat2 ; var sphereTransform = sphere . transform ; var pos = sphereTransform . position ; pos . setValue ( 0 , 1 , 0 ) ; sphereTransform . position = pos ; var physicsCollider = sphere . addComponent ( CannonPhysicsCollider ) ; var sphereShape = new CannonSphereColliderShape ( radius ) ; physicsCollider . colliderShape = sphereShape ; physicsCollider . isTrigger = true ; } addCompoundColliderShape ( ) { var mesh = this . addMeshBox ( 5 , 5 , 0 ) ; mesh . name = "compound" ; var scale = mesh . transform . getWorldLossyScale ( ) ; scale . setValue ( 0.5 , 0.5 , 0.5 ) ; mesh . transform . setWorldLossyScale ( scale ) ; this . scene . addChild ( mesh ) ; var rigidBody = mesh . addComponent ( CannonRigidbody3D ) ; var boxShape0 = new CannonBoxColliderShape ( 1 , 1 , 1 ) ; var boxShape1 = new CannonBoxColliderShape ( 1 , 1 , 1 ) ; var boxShape2 = new CannonBoxColliderShape ( 1 , 1 , 1 ) ; var boxShape3 = new CannonBoxColliderShape ( 1 , 1 , 1 ) ; var boxCompoundShape = new CannonCompoundColliderShape ( ) ; ( < any > boxCompoundShape ) . addChildShape ( boxShape0 , new Vector3 ( 0.5 , 0.5 , 0 ) ) ; ( < any > boxCompoundShape ) . addChildShape ( boxShape1 , new Vector3 ( 0.5 , - 0.5 , 0 ) ) ; ( < any > boxCompoundShape ) . addChildShape ( boxShape2 , new Vector3 ( - 0.5 , 0.5 , 0 ) ) ; ( < any > boxCompoundShape ) . addChildShape ( boxShape3 , new Vector3 ( - 0.5 , - 0.5 ) ) ; rigidBody . colliderShape = boxCompoundShape ; rigidBody . mass = 10 ; } addMeshBox ( x , y , z ) { var sX = 2 ; var sY = 2 ; var sZ = 1 ; var box = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( sX , sY , sZ ) ) ) ) ; box . meshRenderer . material = this . mat3 ; var transform = box . transform ; var pos = transform . position ; pos . setValue ( x , y , z ) ; transform . position = pos ; return box ; } keyDown ( e ) { switch ( e . keyCode ) { case 87 : this . tempSpeed . setValue ( 0 , 0 , - this . speed ) ; break ; case 83 : this . tempSpeed . setValue ( 0 , 0 , this . speed ) ; break ; case 65 : this . tempSpeed . setValue ( - this . speed , 0 , 0 ) ; break ; case 68 : this . tempSpeed . setValue ( this . speed , 0 , 0 ) ; break ; } this . Kinematic . transform . translate ( this . tempSpeed ) ; } } export class colliderCheck extends Script3D { /**
     * 开始触发时执行
     * 此方法为虚方法，使用时重写覆盖即可
     */ onTriggerEnter ( other ) { console . log ( "triggerEnter" ) ; } /**
     * 持续触发时执行
     * 此方法为虚方法，使用时重写覆盖即可
     */ onTriggerStay ( other ) { console . log ( "triggerStay" ) ; } /**
     * 结束触发时执行
     * 此方法为虚方法，使用时重写覆盖即可
     */ onTriggerExit ( other ) { console . log ( "triggerExit" ) ; } /**
     * 开始碰撞时执行
     * 此方法为虚方法，使用时重写覆盖即可
     */ onCollisionEnter ( collision ) { console . log ( "collisionEnter" ) ; } /**
     * 持续碰撞时执行
     * 此方法为虚方法，使用时重写覆盖即可
     */ onCollisionStay ( collision ) { console . log ( "collisionStay" ) ; } /**
     * 结束碰撞时执行
     * 此方法为虚方法，使用时重写覆盖即可
     */ onCollisionExit ( collision ) { console . log ( "collisionexit" ) ; } }