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 { 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 { Config3D } from "Config3D" ; /**
 * 示例基本碰撞
 */ export class CannonPhysicsWorld_PhysicsProperty { private scene : Scene3D ; 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 ( ) ) ) ; var camera = ( < Camera > this . scene . addChild ( new Camera ( 0 , 0.1 , 100 ) ) ) ; camera . transform . translate ( new Vector3 ( 0 , 6 , 9.5 ) ) ; camera . transform . rotate ( new Vector3 ( - 15 , 0 , 0 ) , true , false ) ; camera . addComponent ( CameraMoveScript ) ; 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 ; } ) ) ; plane . meshRenderer . material = planeMat ; var planeCollider = plane . addComponent ( CannonPhysicsCollider ) ; var planeShape = new CannonBoxColliderShape ( 20 , 0.01 , 20 ) ; planeCollider . restitution = 1.0 ; planeCollider . colliderShape = planeShape ; planeCollider . friction = 0.1 ; this . addSphere ( - 4 , 5 , 0 , 0 , 0 ) ; this . addSphere ( - 2 , 5 , 0 , 0.5 , 0 ) ; this . addSphere ( 0 , 5 , 0 , 0.9 , 0 ) ; this . addSphere ( 2 , 1 , 0 , 0 , 0.5 ) . linearVelocity = new Vector3 ( 0 , 0 , - 5 ) ; this . addSphere ( 4 , 1 , 0 , 0 , 0.8 ) . linearVelocity = ( new Vector3 ( 0 , 0 , - 5 ) ) ; this . addBox ( 6 , 0.6 , 0 , 0.1 ) . linearVelocity = new Vector3 ( 0 , 0 , - 10 ) ; this . addBox ( 8 , 0.6 , 0 , 0.5 ) . linearVelocity = new Vector3 ( 0 , 0 , - 10 ) ; } ) ) ; } addSphere ( x , y , z , restitution , damp ) { var radius = 1 ; var sphere = < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createSphere ( 1 ) ) ) ; var sphereTransform = sphere . transform ; var pos = sphereTransform . position ; pos . setValue ( x , y , z ) ; var scale = sphereTransform . getWorldLossyScale ( ) ; scale . setValue ( 0.5 , 0.5 , 0.5 ) ; sphereTransform . setWorldLossyScale ( scale ) ; sphereTransform . position = pos ; var rigidBody = sphere . addComponent ( CannonRigidbody3D ) ; var sphereShape = new CannonSphereColliderShape ( radius ) ; rigidBody . colliderShape = sphereShape ; rigidBody . mass = 10 ; rigidBody . restitution = restitution ; rigidBody . angularDamping = damp ; rigidBody . linearDamping = 0.1 ; return rigidBody ; } addBox ( x , y , z , friction ) { var sX = 1 ; var sY = 1 ; var sZ = 1 ; var box = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createBox ( sX , sY , sZ ) ) ) ) ; var transform = box . transform ; var pos = transform . position ; pos . setValue ( x , y , z ) ; transform . position = pos ; var rigidBody = box . addComponent ( CannonRigidbody3D ) ; var boxShape = new CannonBoxColliderShape ( sX , sY , sZ ) ; rigidBody . colliderShape = boxShape ; rigidBody . mass = 10 ; rigidBody . friction = friction ; return rigidBody ; } }