1 line
8.0 KiB
Plaintext
1 line
8.0 KiB
Plaintext
|
|
{"version":3,"file":"SAOShader.cjs","sources":["../../src/shaders/SAOShader.ts"],"sourcesContent":["import { Matrix4, Vector2 } from 'three'\n\nimport type { IUniform, Texture } from 'three'\nimport type { IShader } from './types'\n\nexport type SAOShaderDefines = Record<\n 'DEPTH_PACKING' | 'DIFFUSE_TEXTURE' | 'NORMAL_TEXTURE' | 'NUM_RINGS' | 'NUM_SAMPLES' | 'PERSPECTIVE_CAMERA',\n number\n>\n\nexport type SAOShaderUniforms = {\n bias: IUniform<number>\n cameraFar: IUniform<number>\n cameraInverseProjectionMatrix: IUniform<Matrix4>\n cameraNear: IUniform<number>\n cameraProjectionMatrix: IUniform<Matrix4>\n intensity: IUniform<number>\n kernelRadius: IUniform<number>\n minResolution: IUniform<number>\n randomSeed: IUniform<number>\n scale: IUniform<number>\n size: IUniform<Vector2>\n tDepth: IUniform<Texture | null>\n tDiffuse: IUniform<Texture | null>\n tNormal: IUniform<Texture | null>\n}\n\nexport interface ISAOShader extends IShader<SAOShaderUniforms, SAOShaderDefines> {\n defines: SAOShaderDefines\n needsUpdate?: boolean\n}\n\nexport const SAOShader: ISAOShader = {\n defines: {\n NUM_SAMPLES: 7,\n NUM_RINGS: 4,\n NORMAL_TEXTURE: 0,\n DIFFUSE_TEXTURE: 0,\n DEPTH_PACKING: 1,\n PERSPECTIVE_CAMERA: 1,\n },\n uniforms: {\n tDepth: { value: null },\n tDiffuse: { value: null },\n tNormal: { value: null },\n size: { value: /* @__PURE__ */ new Vector2(512, 512) },\n\n cameraNear: { value: 1 },\n cameraFar: { value: 100 },\n cameraProjectionMatrix: { value: /* @__PURE__ */ new Matrix4() },\n cameraInverseProjectionMatrix: { value: /* @__PURE__ */ new Matrix4() },\n\n scale: { value: 1.0 },\n intensity: { value: 0.1 },\n bias: { value: 0.5 },\n\n minResolution: { value: 0.0 },\n kernelRadius: { value: 100.0 },\n randomSeed: { value: 0.0 },\n },\n vertexShader: /* glsl */ `\n varying vec2 vUv;\n\n void main() {\n \tvUv = uv;\n \tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n }\n `,\n fragmentShader: /* glsl */ `\n #include <common>\n\n varying vec2 vUv;\n\n #if DIFFUSE_TEXTURE == 1\n uniform sampler2D tDiffuse;\n #endif\n\n uniform sampler2D tDepth;\n\n #if NORMAL_TEXTURE == 1\n uniform sampler2D tNormal;\n #endif\n\n uniform float cameraNear;\n uniform float cameraFar;\n uniform mat4 cameraProjectionMatrix;\n uniform mat4 cameraInverseProjectionMatrix;\n\n uniform float scale;\n uniform float intensity;\n uniform float bias;\n uniform float kernelRadius;\n uniform float minResolution;\n uniform vec2 size;\n uniform float randomSeed;\n\n // RGBA depth\n\n #include <packing>\n\n vec4 getDefaultColor( const in vec2 screenPosition ) {\n \t#if DIFFUSE_TEXTURE == 1\n \treturn texture2D( tDiffuse, vUv );\n \t#else\n \treturn vec4( 1.0 );\n \t#endif\n }\n\n float getDepth( const in vec2 screenPosition ) {\n \t#if DEPTH_PACKING == 1\n \treturn unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );\n \t#else\n \treturn texture2D( tDepth, screenPosition ).x;\n \t#endif\n }\n\n float getViewZ( const in float depth ) {\n \t#if PERSPECTIVE_CAMERA == 1\n \treturn perspectiveDepthToViewZ( depth, cameraNear, cameraFar );\n \t#else\n \treturn orthographicDepthToViewZ( depth, cameraNear, cameraFar );\n \t#endif\n }\n\n vec3 getViewPosition( const in vec2 screenPosition, const in float depth, const in float viewZ ) {\n \tfloat clipW = cameraProjectionMatrix[2][3] * viewZ + cameraProjectionMatrix[3][3];\n \tvec4 clipPosition = vec4( ( vec3( screenPosition, depth ) - 0.5 ) * 2.0, 1.0 );\n \tclipPosition *= clipW; // unprojection.\n\n \treturn ( cameraInverseProjectionMatrix * clipPosition ).xyz;\n }\n\n vec3 getViewNormal( const in vec3 viewPosition, const in vec2 screenPosition ) {\n \t#if NORMAL_TEXTURE == 1\n \treturn unpackRGBToNormal( texture2D( tNormal, screenPosition ).xyz );\n \t#else\n \treturn normalize( cross( dFdx( viewPosition ), dFdy( viewPosit
|