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 { Transform3D } from "laya/d3/core/Transform3D" ; import { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Vector4 } from "laya/d3/math/Vector4" ; 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 { ConeColliderShape } from "laya/d3/physics/shape/ConeColliderShape" ; import { CylinderColliderShape } from "laya/d3/physics/shape/CylinderColliderShape" ; import { SphereColliderShape } from "laya/d3/physics/shape/SphereColliderShape" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { Stage } from "laya/display/Stage" ; import { Texture2D } from "laya/resource/Texture2D" ; 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" ; export class PhysicsWorld_BaseCollider { private scene : Scene3D ; private tmpVector : Vector3 = new Vector3 ( 0 , 0 , 0 ) ; private mat1 : BlinnPhongMaterial ; private mat2 : BlinnPhongMaterial ; private mat3 : BlinnPhongMaterial ; private mat4 : BlinnPhongMaterial ; private mat5 : BlinnPhongMaterial ; constructor ( ) { Laya3D . init ( 0 , 0 , null , Handler . create ( null , ( ) => { Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; Config3D . useCannonPhysics = false ; 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 ( 10 , 10 , 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 planeStaticCollider = plane . addComponent ( PhysicsCollider ) ; var planeShape = new BoxColliderShape ( 10 , 0 , 10 ) ; planeStaticCollider . colliderShape = planeShape ; planeStaticCollider . friction = 2 ; planeStaticCollider . restitution = 0.3 ; this . mat1 = new BlinnPhongMaterial ( ) ; this . mat2 = new BlinnPhongMaterial ( ) ; this . mat3 = new BlinnPhongMaterial ( ) ; this . mat4 = new BlinnPhongMaterial ( ) ; this . mat5 = 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 ; } ) ) ; Texture2D . load ( "res/threeDimen/Physics/steel2.jpg" , Handler . create ( this , function ( tex ) : void { this . mat4 . albedoTexture = tex ; } ) ) ; Texture2D . load ( "res/threeDimen/Physics/steel.jpg" , Handler . create ( this , function ( tex ) : void { this . mat5 . albedoTexture = tex ; } ) ) ; this . randomAddPhysicsSprite ( ) ; } ) ) ; } randomAddPhysicsSprite ( ) { Laya . timer . loop ( 1000 , this , function ( ) : void { var random = Math . floor ( Math . random ( ) * 5 ) % 5 ; switch ( random ) { case 0 : this . addBox ( ) ; break ; case 1 : this . addSphere ( ) ; break ; case 2 : this . addCapsule ( ) ; break ; case 3 : this . addCone ( ) ; break ; case 4 : this . addCylinder ( ) ; break ; default : break ; } } ) ; } addBox ( ) { 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 = this . mat1 ; var transform = box . transform ; var pos = transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 10 , 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 ; } addSphere ( ) { var radius = Math . random ( ) * 0.2 + 0.2 ; var sphere = ( < MeshSprite3D > this . scene . addChild ( new MeshSprite3D ( PrimitiveMesh . createSphere ( radius ) ) ) ) ; sphere . meshRenderer . material = this . mat2 ; var pos = sphere . transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 10 , Math . random ( ) * 4 - 2 ) ; sphere . transform . position = pos ; var rigidBody = sphere . addComponent ( Rigidbody3D ) ; var sphereShape = new SphereColliderShape ( radius ) ; rigidBody . colliderShape = sphereShape ; rigidBody . mass = 10 ; } addCapsule ( ) { 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 = this . mat3 ; var transform = capsule . transform ; var pos = transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 10 , 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 ; } addCone ( ) { var raidius = Math . random ( ) * 0.2 + 0.2 ; var height = Math . random ( ) * 0.5 + 0.8 ; var cone = new MeshSprite3D ( PrimitiveMesh . createCone ( raidius , height ) ) ; this . scene . addChild ( cone ) ; cone . meshRenderer . material = this . mat4 ; var pos = cone . transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 10 , Math . random ( ) * 4 - 2 ) ; cone . transform . position = pos ; var rigidBody = cone . addComponent ( Rigidbody3D ) ; var coneShape = new ConeColliderShape ( raidius , height ) ; rigidBody . colliderShape = coneShape ; rigidBody . mass = 10 ; } addCylinder ( ) { var raidius = Math . random ( ) * 0.2 + 0.2 ; var height = Math . random ( ) * 0.5 + 0.8 ; var cylinder = new MeshSprite3D ( PrimitiveMesh . createCylinder ( raidius , height ) ) ; this . scene . addChild ( cylinder ) ; cylinder . meshRenderer . material = this . mat5 ; var transform = cylinder . transform ; var pos = transform . position ; pos . setValue ( Math . random ( ) * 4 - 2 , 10 , 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 = cylinder . addComponent ( Rigidbody3D ) ; var cylinderShape = new CylinderColliderShape ( raidius , height ) ; rigidBody . colliderShape = cylinderShape ; rigidBody . mass = 10 ; } }