Skip to content

Commit

Permalink
Add depthTexture to gbuffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
repalash committed Feb 3, 2024
1 parent a8b23c5 commit 4757719
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
17 changes: 16 additions & 1 deletion examples/gbuffer-plugin/script.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
_testFinish,
downloadBlob,
FloatType,
GBufferPlugin,
HalfFloatType,
IObject3D,
Expand All @@ -12,11 +13,18 @@ import {createSimpleButtons} from '../examples-utils/simple-bottom-buttons.js'
const viewer = new ThreeViewer({
canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
msaa: true,
zPrepass: true,
})

async function init() {

const gbufferPlugin = viewer.addPluginSync(new GBufferPlugin(HalfFloatType))
const gbufferPlugin = viewer.addPluginSync(new GBufferPlugin(
HalfFloatType,
true, // isPrimaryGBuffer (used for zprepass etc)
true, // enabled by default
true, // render the flags buffer (used for eg selective tonemapping)
true, // render depth texture
FloatType)) // render depth texture as Float type. available - UnsignedShort(16 bits), UnsignedInt(24 bits) or Float(32 bits)

await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
const model = await viewer.load<IObject3D>('https://threejs.org/examples/models/gltf/kira.glb', {
Expand Down Expand Up @@ -48,10 +56,12 @@ async function init() {

const getNormalDepth = ()=>({texture: gbufferPlugin.normalDepthTexture})
const getFlags = ()=>({texture: gbufferPlugin.flagsTexture})
const getDepthTexture = ()=>({texture: gbufferPlugin.depthTexture})

const targetPreview = await viewer.addPlugin(RenderTargetPreviewPlugin)
targetPreview.addTarget(getNormalDepth, 'normalDepth')
targetPreview.addTarget(getFlags, 'gBufferFlags')
targetPreview.addTarget(getDepthTexture, 'depthTexture')

const screenPass = viewer.renderManager.screenPass

Expand All @@ -66,6 +76,11 @@ async function init() {
screenPass.overrideReadBuffer = screenPass.overrideReadBuffer?.texture === rt.texture ? null : rt
viewer.setDirty()
},
['Toggle Depth Texture']: () => {
const rt = getDepthTexture()
screenPass.overrideReadBuffer = screenPass.overrideReadBuffer?.texture === rt.texture ? null : rt
viewer.setDirty()
},
['Download snapshot']: async(btn: HTMLButtonElement) => {
btn.disabled = true
const blob = await viewer.getScreenshotBlob({mimeType: 'image/png'})
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "threepipe",
"version": "0.0.21",
"version": "0.0.22",
"description": "A 3D viewer framework built on top of three.js in TypeScript with a focus on quality rendering, modularity and extensibility.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
20 changes: 18 additions & 2 deletions src/plugins/pipeline/GBufferPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
BufferGeometry,
Camera,
Color,
DepthTexture,
DoubleSide,
FloatType,
GLSL1,
GLSL3,
IUniform,
Expand All @@ -17,6 +19,8 @@ import {
UniformsLib,
UniformsUtils,
UnsignedByteType,
UnsignedIntType,
UnsignedShortType,
Vector2,
Vector4,
WebGLMultipleRenderTargets,
Expand Down Expand Up @@ -82,6 +86,12 @@ export class GBufferPlugin
get flagsTexture(): ITexture|undefined {
return this.textures[1]
}

@uiImage(/* {readOnly: true}*/)
get depthTexture(): (ITexture&DepthTexture)|undefined {
return this.target?.depthTexture
}

// @uiConfig() // not supported in this material yet
material?: GBufferMaterial

Expand Down Expand Up @@ -115,12 +125,14 @@ export class GBufferPlugin
extraUniforms: {
tNormalDepth: ()=>({value: this.normalDepthTexture}),
tGBufferFlags: ()=>({value: this.flagsTexture}),
tGBufferDepthTexture: ()=>({value: this.depthTexture}),
},
extraDefines: {
// ['GBUFFER_PACKING']: BasicDepthPacking,
['HAS_NORMAL_DEPTH_BUFFER']: ()=>this.normalDepthTexture ? 1 : undefined,
// ['HAS_FLAGS_BUFFER']: ()=>this.flagsTexture ? 1 : undefined,
['GBUFFER_HAS_DEPTH_TEXTURE']: ()=>this.depthTexture ? 1 : undefined,
['GBUFFER_HAS_FLAGS']: ()=>this.flagsTexture ? 1 : undefined,
// ['HAS_FLAGS_BUFFER']: ()=>this.flagsTexture ? 1 : undefined,
['HAS_GBUFFER']: ()=>this.isPrimaryGBuffer && this.normalDepthTexture ? 1 : undefined,
},
priority: 100,
Expand All @@ -139,6 +151,8 @@ export class GBufferPlugin
samples: this._viewer.renderManager.composerTarget.samples || 0,
type: this.bufferType,
textureCount: useMultiple ? 2 : 1,
depthTexture: this.renderDepthTexture,
depthTextureType: this.depthTextureType,
// magFilter: NearestFilter,
// minFilter: NearestFilter,
// generateMipmaps: false,
Expand Down Expand Up @@ -208,7 +222,9 @@ export class GBufferPlugin
bufferType: TextureDataType = UnsignedByteType,
isPrimaryGBuffer = true,
enabled = true,
public renderFlagsBuffer: boolean = true
public renderFlagsBuffer: boolean = true,
public renderDepthTexture: boolean = false,
public depthTextureType: typeof UnsignedShortType | typeof UnsignedIntType | typeof FloatType /* | typeof UnsignedInt248Type*/ = UnsignedIntType,
// packing: DepthPackingStrategies = BasicDepthPacking,
) {
super()
Expand Down
12 changes: 12 additions & 0 deletions src/plugins/pipeline/shaders/GBufferPlugin.unpack.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ ivec4 getGBufferFlags(const in vec2 uv){
return ivec4(1);
#endif
}

#if defined(GBUFFER_HAS_DEPTH_TEXTURE) && GBUFFER_HAS_DEPTH_TEXTURE == 1
// NOT TESTED
uniform sampler2D tGBufferDepthTexture;
// needs <packing>. Its made sure that its included in the unpackExtension
float getDepthTexture( vec2 coord, float cameraNear, float cameraFar ) {
float fragCoordZ = texture2D( tGBufferDepthTexture, coord ).x;
float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
}
#endif

//#if DEPTH_NORMAL_TEXTURE == 1
//uniform sampler2D tNormalDepth;
//#else
Expand Down
8 changes: 8 additions & 0 deletions src/rendering/RenderTarget.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {
ColorSpace,
DepthFormat,
DepthStencilFormat,
EventDispatcher,
FloatType,
MagnificationTextureFilter,
MinificationTextureFilter,
Texture,
TextureDataType,
UnsignedInt248Type,
UnsignedIntType,
UnsignedShortType,
Wrapping,
} from 'three'
import {Vector4} from 'three/src/math/Vector4'
Expand Down Expand Up @@ -75,6 +81,8 @@ export interface CreateRenderTargetOptions {
format?: number
depthBuffer?: boolean
depthTexture?: boolean
depthTextureType?: typeof UnsignedShortType | typeof UnsignedInt248Type | typeof UnsignedIntType | typeof FloatType
depthTextureFormat?: typeof DepthFormat | typeof DepthStencilFormat
textureCount?: number
wrapS?: Wrapping
wrapT?: Wrapping
Expand Down
7 changes: 6 additions & 1 deletion src/rendering/RenderTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {createRenderTargetKey, CreateRenderTargetOptions, IRenderTarget} from '.
import {
BaseEvent,
ClampToEdgeWrapping,
DepthFormat,
DepthTexture,
EventDispatcher,
LinearFilter,
Expand All @@ -11,6 +12,7 @@ import {
RGBAFormat,
Texture,
UnsignedByteType,
UnsignedIntType,
Vector2,
WebGLCubeRenderTarget,
WebGLMultipleRenderTargets,
Expand Down Expand Up @@ -58,6 +60,8 @@ export abstract class RenderTargetManager<E extends BaseEvent = BaseEvent, ET ex
format = RGBAFormat,
depthBuffer = true,
depthTexture = false,
depthTextureType = UnsignedIntType,
depthTextureFormat = DepthFormat,
size = undefined,
textureCount = 1,
...op
Expand All @@ -68,7 +72,8 @@ export abstract class RenderTargetManager<E extends BaseEvent = BaseEvent, ET ex
size = size || this.renderSize.clone().multiplyScalar(this.renderScale * (sizeMultiplier = sizeMultiplier || 1))
size.width = Math.floor(size.width)
size.height = Math.floor(size.height)
const depthTex = depthTexture ? new DepthTexture(size.width, size.height, UnsignedByteType) : undefined
const depthTex = depthTexture ? new DepthTexture(size.width, size.height, depthTextureType) : undefined
if (depthTex) depthTex.format = depthTextureFormat
const target = this.createTargetCustom<T>(textureCount > 1 ? {
width: size.width,
height: size.height,
Expand Down
2 changes: 1 addition & 1 deletion src/viewer/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '0.0.21'
export const VERSION = '0.0.22'

0 comments on commit 4757719

Please sign in to comment.