import { Laya } from "Laya" ; import { Script3D } from "laya/d3/component/Script3D" ; import { Camera } from "laya/d3/core/Camera" ; import { DirectionLight } from "laya/d3/core/light/DirectionLight" ; import { ShadowCascadesMode } from "laya/d3/core/light/ShadowCascadesMode" ; import { ShadowMode } from "laya/d3/core/light/ShadowMode" ; import { PBRStandardMaterial } from "laya/d3/core/material/PBRStandardMaterial" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { SkinnedMeshSprite3D } from "laya/d3/core/SkinnedMeshSprite3D" ; import { Sprite3D } from "laya/d3/core/Sprite3D" ; import { Transform3D } from "laya/d3/core/Transform3D" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Mesh } from "laya/d3/resource/models/Mesh" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { Stage } from "laya/display/Stage" ; import { Event } from "laya/events/Event" ; import { Loader } from "laya/net/Loader" ; import { Button } from "laya/ui/Button" ; import { Browser } from "laya/utils/Browser" ; import { Handler } from "laya/utils/Handler" ; import { Laya3D } from "Laya3D" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; import { Stat } from "laya/utils/Stat" ; import { Utils } from "laya/utils/Utils" ; import Client from "../../Client" ; /**
 * Light rotation script.
 */ class RotationScript extends Script3D { /** Roation speed. */ autoRotateSpeed : Vector3 = new Vector3 ( 0 , 0.05 , 0 ) ; /** If roation. */ rotation : boolean = true ; onUpdate ( ) { if ( this . rotation ) ( < DirectionLight > this . owner ) . transform . rotate ( this . autoRotateSpeed , false ) ; } } /**
 * Realtime shadow sample.
 */ export class RealTimeShadow { /**实例类型*/ private btype : any = "RealTimeShadow" ; /**场景内按钮类型*/ private stype : any = 0 ; private rotationButton : Button ; private rotationScript : RotationScript ; constructor ( ) { Laya3D . init ( 0 , 0 ) ; Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; Laya . loader . create ( [ "res/threeDimen/staticModel/grid/plane.lh" , "res/threeDimen/skinModel/LayaMonkey/LayaMonkey.lh" ] , Handler . create ( this , this . onComplete ) ) ; } private onComplete ( ) { var scene = < Scene3D > Laya . stage . addChild ( new Scene3D ( ) ) ; var camera = < Camera > ( scene . addChild ( new Camera ( 0 , 0.1 , 100 ) ) ) ; camera . transform . translate ( new Vector3 ( 0 , 1.2 , 1.6 ) ) ; camera . transform . rotate ( new Vector3 ( - 35 , 0 , 0 ) , true , false ) ; camera . addComponent ( CameraMoveScript ) ; var directionLight = < DirectionLight > scene . addChild ( new DirectionLight ( ) ) ; directionLight . color = new Vector3 ( 0.85 , 0.85 , 0.8 ) ; directionLight . transform . rotate ( new Vector3 ( - Math . PI / 3 , 0 , 0 ) ) ; directionLight . shadowMode = ShadowMode . SoftLow ; directionLight . shadowDistance = 3 ; directionLight . shadowResolution = 1024 ; directionLight . shadowCascadesMode = ShadowCascadesMode . NoCascades ; directionLight . shadowNormalBias = 4 ; this . rotationScript = directionLight . addComponent ( RotationScript ) ; var grid = < Sprite3D > scene . addChild ( Loader . getRes ( "res/threeDimen/staticModel/grid/plane.lh" ) ) ; ( < MeshSprite3D > grid . getChildAt ( 0 ) ) . meshRenderer . receiveShadow = true ; var layaMonkey = < Sprite3D > scene . addChild ( Loader . getRes ( "res/threeDimen/skinModel/LayaMonkey/LayaMonkey.lh" ) ) ; layaMonkey . transform . localScale = new Vector3 ( 0.3 , 0.3 , 0.3 ) ; ( < SkinnedMeshSprite3D > layaMonkey . getChildAt ( 0 ) . getChildAt ( 0 ) ) . skinnedMeshRenderer . castShadow = true ; var sphereSprite = this . addPBRSphere ( PrimitiveMesh . createSphere ( 0.1 ) , new Vector3 ( 0 , 0.2 , 0.5 ) , scene ) ; sphereSprite . meshRenderer . castShadow = true ; this . loadUI ( ) ; } /**
     * Add one with smoothness and metallic sphere.
     */ addPBRSphere ( sphereMesh , position , scene ) { var mat = new PBRStandardMaterial ( ) ; mat . smoothness = 0.2 ; var meshSprite = new MeshSprite3D ( sphereMesh ) ; meshSprite . meshRenderer . sharedMaterial = mat ; var transform = meshSprite . transform ; transform . localPosition = position ; scene . addChild ( meshSprite ) ; return meshSprite ; } /**
     * Add Button control light rotation.
     */ loadUI ( ) { Laya . loader . load ( [ "res/threeDimen/ui/button.png" ] , Handler . create ( this , function ( ) : void { this . rotationButton = < Button > Laya . stage . addChild ( new Button ( "res/threeDimen/ui/button.png" , "Stop Rotation" ) ) ; this . rotationButton . size ( 150 , 30 ) ; this . rotationButton . labelSize = 20 ; this . rotationButton . sizeGrid = "4,4,4,4" ; this . rotationButton . scale ( Browser . pixelRatio , Browser . pixelRatio ) ; this . rotationButton . pos ( Laya . stage . width / 2 - this . rotationButton . width * Browser . pixelRatio / 2 , Laya . stage . height - 40 * Browser . pixelRatio ) ; this . rotationButton . on ( Event . CLICK , this , this . stypeFun0 ) ; } ) ) ; } stypeFun0 ( label = "Stop Rotation" ) { if ( this . rotationScript . rotation ) { this . rotationButton . label = "Start Rotation" ; this . rotationScript . rotation = false ; } else { this . rotationButton . label = "Stop Rotation" ; this . rotationScript . rotation = true ; } label = this . rotationButton . label ; Client . instance . send ( { type : "next" , btype : this . btype , stype : 0 , value : label } ) ; } }