import { Laya } from "Laya" ; import { Camera , CameraEventFlags } from "laya/d3/core/Camera" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { Stage } from "laya/display/Stage" ; import { Handler } from "laya/utils/Handler" ; import { Stat } from "laya/utils/Stat" ; import { Laya3D } from "Laya3D" ; import { Shader3D } from "laya/d3/shader/Shader3D" ; import { CommandBuffer } from "laya/d3/core/render/command/CommandBuffer" ; import { RenderTexture } from "laya/d3/resource/RenderTexture" ; import { Viewport } from "laya/d3/math/Viewport" ; import { RenderTextureDepthFormat , RenderTextureFormat } from "laya/resource/RenderTextureFormat" ; import { ShaderData } from "laya/d3/shader/ShaderData" ; import { FilterMode } from "laya/resource/FilterMode" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { PBRStandardMaterial } from "laya/d3/core/material/PBRStandardMaterial" ; import { BlurEffect } from "../LayaAir3D_PostProcess/BlurShader/BlurEffect" ; import { CameraMoveScript } from "../common/CameraMoveScript" ; import { GlassWithoutGrabMaterial } from "./CommandBufferDemo/GlassWithoutGrabMaterial" ; export class CommandBuffer_BlurryGlass { mat : GlassWithoutGrabMaterial ; texture : RenderTexture ; constructor ( ) { Laya3D . init ( 100 , 100 ) ; Stat . show ( ) ; Shader3D . debugMode = true ; Laya . stage . scaleMode = Stage . SCALE_FULL ; Laya . stage . screenMode = Stage . SCREEN_NONE ; BlurEffect . init ( ) ; GlassWithoutGrabMaterial . init ( ) ; Scene3D . load ( "res/threeDimen/BlurryRefraction/Conventional/BlurryGlass.ls" , Handler . create ( this , function ( scene ) : void { ( < Scene3D > Laya . stage . addChild ( scene ) ) ; var camera = ( < Camera > scene . getChildByName ( "Main Camera" ) ) ; camera . addComponent ( CameraMoveScript ) ; var glass01 = scene . getChildByName ( "glass01" ) as MeshSprite3D ; var glass02 = scene . getChildByName ( "glass02" ) as MeshSprite3D ; var pbrStandard = glass01 . meshRenderer . sharedMaterial as PBRStandardMaterial ; var glassMaterial = new GlassWithoutGrabMaterial ( pbrStandard . albedoTexture ) ; glass01 . meshRenderer . sharedMaterial = glassMaterial ; glass02 . meshRenderer . sharedMaterial = glassMaterial ; this . mat = glassMaterial ; this . createCommandBuffer ( camera ) ; } ) ) ; } param camera createCommandBuffer ( camera ) { camera . enableBuiltInRenderTexture = true ; var buf = new CommandBuffer ( ) ; var viewPort = camera . viewport ; var renderTexture = RenderTexture . createFromPool ( viewPort . width , viewPort . height , RenderTextureFormat . R8G8B8 , RenderTextureDepthFormat . DEPTHSTENCIL_NONE ) ; this . texture = renderTexture ; buf . blitScreenTriangle ( null , renderTexture ) ; var shader = Shader3D . find ( "blurEffect" ) ; var shaderValue = new ShaderData ( ) ; var downSampleFactor = 4 ; var downSampleWidth = viewPort . width / downSampleFactor ; var downSampleheigh = viewPort . height / downSampleFactor ; var texSize = new Vector4 ( 1.0 / viewPort . width , 1.0 / viewPort . height , viewPort . width , downSampleheigh ) ; shaderValue . setNumber ( BlurEffect . SHADERVALUE_DOWNSAMPLEVALUE , 1 ) ; shaderValue . setVector ( BlurEffect . SHADERVALUE_TEXELSIZE , texSize ) ; var downRenderTexture = RenderTexture . createFromPool ( downSampleWidth , downSampleheigh , RenderTextureFormat . R8G8B8 , RenderTextureDepthFormat . DEPTHSTENCIL_NONE ) ; buf . blitScreenTriangle ( renderTexture , downRenderTexture , null , shader , shaderValue , 0 ) ; var blurTexture = RenderTexture . createFromPool ( downSampleWidth , downSampleheigh , RenderTextureFormat . R8G8B8 , RenderTextureDepthFormat . DEPTHSTENCIL_NONE ) ; blurTexture . filterMode = FilterMode . Bilinear ; buf . blitScreenTriangle ( downRenderTexture , blurTexture , null , shader , shaderValue , 1 ) ; buf . blitScreenTriangle ( blurTexture , downRenderTexture , null , shader , shaderValue , 2 ) ; buf . blitScreenTriangle ( downRenderTexture , blurTexture , null , shader , shaderValue , 1 ) ; buf . blitScreenTriangle ( blurTexture , downRenderTexture , null , shader , shaderValue , 2 ) ; var globalUniformNameID = Shader3D . propertyNameToID ( "u_screenTexture" ) ; buf . setGlobalTexture ( globalUniformNameID , downRenderTexture ) ; camera . addCommandBuffer ( CameraEventFlags . BeforeTransparent , buf ) ; RenderTexture . recoverToPool ( downRenderTexture ) ; RenderTexture . recoverToPool ( blurTexture ) ; return ; } }