@@ -5,13 +5,16 @@ import { DependentMode, dependentComponents } from "./ComponentsDependencies";
5
5
import { Entity } from "./Entity" ;
6
6
import { Layer } from "./Layer" ;
7
7
import { BasicRenderPipeline } from "./RenderPipeline/BasicRenderPipeline" ;
8
+ import { PipelineUtils } from "./RenderPipeline/PipelineUtils" ;
8
9
import { Transform } from "./Transform" ;
9
10
import { VirtualCamera } from "./VirtualCamera" ;
10
11
import { Logger } from "./base" ;
11
12
import { deepClone , ignoreClone } from "./clone/CloneManager" ;
12
13
import { CameraClearFlags } from "./enums/CameraClearFlags" ;
13
14
import { CameraType } from "./enums/CameraType" ;
14
15
import { DepthTextureMode } from "./enums/DepthTextureMode" ;
16
+ import { Downsampling } from "./enums/Downsampling" ;
17
+ import { MSAASamples } from "./enums/MSAASamples" ;
15
18
import { Shader } from "./shader/Shader" ;
16
19
import { ShaderData } from "./shader/ShaderData" ;
17
20
import { ShaderMacroCollection } from "./shader/ShaderMacroCollection" ;
@@ -35,6 +38,8 @@ class MathTemp {
35
38
export class Camera extends Component {
36
39
/** @internal */
37
40
static _cameraDepthTextureProperty = ShaderProperty . getByName ( "camera_DepthTexture" ) ;
41
+ /** @internal */
42
+ static _cameraOpaqueTextureProperty = ShaderProperty . getByName ( "camera_OpaqueTexture" ) ;
38
43
39
44
private static _inverseViewMatrixProperty = ShaderProperty . getByName ( "camera_ViewInvMat" ) ;
40
45
private static _cameraPositionProperty = ShaderProperty . getByName ( "camera_Position" ) ;
@@ -47,6 +52,7 @@ export class Camera extends Component {
47
52
48
53
/**
49
54
* Determining what to clear when rendering by a Camera.
55
+ *
50
56
* @defaultValue `CameraClearFlags.All`
51
57
*/
52
58
clearFlags : CameraClearFlags = CameraClearFlags . All ;
@@ -59,10 +65,26 @@ export class Camera extends Component {
59
65
60
66
/**
61
67
* Depth texture mode.
68
+ * If `DepthTextureMode.PrePass` is used, the depth texture can be accessed in the shader using `camera_DepthTexture`.
69
+ *
62
70
* @defaultValue `DepthTextureMode.None`
63
71
*/
64
72
depthTextureMode : DepthTextureMode = DepthTextureMode . None ;
65
73
74
+ /**
75
+ * Opacity texture down sampling.
76
+ *
77
+ * @defaultValue `Downsampling.TwoX`
78
+ */
79
+ opaqueTextureDownsampling : Downsampling = Downsampling . TwoX ;
80
+
81
+ /**
82
+ * Multi-sample anti-aliasing samples when use independent canvas mode.
83
+ *
84
+ * @remarks The `independentCanvasEnabled` property should be `true` to take effect, otherwise it will be invalid.
85
+ */
86
+ msaaSamples : MSAASamples = MSAASamples . None ;
87
+
66
88
/** @internal */
67
89
_cameraType : CameraType = CameraType . Normal ;
68
90
/** @internal */
@@ -95,6 +117,8 @@ export class Camera extends Component {
95
117
private _customAspectRatio : number | undefined = undefined ;
96
118
private _renderTarget : RenderTarget = null ;
97
119
private _depthBufferParams : Vector4 = new Vector4 ( ) ;
120
+ private _customIndependentCanvas : boolean = false ;
121
+ private _opaqueTextureEnabled : boolean = false ;
98
122
99
123
@ignoreClone
100
124
private _frustumChangeFlag : BoolUpdateFlag ;
@@ -113,6 +137,46 @@ export class Camera extends Component {
113
137
@deepClone
114
138
private _invViewProjMat : Matrix = new Matrix ( ) ;
115
139
140
+ /**
141
+ * Whether to enable opaque texture.
142
+ * If enabled, the opaque texture can be accessed in the shader using `camera_OpaqueTexture`.
143
+ *
144
+ * @defaultValue `false`
145
+ *
146
+ * @remarks If enabled, the `independentCanvasEnabled` property will be forced to be true.
147
+ */
148
+ get opaqueTextureEnabled ( ) : boolean {
149
+ return this . _opaqueTextureEnabled ;
150
+ }
151
+
152
+ set opaqueTextureEnabled ( value : boolean ) {
153
+ if ( this . _opaqueTextureEnabled !== value ) {
154
+ this . _opaqueTextureEnabled = value ;
155
+ this . _checkMainCanvasAntialiasWaste ( ) ;
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Whether to use an independent canvas in viewport area.
161
+ *
162
+ * @remarks If true, the msaa in viewport can turn or off independently by `msaaSamples` property.
163
+ */
164
+ get independentCanvasEnabled ( ) : boolean {
165
+ const forceIndependent = this . _forceUseInternalCanvas ( ) ;
166
+ return forceIndependent || this . _customIndependentCanvas ;
167
+ }
168
+
169
+ set independentCanvasEnabled ( value : boolean ) {
170
+ const forceIndependent = this . _forceUseInternalCanvas ( ) ;
171
+ if ( forceIndependent && ! value ) {
172
+ console . warn (
173
+ "The camera is forced to use the independent canvas because the opaqueTextureEnabled property is enabled."
174
+ ) ;
175
+ }
176
+ this . _customIndependentCanvas = value ;
177
+ this . _checkMainCanvasAntialiasWaste ( ) ;
178
+ }
179
+
116
180
/**
117
181
* Shader data.
118
182
*/
@@ -325,6 +389,7 @@ export class Camera extends Component {
325
389
value && this . _addResourceReferCount ( value , 1 ) ;
326
390
this . _renderTarget = value ;
327
391
this . _onPixelViewportChanged ( ) ;
392
+ this . _checkMainCanvasAntialiasWaste ( ) ;
328
393
}
329
394
}
330
395
@@ -712,9 +777,22 @@ export class Camera extends Component {
712
777
return this . _inverseProjectionMatrix ;
713
778
}
714
779
780
+ private _forceUseInternalCanvas ( ) : boolean {
781
+ return ! this . _renderTarget && this . opaqueTextureEnabled ;
782
+ }
783
+
715
784
@ignoreClone
716
785
private _onPixelViewportChanged ( ) : void {
717
786
this . _updatePixelViewport ( ) ;
718
787
this . _customAspectRatio ?? this . _projectionMatrixChange ( ) ;
788
+ this . _checkMainCanvasAntialiasWaste ( ) ;
789
+ }
790
+
791
+ private _checkMainCanvasAntialiasWaste ( ) : void {
792
+ if ( this . independentCanvasEnabled && Vector4 . equals ( this . _viewport , PipelineUtils . defaultViewport ) ) {
793
+ console . warn (
794
+ "Camera use independent canvas and viewport cover the whole screen, it is recommended to disable antialias, depth and stencil to save memory when create engine."
795
+ ) ;
796
+ }
719
797
}
720
798
}
0 commit comments