|
| 1 | +import { EventDispatcher, Vector4, REVISION, createCanvasElement, SRGBColorSpace } from 'three'; |
| 2 | + |
| 3 | +class CanvasRenderTarget extends EventDispatcher { |
| 4 | + |
| 5 | + constructor( parameters ) { |
| 6 | + |
| 7 | + super(); |
| 8 | + |
| 9 | + this.isCanvasRenderTarget = true; |
| 10 | + |
| 11 | + this.canvas = parameters.canvas; |
| 12 | + this.context = parameters.context; |
| 13 | + this._domElement = parameters.domElement; |
| 14 | + this.alpha = ( parameters.alpha === undefined ) ? true : parameters.alpha; |
| 15 | + |
| 16 | + this.antialias = ( parameters.antialias === true ); |
| 17 | + |
| 18 | + if ( this.antialias === true ) { |
| 19 | + |
| 20 | + this.sampleCount = ( parameters.sampleCount === undefined ) ? 4 : parameters.sampleCount; |
| 21 | + |
| 22 | + } else { |
| 23 | + |
| 24 | + this.sampleCount = 1; |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + this.outputColorSpace = SRGBColorSpace; |
| 29 | + |
| 30 | + this.depth = true; |
| 31 | + this.stencil = false; |
| 32 | + |
| 33 | + this._width = 0; |
| 34 | + this._height = 0; |
| 35 | + this.pixelRatio = 1; |
| 36 | + |
| 37 | + this.viewport = new Vector4( 0, 0, this._width, this._height ); |
| 38 | + this.scissor = new Vector4( 0, 0, this._width, this._height ); |
| 39 | + this.scissorTest = false; |
| 40 | + |
| 41 | + this.version = 0; |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + set needsUpdate( value ) { |
| 46 | + |
| 47 | + if ( value === true ) this.version ++; |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + get domElement() { |
| 52 | + |
| 53 | + let domElement = this._domElement; |
| 54 | + |
| 55 | + if ( ! domElement ) { |
| 56 | + |
| 57 | + domElement = ( this.canvas !== undefined ) ? this.canvas : createCanvasElement(); |
| 58 | + |
| 59 | + // OffscreenCanvas does not have setAttribute, see #22811 |
| 60 | + if ( 'setAttribute' in domElement ) domElement.setAttribute( 'data-engine', `three.js r${REVISION} webgpu` ); |
| 61 | + |
| 62 | + this._domElement = domElement; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + return domElement; |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + get samples() { |
| 71 | + |
| 72 | + return this.sampleCount; |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + get depthBuffer() { |
| 77 | + |
| 78 | + return this.depth; |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + get stencilBuffer() { |
| 83 | + |
| 84 | + return this.stencil; |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + getPixelRatio() { |
| 89 | + |
| 90 | + return this.pixelRatio; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + getDrawingBufferSize( target ) { |
| 95 | + |
| 96 | + return target.set( this._width * this.pixelRatio, this._height * this.pixelRatio ).floor(); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + getSize( target ) { |
| 101 | + |
| 102 | + return target.set( this._width, this._height ); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + setPixelRatio( value = 1 ) { |
| 107 | + |
| 108 | + this.pixelRatio = value; |
| 109 | + |
| 110 | + this.setSize( this._width, this._height, false ); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + setDrawingBufferSize( width, height, pixelRatio ) { |
| 115 | + |
| 116 | + this._width = width; |
| 117 | + this._height = height; |
| 118 | + |
| 119 | + this.pixelRatio = pixelRatio; |
| 120 | + |
| 121 | + this.domElement.width = Math.floor( width * pixelRatio ); |
| 122 | + this.domElement.height = Math.floor( height * pixelRatio ); |
| 123 | + |
| 124 | + this.setViewport( 0, 0, width, height ); |
| 125 | + |
| 126 | + this.needsUpdate = true; |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + setSize( width, height, updateStyle = true ) { |
| 131 | + |
| 132 | + this._width = width; |
| 133 | + this._height = height; |
| 134 | + |
| 135 | + this.domElement.width = Math.floor( width * this.pixelRatio ); |
| 136 | + this.domElement.height = Math.floor( height * this.pixelRatio ); |
| 137 | + |
| 138 | + if ( updateStyle === true ) { |
| 139 | + |
| 140 | + this.domElement.style.width = width + 'px'; |
| 141 | + this.domElement.style.height = height + 'px'; |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + this.setViewport( 0, 0, width, height ); |
| 146 | + |
| 147 | + this.needsUpdate = true; |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + getScissor( target ) { |
| 152 | + |
| 153 | + const scissor = this.scissor; |
| 154 | + |
| 155 | + target.x = scissor.x; |
| 156 | + target.y = scissor.y; |
| 157 | + target.width = scissor.width; |
| 158 | + target.height = scissor.height; |
| 159 | + |
| 160 | + return target; |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + setScissor( x, y, width, height ) { |
| 165 | + |
| 166 | + const scissor = this.scissor; |
| 167 | + |
| 168 | + if ( x.isVector4 ) { |
| 169 | + |
| 170 | + scissor.copy( x ); |
| 171 | + |
| 172 | + } else { |
| 173 | + |
| 174 | + scissor.set( x, y, width, height ); |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + getScissorTest() { |
| 181 | + |
| 182 | + return this.scissorTest; |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + setScissorTest( value ) { |
| 187 | + |
| 188 | + this.scissorTest = value; |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + getViewport( target ) { |
| 193 | + |
| 194 | + return target.copy( this.viewport ); |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) { |
| 199 | + |
| 200 | + const viewport = this.viewport; |
| 201 | + |
| 202 | + if ( x.isVector4 ) { |
| 203 | + |
| 204 | + viewport.copy( x ); |
| 205 | + |
| 206 | + } else { |
| 207 | + |
| 208 | + viewport.set( x, y, width, height ); |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + viewport.minDepth = minDepth; |
| 213 | + viewport.maxDepth = maxDepth; |
| 214 | + |
| 215 | + } |
| 216 | + |
| 217 | + dispose() { |
| 218 | + |
| 219 | + this.dispatchEvent( { type: 'dispose' } ); |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | +} |
| 224 | + |
| 225 | +export default CanvasRenderTarget; |
0 commit comments