浏览代码

Add depthTexture to gbuffer.

master
Palash Bansal 2 年前
父节点
当前提交
475771914e
没有帐户链接到提交者的电子邮件

+ 16
- 1
examples/gbuffer-plugin/script.ts 查看文件

import { import {
_testFinish, _testFinish,
downloadBlob, downloadBlob,
FloatType,
GBufferPlugin, GBufferPlugin,
HalfFloatType, HalfFloatType,
IObject3D, IObject3D,
const viewer = new ThreeViewer({ const viewer = new ThreeViewer({
canvas: document.getElementById('mcanvas') as HTMLCanvasElement, canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
msaa: true, msaa: true,
zPrepass: true,
}) })


async function init() { 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') 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', { const model = await viewer.load<IObject3D>('https://threejs.org/examples/models/gltf/kira.glb', {


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


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


const screenPass = viewer.renderManager.screenPass const screenPass = viewer.renderManager.screenPass


screenPass.overrideReadBuffer = screenPass.overrideReadBuffer?.texture === rt.texture ? null : rt screenPass.overrideReadBuffer = screenPass.overrideReadBuffer?.texture === rt.texture ? null : rt
viewer.setDirty() viewer.setDirty()
}, },
['Toggle Depth Texture']: () => {
const rt = getDepthTexture()
screenPass.overrideReadBuffer = screenPass.overrideReadBuffer?.texture === rt.texture ? null : rt
viewer.setDirty()
},
['Download snapshot']: async(btn: HTMLButtonElement) => { ['Download snapshot']: async(btn: HTMLButtonElement) => {
btn.disabled = true btn.disabled = true
const blob = await viewer.getScreenshotBlob({mimeType: 'image/png'}) const blob = await viewer.getScreenshotBlob({mimeType: 'image/png'})

+ 2
- 2
package-lock.json 查看文件

{ {
"name": "threepipe", "name": "threepipe",
"version": "0.0.21",
"version": "0.0.22",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "threepipe", "name": "threepipe",
"version": "0.0.21",
"version": "0.0.22",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@types/three": "https://github.com/repalash/three-ts-types/releases/download/v0.152.1020/package.tgz", "@types/three": "https://github.com/repalash/three-ts-types/releases/download/v0.152.1020/package.tgz",

+ 1
- 1
package.json 查看文件

{ {
"name": "threepipe", "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.", "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", "main": "dist/index.js",
"module": "dist/index.mjs", "module": "dist/index.mjs",

+ 18
- 2
src/plugins/pipeline/GBufferPlugin.ts 查看文件

BufferGeometry, BufferGeometry,
Camera, Camera,
Color, Color,
DepthTexture,
DoubleSide, DoubleSide,
FloatType,
GLSL1, GLSL1,
GLSL3, GLSL3,
IUniform, IUniform,
UniformsLib, UniformsLib,
UniformsUtils, UniformsUtils,
UnsignedByteType, UnsignedByteType,
UnsignedIntType,
UnsignedShortType,
Vector2, Vector2,
Vector4, Vector4,
WebGLMultipleRenderTargets, WebGLMultipleRenderTargets,
get flagsTexture(): ITexture|undefined { get flagsTexture(): ITexture|undefined {
return this.textures[1] return this.textures[1]
} }

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

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


extraUniforms: { extraUniforms: {
tNormalDepth: ()=>({value: this.normalDepthTexture}), tNormalDepth: ()=>({value: this.normalDepthTexture}),
tGBufferFlags: ()=>({value: this.flagsTexture}), tGBufferFlags: ()=>({value: this.flagsTexture}),
tGBufferDepthTexture: ()=>({value: this.depthTexture}),
}, },
extraDefines: { extraDefines: {
// ['GBUFFER_PACKING']: BasicDepthPacking, // ['GBUFFER_PACKING']: BasicDepthPacking,
['HAS_NORMAL_DEPTH_BUFFER']: ()=>this.normalDepthTexture ? 1 : undefined, ['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, ['GBUFFER_HAS_FLAGS']: ()=>this.flagsTexture ? 1 : undefined,
// ['HAS_FLAGS_BUFFER']: ()=>this.flagsTexture ? 1 : undefined,
['HAS_GBUFFER']: ()=>this.isPrimaryGBuffer && this.normalDepthTexture ? 1 : undefined, ['HAS_GBUFFER']: ()=>this.isPrimaryGBuffer && this.normalDepthTexture ? 1 : undefined,
}, },
priority: 100, priority: 100,
samples: this._viewer.renderManager.composerTarget.samples || 0, samples: this._viewer.renderManager.composerTarget.samples || 0,
type: this.bufferType, type: this.bufferType,
textureCount: useMultiple ? 2 : 1, textureCount: useMultiple ? 2 : 1,
depthTexture: this.renderDepthTexture,
depthTextureType: this.depthTextureType,
// magFilter: NearestFilter, // magFilter: NearestFilter,
// minFilter: NearestFilter, // minFilter: NearestFilter,
// generateMipmaps: false, // generateMipmaps: false,
bufferType: TextureDataType = UnsignedByteType, bufferType: TextureDataType = UnsignedByteType,
isPrimaryGBuffer = true, isPrimaryGBuffer = true,
enabled = 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, // packing: DepthPackingStrategies = BasicDepthPacking,
) { ) {
super() super()

+ 12
- 0
src/plugins/pipeline/shaders/GBufferPlugin.unpack.glsl 查看文件

return ivec4(1); return ivec4(1);
#endif #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 //#if DEPTH_NORMAL_TEXTURE == 1
//uniform sampler2D tNormalDepth; //uniform sampler2D tNormalDepth;
//#else //#else

+ 8
- 0
src/rendering/RenderTarget.ts 查看文件

import { import {
ColorSpace, ColorSpace,
DepthFormat,
DepthStencilFormat,
EventDispatcher, EventDispatcher,
FloatType,
MagnificationTextureFilter, MagnificationTextureFilter,
MinificationTextureFilter, MinificationTextureFilter,
Texture, Texture,
TextureDataType, TextureDataType,
UnsignedInt248Type,
UnsignedIntType,
UnsignedShortType,
Wrapping, Wrapping,
} from 'three' } from 'three'
import {Vector4} from 'three/src/math/Vector4' import {Vector4} from 'three/src/math/Vector4'
format?: number format?: number
depthBuffer?: boolean depthBuffer?: boolean
depthTexture?: boolean depthTexture?: boolean
depthTextureType?: typeof UnsignedShortType | typeof UnsignedInt248Type | typeof UnsignedIntType | typeof FloatType
depthTextureFormat?: typeof DepthFormat | typeof DepthStencilFormat
textureCount?: number textureCount?: number
wrapS?: Wrapping wrapS?: Wrapping
wrapT?: Wrapping wrapT?: Wrapping

+ 6
- 1
src/rendering/RenderTargetManager.ts 查看文件

import { import {
BaseEvent, BaseEvent,
ClampToEdgeWrapping, ClampToEdgeWrapping,
DepthFormat,
DepthTexture, DepthTexture,
EventDispatcher, EventDispatcher,
LinearFilter, LinearFilter,
RGBAFormat, RGBAFormat,
Texture, Texture,
UnsignedByteType, UnsignedByteType,
UnsignedIntType,
Vector2, Vector2,
WebGLCubeRenderTarget, WebGLCubeRenderTarget,
WebGLMultipleRenderTargets, WebGLMultipleRenderTargets,
format = RGBAFormat, format = RGBAFormat,
depthBuffer = true, depthBuffer = true,
depthTexture = false, depthTexture = false,
depthTextureType = UnsignedIntType,
depthTextureFormat = DepthFormat,
size = undefined, size = undefined,
textureCount = 1, textureCount = 1,
...op ...op
size = size || this.renderSize.clone().multiplyScalar(this.renderScale * (sizeMultiplier = sizeMultiplier || 1)) size = size || this.renderSize.clone().multiplyScalar(this.renderScale * (sizeMultiplier = sizeMultiplier || 1))
size.width = Math.floor(size.width) size.width = Math.floor(size.width)
size.height = Math.floor(size.height) 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 ? { const target = this.createTargetCustom<T>(textureCount > 1 ? {
width: size.width, width: size.width,
height: size.height, height: size.height,

+ 1
- 1
src/viewer/version.ts 查看文件

export const VERSION = '0.0.21'
export const VERSION = '0.0.22'

正在加载...
取消
保存