{"record":{"id":"a29cda843ef2075e","repo":"mrdoob/three.js","slug":"three-webglrenderer-attached-depthtexture-is-init","errorCode":null,"errorMessage":"THREE.WebGLRenderer: Attached DepthTexture is initialized to the incorrect size.","messagePattern":"THREE\\.WebGLRenderer: Attached DepthTexture is initialized to the incorrect size\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/renderers/WebGLRenderer.js","lineNumber":2954,"sourceCode":"\t\t\t\t} else if ( renderTargetProperties.__hasExternalTextures ) {\n\n\t\t\t\t\t// Color and depth texture must be rebound in order for the swapchain to update.\n\t\t\t\t\ttextures.rebindTextures( renderTarget, properties.get( renderTarget.texture ).__webglTexture, properties.get( renderTarget.depthTexture ).__webglTexture );\n\n\t\t\t\t} else if ( renderTarget.depthBuffer ) {\n\n\t\t\t\t\t// check if the depth texture is already bound to the frame buffer and that it's been initialized\n\t\t\t\t\tconst depthTexture = renderTarget.depthTexture;\n\t\t\t\t\tif ( renderTargetProperties.__boundDepthTexture !== depthTexture ) {\n\n\t\t\t\t\t\t// check if the depth texture is compatible\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tdepthTexture !== null &&\n\t\t\t\t\t\t\tproperties.has( depthTexture ) &&\n\t\t\t\t\t\t\t( renderTarget.width !== depthTexture.image.width || renderTarget.height !== depthTexture.image.height )\n\t\t\t\t\t\t) {\n\n\t\t\t\t\t\t\tthrow new Error( 'THREE.WebGLRenderer: Attached DepthTexture is initialized to the incorrect size.' );\n\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Swap the depth buffer to the currently attached one\n\t\t\t\t\t\ttextures.setupDepthRenderbuffer( renderTarget );\n\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t\tconst texture = renderTarget.texture;\n\n\t\t\t\tif ( texture.isData3DTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {\n\n\t\t\t\t\tisRenderTarget3D = true;\n\n\t\t\t\t}\n","sourceCodeStart":2936,"sourceCodeEnd":2972,"githubUrl":"https://github.com/mrdoob/three.js/blob/da05705fa31f02710c19772a2aa70c7454807f0c/src/renderers/WebGLRenderer.js#L2936-L2972","documentation":"Thrown when a render target's attached DepthTexture has been initialized to dimensions that do not match the render target's width/height. The check fires while setting up the depth renderbuffer when a different depth texture is bound. Mismatched depth-texture dimensions make the framebuffer incomplete, so three.js refuses it.","triggerScenarios":"Assigning renderTarget.depthTexture = new THREE.DepthTexture(w, h) with w/h != renderTarget.width/height; resizing the renderTarget (resize()) after attaching a depth texture without also recreating the depth texture; reusing a depth texture across differently-sized targets.","commonSituations":"Window/canvas resize handlers that call renderTarget.setSize() but forget to recreate the DepthTexture; shadow-map or post-processing pipelines that share a DepthTexture; copy-paste of DepthTexture setup with hardcoded sizes.","solutions":["Create the DepthTexture with the SAME dimensions as the render target, and recreate it whenever the render target is resized.","In your resize handler, after renderTarget.setSize(w,h), also set renderTarget.depthTexture = new THREE.DepthTexture(w, h, ...).","Do not share a single DepthTexture across render targets of different sizes; create one per target.","Validate depthTexture.image.width/height against renderTarget.width/height before rendering."],"exampleFix":"// before\nconst rt = new THREE.WebGLRenderTarget( 1024, 1024 );\nrt.depthTexture = new THREE.DepthTexture( 512, 512 ); // wrong size\n\n// after\nconst rt = new THREE.WebGLRenderTarget( 1024, 1024 );\nrt.depthTexture = new THREE.DepthTexture( rt.width, rt.height );\n// on resize:\nfunction onResize( w, h ) {\n  rt.setSize( w, h );\n  rt.depthTexture = new THREE.DepthTexture( w, h );\n}","handlingStrategy":"validation","validationCode":"function attachDepthTexture( renderTarget, type = THREE.UnsignedIntType ) {\n  renderTarget.depthTexture = new THREE.DepthTexture( renderTarget.width, renderTarget.height, type );\n}\n// call after every setSize:\nfunction resizeRT( renderTarget, w, h ) {\n  renderTarget.setSize( w, h );\n  attachDepthTexture( renderTarget );\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always size DepthTexture to match the render target exactly.","Recreate the DepthTexture whenever the render target is resized.","Do not share a single DepthTexture across differently-sized targets.","Validate depthTexture.image.width/height against renderTarget.width/height before rendering."],"tags":["webglrenderer","rendertarget","depth-texture","framebuffer","resize"],"backgroundTag":null,"analyzedSha":"da05705fa31f02710c19772a2aa70c7454807f0c","analyzedAt":"2026-08-12T22:51:37.160Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}