import { Script3D } from "laya/d3/component/Script3D" ; import { MeshSprite3D } from "laya/d3/core/MeshSprite3D" ; import { Camera } from "laya/d3/core/Camera" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; import { RenderTexture } from "laya/d3/resource/RenderTexture" ; import { RenderTextureFormat } from "laya/resource/RenderTextureFormat" ; import { UnlitMaterial } from "laya/d3/core/material/UnlitMaterial" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Material } from "../../../../bin/tsc/layaAir/laya/d3/core/material/Material" ; export class ChinarMirrorPlane extends Script3D { public static oriPa : Vector3 = new Vector3 ( 5 , 0 , - 5 ) ; public static oriPb : Vector3 = new Vector3 ( - 5 , 0 , - 5 ) ; public static oriPc : Vector3 = new Vector3 ( 5 , 0 , 5 ) ; public _mirrorPlane : MeshSprite3D ; public mainCamera : Camera ; private mirrorCamera : Camera = new Camera ( ) ; private renderTexture : RenderTexture = new RenderTexture ( 1024 , 1024 , RenderTextureFormat . R8G8B8 ) ; public estimateViewFrustum : boolean = true ; public setNearClipPlane : boolean = true ; public nearClipDistanceOffset : number = - 0.01 ; private vn : Vector3 = new Vector3 ( ) ; private l : number ; private r : number ; private b : number ; private t : number ; private d : number ; private n : number ; private f : number ; private pa : Vector3 = new Vector3 ( ) ; private pb : Vector3 = new Vector3 ( ) ; private pc : Vector3 = new Vector3 ( ) ; private pe : Vector3 = new Vector3 ( ) ; private va : Vector3 = new Vector3 ( ) ; private vb : Vector3 = new Vector3 ( ) ; private vc : Vector3 = new Vector3 ( ) ; private vr : Vector3 = new Vector3 ( ) ; private vu : Vector3 = new Vector3 ( ) ; private p : Matrix4x4 = new Matrix4x4 ( ) ; private rm : Matrix4x4 = new Matrix4x4 ( ) ; private tm : Matrix4x4 = new Matrix4x4 ( ) ; private static tempMat : Matrix4x4 = new Matrix4x4 ( ) ; private static tempV3 = new Vector3 ( ) ; set mirrorPlane ( value ) { this . _mirrorPlane = value ; var material = new UnlitMaterial ( ) ; value . meshRenderer . sharedMaterial = material ; material . albedoTexture = this . renderTexture ; material . tilingOffset = new Vector4 ( - 1 , 1 , 0 , 0 ) ; } set onlyMainCamera ( value ) { ( value . scene as Scene3D ) . addChild ( this . mirrorCamera ) ; this . mainCamera = value ; } constructor ( ) { super ( ) ; } onStart ( ) { this . mirrorCamera . renderTarget = this . renderTexture ; this . mirrorCamera . clearColor = new Vector4 ( 0.0 , 0.0 , 0.0 , 1.0 ) ; } onUpdate ( ) { if ( this . mirrorCamera == null || this . _mirrorPlane == null || this . mainCamera == null ) { return ; } this . _mirrorPlane . transform . worldMatrix . invert ( ChinarMirrorPlane . tempMat ) ; Vector3 . transformV3ToV3 ( this . mainCamera . transform . position , ChinarMirrorPlane . tempMat , ChinarMirrorPlane . tempV3 ) ; ChinarMirrorPlane . tempV3 . y = - ChinarMirrorPlane . tempV3 . y ; Vector3 . transformV3ToV3 ( ChinarMirrorPlane . tempV3 , this . _mirrorPlane . transform . worldMatrix , ChinarMirrorPlane . tempV3 ) ; this . mirrorCamera . transform . position = ChinarMirrorPlane . tempV3 ; Vector3 . transformV3ToV3 ( ChinarMirrorPlane . oriPa , this . _mirrorPlane . transform . worldMatrix , this . pa ) ; Vector3 . transformV3ToV3 ( ChinarMirrorPlane . oriPb , this . _mirrorPlane . transform . worldMatrix , this . pb ) ; Vector3 . transformV3ToV3 ( ChinarMirrorPlane . oriPc , this . _mirrorPlane . transform . worldMatrix , this . pc ) ; this . pe = this . mirrorCamera . transform . position ; this . n = this . mirrorCamera . nearPlane ; this . f = this . mirrorCamera . farPlane ; Vector3 . subtract ( this . pa , this . pe , this . va ) ; Vector3 . subtract ( this . pb , this . pe , this . vb ) ; Vector3 . subtract ( this . pc , this . pe , this . vc ) ; Vector3 . subtract ( this . pb , this . pa , this . vr ) ; Vector3 . subtract ( this . pc , this . pa , this . vu ) ; Vector3 . cross ( this . va , this . vc , ChinarMirrorPlane . tempV3 ) ; if ( Vector3 . dot ( ChinarMirrorPlane . tempV3 , this . vb ) < 0.0 ) { Vector3 . scale ( this . vu , - 1 , this . vu ) ; this . pc . cloneTo ( this . pa ) ; Vector3 . add ( this . pa , this . vr , this . pb ) ; Vector3 . add ( this . pa , this . vu , this . pc ) ; Vector3 . subtract ( this . pa , this . pe , this . va ) ; Vector3 . subtract ( this . pb , this . pe , this . vb ) ; Vector3 . subtract ( this . pc , this . pe , this . vc ) ; } Vector3 . normalize ( this . vr , this . vr ) ; Vector3 . normalize ( this . vu , this . vu ) ; Vector3 . cross ( this . vr , this . vu , ChinarMirrorPlane . tempV3 ) ; Vector3 . normalize ( ChinarMirrorPlane . tempV3 , this . vn ) ; this . d = Vector3 . dot ( this . va , this . vn ) ; if ( this . setNearClipPlane ) { this . n = this . d + this . nearClipDistanceOffset ; this . mirrorCamera . nearPlane = this . n ; } this . l = Vector3 . dot ( this . vr , this . va ) * this . n / this . d ; this . r = Vector3 . dot ( this . vr , this . vb ) * this . n / this . d ; this . b = Vector3 . dot ( this . vu , this . va ) * this . n / this . d ; this . t = Vector3 . dot ( this . vu , this . vc ) * this . n / this . d ; this . p . elements [ 0 ] = 2.0 * this . n / ( this . r - this . l ) ; this . p . elements [ 4 ] = 0 ; this . p . elements [ 8 ] = ( this . r + this . l ) / ( this . r - this . l ) ; this . p . elements [ 12 ] = 0.0 ; this . p . elements [ 1 ] = 0.0 ; this . p . elements [ 5 ] = 2.0 * this . n / ( this . t - this . b ) ; this . p . elements [ 9 ] = ( this . t + this . b ) / ( this . t - this . b ) ; this . p . elements [ 13 ] = 0.0 ; this . p . elements [ 2 ] = 0 ; this . p . elements [ 6 ] = 0 ; this . p . elements [ 10 ] = ( this . f + this . n ) / ( this . n - this . f ) ; this . p . elements [ 14 ] = ( 2.0 * this . f * this . n / ( this . n - this . f ) ) / 2 ; this . p . elements [ 3 ] = 0 ; this . p . elements [ 7 ] = 0 ; this . p . elements [ 11 ] = - 1 ; this . p . elements [ 15 ] = 0 ; this . p . elements [ 0 ] *= - 1 ; this . p . elements [ 5 ] *= - 1 ; this . p . elements [ 14 ] *= - 1 ; this . rm . elements [ 0 ] = this . vr . x ; this . rm . elements [ 4 ] = this . vr . y ; this . rm . elements [ 8 ] = this . vr . z ; this . rm . elements [ 12 ] = this . pe . x ; this . rm . elements [ 1 ] = this . vu . x ; this . rm . elements [ 5 ] = this . vu . y ; this . rm . elements [ 9 ] = this . vu . z ; this . rm . elements [ 13 ] = this . pe . z ; this . rm . elements [ 2 ] = this . vn . x ; this . rm . elements [ 6 ] = this . vn . y ; this . rm . elements [ 10 ] = this . vn . z ; this . rm . elements [ 14 ] = this . pe . y ; this . rm . elements [ 3 ] = 0 ; this . rm . elements [ 7 ] = 0 ; this . rm . elements [ 11 ] = 0 ; this . rm . elements [ 15 ] = 1 ; this . mirrorCamera . projectionMatrix = this . p ; this . rm . invert ( ChinarMirrorPlane . tempMat ) ; this . mirrorCamera . transform . worldMatrix = ChinarMirrorPlane . tempMat ; if ( ! this . estimateViewFrustum ) return ; } }