import { Laya } from "Laya" ; import { Camera } from "laya/d3/core/Camera" ; import { Material } from "laya/d3/core/material/Material" ; import { RenderState } from "laya/d3/core/material/RenderState" ; import { UnlitMaterial } from "laya/d3/core/material/UnlitMaterial" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { Stage } from "laya/display/Stage" ; import { Loader } from "laya/net/Loader" ; import { RenderTextureDepthFormat } from "laya/resource/RenderTextureFormat" ; 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 { Event } from "laya/events/Event" ; import { Button } from "laya/ui/Button" ; import Client from "../../Client" ; author miner
  export class StencilDemo { stencilMat : Material ; constructor ( ) { Laya3D . init ( 0 , 0 ) ; Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; Stat . show ( ) ; var resource = [ "res/threeDimen/scene/ChangeMaterialDemo/Conventional/scene.ls" , "res/threeDimen/texture/earth.png" ] ; Laya . loader . create ( resource , Handler . create ( this , this . onPreLoadFinish ) ) ; } onPreLoadFinish ( ) { var scene = ( < Scene3D > Laya . stage . addChild ( Loader . getRes ( "res/threeDimen/scene/ChangeMaterialDemo/Conventional/scene.ls" ) ) ) ; var camera = ( < Camera > scene . getChildByName ( "Main Camera" ) ) ; camera . depthTextureFormat = RenderTextureDepthFormat . DEPTHSTENCIL_24_8 ; camera . addComponent ( CameraMoveScript ) ; let sphere = ( < MeshSprite3D > scene . getChildByName ( "Sphere" ) ) ; let sphereClone = sphere . clone ( ) as MeshSprite3D ; scene . addChild ( sphereClone ) ; let matW = sphere . meshRenderer . sharedMaterial ; matW . stencilRef = 2 ; matW . stencilWrite = true ; matW . stencilTest = RenderState . STENCILTEST_ALWAYS ; matW . renderQueue = Material . RENDERQUEUE_OPAQUE ; Vector3 . scale ( sphereClone . transform . localScale , 1.5 , sphereClone . transform . localScale ) ; sphereClone . transform . localScale = sphereClone . transform . localScale ; let mat = new UnlitMaterial ( ) ; mat . albedoColor = new Vector4 ( 0.8 , 0.5 , 0.1 ) ; sphereClone . meshRenderer . sharedMaterial = mat ; mat . stencilRef = 0 ; mat . stencilWrite = false ; mat . stencilTest = RenderState . STENCILTEST_GEQUAL ; mat . renderQueue = Material . RENDERQUEUE_OPAQUE + 1 ; this . stencilMat = mat ; this . loadUI ( ) ; } private curStateIndex : number = 0 ; private loadUI ( ) { Laya . loader . load ( [ "res/threeDimen/ui/button.png" ] , Handler . create ( this , function ( ) : void { this . changeActionButton = Laya . stage . addChild ( new Button ( "res/threeDimen/ui/button.png" , "Stencil开启" ) ) ; this . changeActionButton . size ( 160 , 40 ) ; this . changeActionButton . labelBold = true ; this . changeActionButton . labelSize = 30 ; this . changeActionButton . sizeGrid = "4,4,4,4" ; this . changeActionButton . scale ( Browser . pixelRatio , Browser . pixelRatio ) ; this . changeActionButton . pos ( Laya . stage . width / 2 - this . changeActionButton . width * Browser . pixelRatio / 2 , Laya . stage . height - 100 * Browser . pixelRatio ) ; this . changeActionButton . on ( Event . CLICK , this , this . stypeFun0 ) ; } ) ) ; } private changeActionButton : Button ; isMaster : any ; /**实例类型*/ private btype : any = "StencilDemo" ; stypeFun0 ( label = "Stencil开启" ) { if ( ++ this . curStateIndex % 2 == 1 ) { this . changeActionButton . label = "Stencil开启" ; this . stencilMat . stencilTest = RenderState . STENCILTEST_OFF ; } else { this . changeActionButton . label = "Stencil关闭" ; this . stencilMat . stencilTest = RenderState . STENCILTEST_GEQUAL ; } label = this . changeActionButton . label ; Client . instance . send ( { type : "next" , btype : this . btype , stype : 0 , value : label } ) ; } }