├── .gitignore ├── Assets ├── AssetStoreTools.meta ├── AssetStoreTools │ ├── Editor.meta │ ├── Editor │ │ ├── AssetStoreTools.dll │ │ ├── AssetStoreTools.dll.meta │ │ ├── AssetStoreToolsExtra.dll │ │ ├── AssetStoreToolsExtra.dll.meta │ │ ├── DroidSansMono.ttf │ │ ├── DroidSansMono.ttf.meta │ │ ├── icon.png │ │ └── icon.png.meta │ ├── Labels.asset │ └── Labels.asset.meta ├── DepthOfField.meta └── DepthOfField │ ├── Example.meta │ ├── Example │ ├── Backdrop.fbx │ ├── Backdrop.fbx.meta │ ├── Example.meta │ ├── Example.unity │ ├── Example.unity.meta │ ├── Example │ │ ├── LightmapColor-0.exr │ │ ├── LightmapColor-0.exr.meta │ │ ├── LightmapScale-0.exr │ │ └── LightmapScale-0.exr.meta │ ├── Images.meta │ ├── Images │ │ ├── Exclude 1.png │ │ ├── Exclude 1.png.meta │ │ ├── Exclude 2.png │ │ └── Exclude 2.png.meta │ ├── Materials.meta │ ├── Materials │ │ ├── Copper.mat │ │ ├── Copper.mat.meta │ │ ├── Diffuse Excluded.mat │ │ ├── Diffuse Excluded.mat.meta │ │ ├── Mosaic.mat │ │ ├── Mosaic.mat.meta │ │ ├── Spackle.mat │ │ ├── Spackle.mat.meta │ │ ├── Transparency.mat │ │ ├── Transparency.mat.meta │ │ ├── Tree.mat │ │ └── Tree.mat.meta │ ├── Shaders.meta │ ├── Shaders │ │ ├── DiffuseSurfaceExample.shader │ │ ├── DiffuseSurfaceExample.shader.meta │ │ ├── DiffuseSurfaceExcludedExample.shader │ │ ├── DiffuseSurfaceExcludedExample.shader.meta │ │ ├── NormalSurfaceExample.shader │ │ ├── NormalSurfaceExample.shader.meta │ │ ├── SpecularSurfaceExample.shader │ │ ├── SpecularSurfaceExample.shader.meta │ │ ├── TransparencyExample.shader │ │ ├── TransparencyExample.shader.meta │ │ ├── TreeSoftOcclusionLeaves.shader │ │ ├── TreeSoftOcclusionLeaves.shader.meta │ │ ├── UnityBuiltin2xTreeLibrary.cginc │ │ ├── UnityBuiltin2xTreeLibrary.cginc.meta │ │ ├── VertexFragmentExample.shader │ │ └── VertexFragmentExample.shader.meta │ ├── Textures.meta │ └── Textures │ │ ├── PalmBranch.psd │ │ ├── PalmBranch.psd.meta │ │ ├── Transparency.psd │ │ ├── Transparency.psd.meta │ │ ├── brushed_gold.tga │ │ ├── brushed_gold.tga.meta │ │ ├── brushed_gold_normals.png │ │ ├── brushed_gold_normals.png.meta │ │ ├── concrete.tga │ │ ├── concrete.tga.meta │ │ ├── concrete_normals.png │ │ ├── concrete_normals.png.meta │ │ ├── mosaic.tga │ │ ├── mosaic.tga.meta │ │ ├── mosaic_normals.png │ │ └── mosaic_normals.png.meta │ ├── Scripts.meta │ ├── Scripts │ ├── DepthOfField.cs │ └── DepthOfField.cs.meta │ ├── Shaders.meta │ └── Shaders │ ├── DepthCG.cginc │ ├── DepthCG.cginc.meta │ ├── DepthOfField.shader │ └── DepthOfField.shader.meta ├── LICENSE.md ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset ├── UnityAdsSettings.asset └── UnityAnalyticsManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Library/ 3 | Temp/ 4 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6fae6bc2e65c047c188f69b4d52661e2 3 | folderAsset: yes 4 | timeCreated: 1431625562 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 47c3c77b488bde14eac761a5144660ed 3 | folderAsset: yes 4 | timeCreated: 1431625562 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/AssetStoreTools.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/AssetStoreTools/Editor/AssetStoreTools.dll -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/AssetStoreTools.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 11188de2b6632fa4486c470af4b55fa0 3 | PluginImporter: 4 | serializedVersion: 1 5 | iconMap: {} 6 | executionOrder: {} 7 | isPreloaded: 0 8 | platformData: 9 | Any: 10 | enabled: 1 11 | settings: {} 12 | userData: 13 | assetBundleName: 14 | assetBundleVariant: 15 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/AssetStoreToolsExtra.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/AssetStoreTools/Editor/AssetStoreToolsExtra.dll -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/AssetStoreToolsExtra.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e35231d99115e9e4c8cb29414445831f 3 | PluginImporter: 4 | serializedVersion: 1 5 | iconMap: {} 6 | executionOrder: {} 7 | isPreloaded: 0 8 | platformData: 9 | Any: 10 | enabled: 1 11 | settings: {} 12 | userData: 13 | assetBundleName: 14 | assetBundleVariant: 15 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/DroidSansMono.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/AssetStoreTools/Editor/DroidSansMono.ttf -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/DroidSansMono.ttf.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 35021dda9bd03434195c7dcd6ad7618f 3 | TrueTypeFontImporter: 4 | serializedVersion: 2 5 | fontSize: 16 6 | forceTextureCase: -2 7 | characterSpacing: 1 8 | characterPadding: 0 9 | includeFontData: 1 10 | use2xBehaviour: 0 11 | fontNames: [] 12 | customCharacters: 13 | fontRenderingMode: 0 14 | userData: 15 | assetBundleName: 16 | assetBundleVariant: 17 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/AssetStoreTools/Editor/icon.png -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Editor/icon.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 41844c716792706449720732c95b2747 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | cubemapConvolution: 0 24 | cubemapConvolutionSteps: 8 25 | cubemapConvolutionExponent: 1.5 26 | seamlessCubemap: 0 27 | textureFormat: -1 28 | maxTextureSize: 1024 29 | textureSettings: 30 | filterMode: -1 31 | aniso: -1 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | rGBM: 0 37 | compressionQuality: 50 38 | spriteMode: 0 39 | spriteExtrude: 1 40 | spriteMeshType: 1 41 | alignment: 0 42 | spritePivot: {x: .5, y: .5} 43 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 44 | spritePixelsToUnits: 100 45 | alphaIsTransparency: 0 46 | textureType: -1 47 | buildTargetSettings: [] 48 | spriteSheet: 49 | sprites: [] 50 | spritePackingTag: 51 | userData: 52 | assetBundleName: 53 | assetBundleVariant: 54 | -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Labels.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/AssetStoreTools/Labels.asset -------------------------------------------------------------------------------- /Assets/AssetStoreTools/Labels.asset.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a661972d173754003920c9c956567ca0 3 | timeCreated: 1431630846 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/DepthOfField.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b98555cbed67449ce869795d859c4468 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f959ad9499f4d4f4eaffcd770e7faa32 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Backdrop.fbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Backdrop.fbx -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Backdrop.fbx.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ba3e414699a764d3bbbf4c2d9eb946f4 3 | ModelImporter: 4 | serializedVersion: 16 5 | fileIDToRecycleName: 6 | 100000: //RootNode 7 | 100002: bend1Handle 8 | 100004: pPlane1 9 | 400000: //RootNode 10 | 400002: bend1Handle 11 | 400004: pPlane1 12 | 2300000: pPlane1 13 | 2300002: //RootNode 14 | 3300000: pPlane1 15 | 3300002: //RootNode 16 | 4300000: pPlane1 17 | 9500000: //RootNode 18 | materials: 19 | importMaterials: 0 20 | materialName: 0 21 | materialSearch: 1 22 | animations: 23 | legacyGenerateAnimations: 4 24 | bakeSimulation: 0 25 | optimizeGameObjects: 0 26 | animationCompression: 1 27 | animationRotationError: .5 28 | animationPositionError: .5 29 | animationScaleError: .5 30 | animationWrapMode: 0 31 | extraExposedTransformPaths: [] 32 | clipAnimations: [] 33 | isReadable: 1 34 | meshes: 35 | lODScreenPercentages: [] 36 | globalScale: 1 37 | meshCompression: 0 38 | addColliders: 0 39 | importBlendShapes: 1 40 | swapUVChannels: 0 41 | generateSecondaryUV: 1 42 | useFileUnits: 1 43 | optimizeMeshForGPU: 1 44 | weldVertices: 1 45 | secondaryUVAngleDistortion: 8 46 | secondaryUVAreaDistortion: 15.000001 47 | secondaryUVHardAngle: 88 48 | secondaryUVPackMargin: 4 49 | tangentSpace: 50 | normalSmoothAngle: 60 51 | splitTangentsAcrossUV: 1 52 | normalImportMode: 0 53 | tangentImportMode: 1 54 | importAnimation: 1 55 | copyAvatar: 0 56 | humanDescription: 57 | human: [] 58 | skeleton: [] 59 | armTwist: .5 60 | foreArmTwist: .5 61 | upperLegTwist: .5 62 | legTwist: .5 63 | armStretch: .0500000007 64 | legStretch: .0500000007 65 | feetSpacing: 0 66 | rootMotionBoneName: 67 | lastHumanDescriptionAvatarSource: {instanceID: 0} 68 | animationType: 0 69 | additionalBone: 0 70 | userData: 71 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b4821bbcd8b984ccd99b06e99a4ee86b 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Example.unity -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d4be85e733a254433917c2def0b90329 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example/LightmapColor-0.exr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Example/LightmapColor-0.exr -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example/LightmapColor-0.exr.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0c2439678505644b8833d7e49fa13fe8 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 1 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: 1 27 | aniso: 3 28 | mipBias: -1 29 | wrapMode: 1 30 | nPOTScale: 1 31 | lightmap: 1 32 | compressionQuality: 100 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | spriteMeshType: 1 36 | alignment: 0 37 | spritePivot: {x: .5, y: .5} 38 | spritePixelsToUnits: 100 39 | alphaIsTransparency: 0 40 | textureType: 6 41 | buildTargetSettings: [] 42 | spriteSheet: 43 | sprites: [] 44 | spritePackingTag: 45 | userData: 46 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example/LightmapScale-0.exr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Example/LightmapScale-0.exr -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Example/LightmapScale-0.exr.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8affa59385c3c44bb88addfb067a1cde 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 1 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: 1 27 | aniso: 3 28 | mipBias: -1 29 | wrapMode: 1 30 | nPOTScale: 1 31 | lightmap: 1 32 | compressionQuality: 100 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | spriteMeshType: 1 36 | alignment: 0 37 | spritePivot: {x: .5, y: .5} 38 | spritePixelsToUnits: 100 39 | alphaIsTransparency: 0 40 | textureType: 6 41 | buildTargetSettings: [] 42 | spriteSheet: 43 | sprites: [] 44 | spritePackingTag: 45 | userData: 46 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Images.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 361bd73bdead845b5b14a9c2d85eaeb9 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Images/Exclude 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Images/Exclude 1.png -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Images/Exclude 1.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 989fe4e6abf6c4399af3b580621ae715 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -3 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: -1 28 | aniso: -1 29 | mipBias: -1 30 | wrapMode: -1 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: -1 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Images/Exclude 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Images/Exclude 2.png -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Images/Exclude 2.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f6c6b56dda76d423a9efe0eac7c45d20 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -3 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: -1 28 | aniso: -1 29 | mipBias: -1 30 | wrapMode: -1 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: -1 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 961151cfc0d6d4085885432d85aa4992 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Copper.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Materials/Copper.mat -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Copper.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ced56b5ed4dee400ebb8693e057c6cd3 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Diffuse Excluded.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Materials/Diffuse Excluded.mat -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Diffuse Excluded.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 71fa78dcccf6a48f38d15ef8fd0892a7 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Mosaic.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Materials/Mosaic.mat -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Mosaic.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c4ccbc2a81b484d5aa187b9547ce75c7 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Spackle.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Materials/Spackle.mat -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Spackle.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2fadaa67b78bd472592fb1727f136846 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Transparency.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Materials/Transparency.mat -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Transparency.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cb8fc8f872d55493cabf8d5706acc757 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Tree.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Materials/Tree.mat -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Materials/Tree.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3d92e817d33014dc7af3a4e758d82059 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c1fbd76b99f744dc6bdfcc3f3d671264 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/DiffuseSurfaceExample.shader: -------------------------------------------------------------------------------- 1 | Shader "Mobile/Depth of Field/Diffuse (Surface)" { 2 | Properties { 3 | _MainTex ("Base (RGB)", 2D) = "white" {} 4 | } 5 | 6 | SubShader { 7 | Tags { "RenderType"="Opaque" } 8 | 9 | CGPROGRAM 10 | #pragma surface surf Lambert vertex:vert keepalpha 11 | 12 | #include "UnityCG.cginc" 13 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 14 | 15 | sampler2D _MainTex; 16 | 17 | struct Input { 18 | float2 uv_MainTex; 19 | float depth; // Define depth float to pass to `surf` 20 | }; 21 | 22 | // `vert` function to calculate depth 23 | void vert(inout appdata_full v, out Input o) { 24 | // Unity helper (see UnityCG.cginc for definition) 25 | UNITY_INITIALIZE_OUTPUT(Input, o); 26 | 27 | // Calculate depth to pass to `surf` 28 | o.depth = CalculateDepth(v.vertex); 29 | } 30 | 31 | void surf(Input IN, inout SurfaceOutput o) { 32 | o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb; 33 | 34 | // Place `vert` depth calculation into alpha channel 35 | o.Alpha = IN.depth; 36 | } 37 | ENDCG 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/DiffuseSurfaceExample.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2b1ce2b10fd614914a73ffef4c58294f 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/DiffuseSurfaceExcludedExample.shader: -------------------------------------------------------------------------------- 1 | Shader "Mobile/Depth of Field/Diffuse Excluded (Surface)" { 2 | Properties { 3 | _MainTex ("Base (RGB)", 2D) = "white" {} 4 | } 5 | 6 | SubShader { 7 | Tags { "RenderType"="Opaque" } 8 | 9 | CGPROGRAM 10 | #pragma surface surf Lambert keepalpha 11 | 12 | #include "UnityCG.cginc" 13 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 14 | 15 | sampler2D _MainTex; 16 | 17 | struct Input { 18 | float2 uv_MainTex; 19 | float depth; // Define depth float to pass to `surf` 20 | }; 21 | 22 | void surf(Input IN, inout SurfaceOutput o) { 23 | o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb; 24 | 25 | // Place `vert` depth calculation into alpha channel 26 | o.Alpha = -1.0f; 27 | } 28 | ENDCG 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/DiffuseSurfaceExcludedExample.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ffdccfa2a7e2346639f979f3cf179286 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/NormalSurfaceExample.shader: -------------------------------------------------------------------------------- 1 | Shader "Mobile/Depth of Field/Normal (Surface)" { 2 | Properties { 3 | _MainTex ("Base (RGB)", 2D) = "white" {} 4 | _BumpMap ("Bumpmap", 2D) = "bump" {} 5 | } 6 | 7 | SubShader { 8 | Tags { "RenderType"="Opaque" } 9 | 10 | CGPROGRAM 11 | #pragma surface surf Lambert vertex:vert keepalpha 12 | 13 | #include "UnityCG.cginc" 14 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 15 | 16 | sampler2D _MainTex; 17 | sampler2D _BumpMap; 18 | 19 | struct Input { 20 | float2 uv_MainTex; 21 | float2 uv_BumpMap; 22 | float depth; // Define depth float to pass to `surf` 23 | }; 24 | 25 | // `vert` function to calculate depth 26 | void vert(inout appdata_full v, out Input o) { 27 | // Unity helper (see UnityCG.cginc for definition) 28 | UNITY_INITIALIZE_OUTPUT(Input, o); 29 | 30 | // Calculate depth to pass to `surf` 31 | o.depth = CalculateDepth(v.vertex); 32 | } 33 | 34 | void surf(Input IN, inout SurfaceOutput o) { 35 | o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb; 36 | o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); 37 | 38 | // Place `vert` depth calculation into alpha channel 39 | o.Alpha = IN.depth; 40 | } 41 | ENDCG 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/NormalSurfaceExample.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c4089188cf42b49e0a4ee951eaaf1931 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/SpecularSurfaceExample.shader: -------------------------------------------------------------------------------- 1 | Shader "Mobile/Depth of Field/Specular (Surface)" { 2 | Properties { 3 | _MainTex ("Base (RGB)", 2D) = "white" {} 4 | _BumpMap ("Bumpmap", 2D) = "bump" {} 5 | _Shininess ("Shininess", Range (0.03, 1)) = 0.078125 6 | _SpecColor ("Spec Color", Color) = (1,1,1,1) 7 | } 8 | 9 | SubShader { 10 | Tags { "RenderType"="Opaque" } 11 | 12 | CGPROGRAM 13 | #pragma surface surf MobileBlinnPhong vertex:vert exclude_path:prepass noforwardadd halfasview keepalpha 14 | 15 | #include "UnityCG.cginc" 16 | #include "Lighting.cginc" 17 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 18 | 19 | sampler2D _MainTex; 20 | sampler2D _BumpMap; 21 | half _Shininess; 22 | 23 | struct Input { 24 | float2 uv_MainTex; 25 | float2 uv_BumpMap; 26 | float depth; // Define depth float to pass to `surf` 27 | }; 28 | 29 | inline fixed4 LightingMobileBlinnPhong_PrePass(SurfaceOutput s, half4 light) { 30 | fixed spec = light.a * s.Gloss; 31 | 32 | fixed4 c; 33 | c.rgb = (s.Albedo * light.rgb + light.rgb * _SpecColor.rgb * spec); 34 | c.a = s.Alpha; 35 | return c; 36 | } 37 | 38 | inline half4 LightingMobileBlinnPhong_DirLightmap(SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor) { 39 | UNITY_DIRBASIS 40 | half3 scalePerBasisVector; 41 | 42 | half3 lm = DirLightmapDiffuse(unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector); 43 | 44 | half3 lightDir = normalize(scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]); 45 | half3 h = normalize(lightDir + viewDir); 46 | 47 | float nh = max(0, dot (s.Normal, h)); 48 | float spec = pow(nh, s.Specular * 128.0); 49 | 50 | // specColor used outside in the forward path, compiled out in prepass 51 | specColor = lm * _SpecColor.rgb * s.Gloss * spec; 52 | 53 | // spec from the alpha component is used to calculate specular 54 | // in the Lighting*_Prepass function, it's not used in forward 55 | return half4(lm, spec); 56 | } 57 | 58 | inline fixed4 LightingMobileBlinnPhong(SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten) { 59 | fixed diff = max(0, dot(s.Normal, lightDir)); 60 | fixed nh = max(0, dot(s.Normal, halfDir)); 61 | fixed spec = pow(nh, s.Specular*128) * s.Gloss; 62 | 63 | fixed4 c; 64 | c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten*2); 65 | c.a = s.Alpha; 66 | return c; 67 | } 68 | 69 | // `vert` function to calculate depth 70 | void vert(inout appdata_full v, out Input o) { 71 | // Unity helper (see UnityCG.cginc for definition) 72 | UNITY_INITIALIZE_OUTPUT(Input, o); 73 | 74 | // Calculate depth to pass to `surf` 75 | o.depth = CalculateDepth(v.vertex); 76 | } 77 | 78 | void surf(Input IN, inout SurfaceOutput o) { 79 | half4 color = tex2D(_MainTex, IN.uv_MainTex); 80 | o.Albedo = color.rgb; 81 | o.Gloss = color.a; 82 | o.Specular = _Shininess; 83 | o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); 84 | 85 | // Place `vert` depth calculation into alpha channel 86 | o.Alpha = IN.depth; 87 | } 88 | ENDCG 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/SpecularSurfaceExample.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1e6191cc1362d44a7857b26e7a24a800 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/TransparencyExample.shader: -------------------------------------------------------------------------------- 1 | Shader "Mobile/Depth of Field/Transparency (VertFrag)" { 2 | Properties { 3 | _MainTex ("Base (RGBA)", 2D) = "white" {} 4 | } 5 | 6 | SubShader { 7 | // Set the appropriate render tags for Queue and RenderType 8 | Tags { 9 | "Queue" = "Transparent" 10 | "RenderType" = "TransparentCutout" 11 | } 12 | 13 | Pass { 14 | CGPROGRAM 15 | #pragma vertex vert 16 | #pragma fragment frag 17 | 18 | #include "UnityCG.cginc" 19 | #include "Lighting.cginc" 20 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 21 | 22 | struct v2f { 23 | float4 position : SV_POSITION; 24 | float2 uv : TEXCOORD0; 25 | float4 lighting : COLOR0; 26 | float depth : TEXCOORD2; // Define depth float to pass to `frag` 27 | }; 28 | 29 | sampler2D _MainTex; 30 | float4 _MainTex_ST; 31 | 32 | v2f vert(appdata_base i) { 33 | v2f o; 34 | o.position = mul(UNITY_MATRIX_MVP, i.vertex); 35 | o.uv = TRANSFORM_TEX(i.texcoord, _MainTex); 36 | 37 | // Calculate depth and place in output 38 | o.depth = CalculateDepth(i.vertex); 39 | 40 | // Simple Lambert lighting 41 | float3 normalDirection = normalize(float3(mul(float4(i.normal, 0.0), _World2Object))); 42 | float3 lightDirection = normalize(float3(_WorldSpaceLightPos0)); 43 | float3 diffuseReflection = float3(_LightColor0) * (max(0.0, dot(normalDirection, lightDirection)) * 2.0); 44 | o.lighting = float4(diffuseReflection, 1.0) + UNITY_LIGHTMODEL_AMBIENT; 45 | 46 | return o; 47 | } 48 | 49 | half4 frag(v2f i) : COLOR { 50 | half4 main_color = tex2D(_MainTex, i.uv) * i.lighting; 51 | 52 | // The magic 53 | clip(main_color.a - 0.5f); 54 | 55 | // Place `vert` depth calculation into alpha channel 56 | main_color.a = i.depth; 57 | return main_color; 58 | } 59 | ENDCG 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/TransparencyExample.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b099b328835b946d99e9f8e2037e1ab0 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/TreeSoftOcclusionLeaves.shader: -------------------------------------------------------------------------------- 1 | Shader "Nature/Tree Soft Occlusion Leaves (DoF)" { 2 | Properties { 3 | _Color ("Main Color", Color) = (1,1,1,1) 4 | _MainTex ("Main Texture", 2D) = "white" { } 5 | _Cutoff ("Alpha cutoff", Range(0.25,0.9)) = 0.5 6 | _BaseLight ("Base Light", Range(0, 1)) = 0.35 7 | _AO ("Amb. Occlusion", Range(0, 10)) = 2.4 8 | _Occlusion ("Dir Occlusion", Range(0, 20)) = 7.5 9 | 10 | // These are here only to provide default values 11 | [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1) 12 | [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1) 13 | [HideInInspector] _SquashAmount ("Squash", Float) = 1 14 | } 15 | 16 | SubShader { 17 | Tags { 18 | "Queue" = "Transparent-99" 19 | "IgnoreProjector"="True" 20 | "RenderType" = "TreeTransparentCutout" 21 | "DisableBatching"="True" 22 | } 23 | Cull Off 24 | 25 | // Make sure to disable the ColorMask as it excludes the alpha channel 26 | // ColorMask RGB 27 | 28 | Pass { 29 | Lighting On 30 | 31 | CGPROGRAM 32 | #pragma vertex leaves 33 | #pragma fragment frag 34 | #pragma multi_compile_fog 35 | #include "UnityBuiltin2xTreeLibrary.cginc" 36 | 37 | sampler2D _MainTex; 38 | fixed _Cutoff; 39 | 40 | fixed4 frag(v2f input) : SV_Target 41 | { 42 | fixed4 c = tex2D( _MainTex, input.uv.xy); 43 | c.rgb *= input.color.rgb; 44 | 45 | clip (c.a - _Cutoff); 46 | UNITY_APPLY_FOG(input.fogCoord, c); 47 | 48 | // Override alpha, _after_ `clip` 49 | c.a = input.depth; 50 | return c; 51 | } 52 | ENDCG 53 | } 54 | 55 | Pass { 56 | Name "ShadowCaster" 57 | Tags { "LightMode" = "ShadowCaster" } 58 | 59 | CGPROGRAM 60 | #pragma vertex vert 61 | #pragma fragment frag 62 | #pragma multi_compile_shadowcaster 63 | #include "UnityCG.cginc" 64 | #include "TerrainEngine.cginc" 65 | 66 | struct v2f { 67 | V2F_SHADOW_CASTER; 68 | float2 uv : TEXCOORD1; 69 | }; 70 | 71 | struct appdata { 72 | float4 vertex : POSITION; 73 | float3 normal : NORMAL; 74 | fixed4 color : COLOR; 75 | float4 texcoord : TEXCOORD0; 76 | }; 77 | v2f vert( appdata v ) 78 | { 79 | v2f o; 80 | TerrainAnimateTree(v.vertex, v.color.w); 81 | TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) 82 | o.uv = v.texcoord; 83 | return o; 84 | } 85 | 86 | sampler2D _MainTex; 87 | fixed _Cutoff; 88 | 89 | float4 frag( v2f i ) : SV_Target 90 | { 91 | fixed4 texcol = tex2D( _MainTex, i.uv ); 92 | clip( texcol.a - _Cutoff ); 93 | SHADOW_CASTER_FRAGMENT(i) 94 | } 95 | ENDCG 96 | } 97 | } 98 | 99 | // This subshader is never actually used, but is only kept so 100 | // that the tree mesh still assumes that normals are needed 101 | // at build time (due to Lighting On in the pass). The subshader 102 | // above does not actually use normals, so they are stripped out. 103 | // We want to keep normals for backwards compatibility with Unity 4.2 104 | // and earlier. 105 | SubShader { 106 | Tags { 107 | "Queue" = "Transparent-99" 108 | "IgnoreProjector"="True" 109 | "RenderType" = "TransparentCutout" 110 | } 111 | Cull Off 112 | ColorMask RGB 113 | Pass { 114 | Tags { "LightMode" = "Vertex" } 115 | AlphaTest GEqual [_Cutoff] 116 | Lighting On 117 | Material { 118 | Diffuse [_Color] 119 | Ambient [_Color] 120 | } 121 | SetTexture [_MainTex] { combine primary * texture DOUBLE, texture } 122 | } 123 | } 124 | 125 | Dependency "BillboardShader" = "Hidden/Nature/Tree Soft Occlusion Leaves Rendertex" 126 | Fallback Off 127 | } 128 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/TreeSoftOcclusionLeaves.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 96790f3284f384106aee2c531156976d 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/UnityBuiltin2xTreeLibrary.cginc: -------------------------------------------------------------------------------- 1 | // Shared tree shader functionality for Unity 2.x tree shaders 2 | 3 | #include "HLSLSupport.cginc" 4 | #include "UnityCG.cginc" 5 | #include "TerrainEngine.cginc" 6 | 7 | // Add import for DoF helper here 8 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 9 | 10 | float _Occlusion, _AO, _BaseLight; 11 | fixed4 _Color; 12 | 13 | #ifdef USE_CUSTOM_LIGHT_DIR 14 | CBUFFER_START(UnityTerrainImposter) 15 | float3 _TerrainTreeLightDirections[4]; 16 | float4 _TerrainTreeLightColors[4]; 17 | CBUFFER_END 18 | #endif 19 | 20 | CBUFFER_START(UnityPerCamera2) 21 | float4x4 _CameraToWorld; 22 | CBUFFER_END 23 | 24 | float _HalfOverCutoff; 25 | 26 | struct v2f { 27 | float4 pos : SV_POSITION; 28 | float4 uv : TEXCOORD0; 29 | half4 color : TEXCOORD1; 30 | float depth : TEXCOORD2; // Define depth float to pass to `frag` 31 | UNITY_FOG_COORDS(2) 32 | }; 33 | 34 | v2f leaves(appdata_tree v) 35 | { 36 | v2f o; 37 | 38 | TerrainAnimateTree(v.vertex, v.color.w); 39 | 40 | float3 viewpos = mul(UNITY_MATRIX_MV, v.vertex); 41 | o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 42 | o.uv = v.texcoord; 43 | 44 | float4 lightDir = 0; 45 | float4 lightColor = 0; 46 | lightDir.w = _AO; 47 | 48 | float4 light = UNITY_LIGHTMODEL_AMBIENT; 49 | 50 | for (int i = 0; i < 4; i++) { 51 | float atten = 1.0; 52 | #ifdef USE_CUSTOM_LIGHT_DIR 53 | lightDir.xyz = _TerrainTreeLightDirections[i]; 54 | lightColor = _TerrainTreeLightColors[i]; 55 | #else 56 | float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w; 57 | toLight.z *= -1.0; 58 | lightDir.xyz = mul( (float3x3)_CameraToWorld, normalize(toLight) ); 59 | float lengthSq = dot(toLight, toLight); 60 | atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z); 61 | 62 | lightColor.rgb = unity_LightColor[i].rgb; 63 | #endif 64 | 65 | lightDir.xyz *= _Occlusion; 66 | float occ = dot (v.tangent, lightDir); 67 | occ = max(0, occ); 68 | occ += _BaseLight; 69 | light += lightColor * (occ * atten); 70 | } 71 | 72 | o.color = light * _Color * _TreeInstanceColor; 73 | o.color.a = 0.5 * _HalfOverCutoff; 74 | 75 | UNITY_TRANSFER_FOG(o,o.pos); 76 | 77 | // Calculate depth and place in output to pass to frag 78 | o.depth = CalculateDepth(v.vertex); 79 | return o; 80 | } 81 | 82 | v2f bark(appdata_tree v) 83 | { 84 | v2f o; 85 | 86 | TerrainAnimateTree(v.vertex, v.color.w); 87 | 88 | float3 viewpos = mul(UNITY_MATRIX_MV, v.vertex); 89 | o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 90 | o.uv = v.texcoord; 91 | 92 | float4 lightDir = 0; 93 | float4 lightColor = 0; 94 | lightDir.w = _AO; 95 | 96 | float4 light = UNITY_LIGHTMODEL_AMBIENT; 97 | 98 | for (int i = 0; i < 4; i++) { 99 | float atten = 1.0; 100 | #ifdef USE_CUSTOM_LIGHT_DIR 101 | lightDir.xyz = _TerrainTreeLightDirections[i]; 102 | lightColor = _TerrainTreeLightColors[i]; 103 | #else 104 | float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w; 105 | toLight.z *= -1.0; 106 | lightDir.xyz = mul( (float3x3)_CameraToWorld, normalize(toLight) ); 107 | float lengthSq = dot(toLight, toLight); 108 | atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z); 109 | 110 | lightColor.rgb = unity_LightColor[i].rgb; 111 | #endif 112 | 113 | 114 | float diffuse = dot (v.normal, lightDir.xyz); 115 | diffuse = max(0, diffuse); 116 | diffuse *= _AO * v.tangent.w + _BaseLight; 117 | light += lightColor * (diffuse * atten); 118 | } 119 | 120 | light.a = 1; 121 | o.color = light * _Color * _TreeInstanceColor; 122 | 123 | #ifdef WRITE_ALPHA_1 124 | o.color.a = 1; 125 | #endif 126 | 127 | UNITY_TRANSFER_FOG(o,o.pos); 128 | return o; 129 | } 130 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/UnityBuiltin2xTreeLibrary.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: df8a857e91cc5457197612072d4bb68c 3 | timeCreated: 1431624617 4 | licenseType: Pro 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/VertexFragmentExample.shader: -------------------------------------------------------------------------------- 1 | Shader "Mobile/Depth of Field/Diffuse (VertFrag)" { 2 | Properties { 3 | _MainTex ("Base (RGB)", 2D) = "white" {} 4 | } 5 | 6 | SubShader { 7 | Tags { "RenderType"="Opaque" } 8 | 9 | Pass { 10 | CGPROGRAM 11 | #pragma vertex vert 12 | #pragma fragment frag 13 | 14 | #include "UnityCG.cginc" 15 | #include "Lighting.cginc" 16 | #include "Assets/DepthOfField/Shaders/DepthCG.cginc" 17 | 18 | struct v2f { 19 | float4 position : SV_POSITION; 20 | float2 uv : TEXCOORD0; 21 | float4 lighting : COLOR0; 22 | float depth : TEXCOORD2; // Define depth float to pass to `frag` 23 | }; 24 | 25 | sampler2D _MainTex; 26 | float4 _MainTex_ST; 27 | 28 | v2f vert(appdata_base i) { 29 | v2f o; 30 | o.position = mul(UNITY_MATRIX_MVP, i.vertex); 31 | o.uv = TRANSFORM_TEX(i.texcoord, _MainTex); 32 | 33 | // Calculate depth and place in output 34 | o.depth = CalculateDepth(i.vertex); 35 | 36 | // Simple Lambert lighting 37 | float3 normalDirection = normalize(float3(mul(float4(i.normal, 0.0), _World2Object))); 38 | float3 lightDirection = normalize(float3(_WorldSpaceLightPos0)); 39 | float3 diffuseReflection = float3(_LightColor0) * (max(0.0, dot(normalDirection, lightDirection)) * 2.0); 40 | o.lighting = float4(diffuseReflection, 1.0) + UNITY_LIGHTMODEL_AMBIENT; 41 | 42 | return o; 43 | } 44 | 45 | half4 frag(v2f i) : COLOR { 46 | half4 main_color = tex2D(_MainTex, i.uv) * i.lighting; 47 | 48 | // Place `vert` depth calculation into alpha channel 49 | main_color.a = i.depth; 50 | return main_color; 51 | } 52 | ENDCG 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Shaders/VertexFragmentExample.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ea8d446aafc074f3a885ac7e1d2a0c51 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 36cfd80d41f5e44f9b449136ca9b9a3b 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/PalmBranch.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/PalmBranch.psd -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/PalmBranch.psd.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 523aff9770d4f4c40b2a78f891bc0a4a 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 2 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -1 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: 1 28 | aniso: 1 29 | mipBias: 0 30 | wrapMode: 0 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: 0 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/Transparency.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/Transparency.psd -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/Transparency.psd.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c209921d00c38492c9922f1d12fffdc7 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 1 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -1 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: -1 28 | aniso: -1 29 | mipBias: -1 30 | wrapMode: 1 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: -1 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/brushed_gold.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/brushed_gold.tga -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/brushed_gold.tga.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fbc076ea5c40a443f93686e61bc690a5 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -1 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: -1 28 | aniso: -1 29 | mipBias: -1 30 | wrapMode: -1 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: -1 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/brushed_gold_normals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/brushed_gold_normals.png -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/brushed_gold_normals.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d1c99daf62b5542f0a506b9820406741 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 1 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 1 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | spriteMeshType: 1 36 | alignment: 0 37 | spritePivot: {x: .5, y: .5} 38 | spritePixelsToUnits: 100 39 | alphaIsTransparency: 0 40 | textureType: 1 41 | buildTargetSettings: [] 42 | spriteSheet: 43 | sprites: [] 44 | spritePackingTag: 45 | userData: 46 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/concrete.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/concrete.tga -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/concrete.tga.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2ef8916277b514bf189ec38c59c64d64 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -1 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: -1 28 | aniso: -1 29 | mipBias: -1 30 | wrapMode: -1 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: -1 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/concrete_normals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/concrete_normals.png -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/concrete_normals.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d24e912dce4124fb788830b02d28459d 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 1 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 1 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | spriteMeshType: 1 36 | alignment: 0 37 | spritePivot: {x: .5, y: .5} 38 | spritePixelsToUnits: 100 39 | alphaIsTransparency: 0 40 | textureType: 1 41 | buildTargetSettings: [] 42 | spriteSheet: 43 | sprites: [] 44 | spritePackingTag: 45 | userData: 46 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/mosaic.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/mosaic.tga -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/mosaic.tga.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 00a42b7da928e4fa3903829a7d45782f 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: .25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | seamlessCubemap: 0 24 | textureFormat: -1 25 | maxTextureSize: 1024 26 | textureSettings: 27 | filterMode: -1 28 | aniso: -1 29 | mipBias: -1 30 | wrapMode: -1 31 | nPOTScale: 1 32 | lightmap: 0 33 | compressionQuality: 50 34 | spriteMode: 0 35 | spriteExtrude: 1 36 | spriteMeshType: 1 37 | alignment: 0 38 | spritePivot: {x: .5, y: .5} 39 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 40 | spritePixelsToUnits: 100 41 | alphaIsTransparency: 0 42 | textureType: -1 43 | buildTargetSettings: [] 44 | spriteSheet: 45 | sprites: [] 46 | spritePackingTag: 47 | userData: 48 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/mosaic_normals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/Assets/DepthOfField/Example/Textures/mosaic_normals.png -------------------------------------------------------------------------------- /Assets/DepthOfField/Example/Textures/mosaic_normals.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a7a6710c6fac24197a015c603f6d5368 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 1 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 1 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | spriteMeshType: 1 36 | alignment: 0 37 | spritePivot: {x: .5, y: .5} 38 | spritePixelsToUnits: 100 39 | alphaIsTransparency: 0 40 | textureType: 1 41 | buildTargetSettings: [] 42 | spriteSheet: 43 | sprites: [] 44 | spritePackingTag: 45 | userData: 46 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f39899f1fa7694536810aeef449c93cb 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Scripts/DepthOfField.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [ExecuteInEditMode] 5 | [RequireComponent(typeof(Camera))] 6 | public class DepthOfField : MonoBehaviour { 7 | 8 | public Transform focus; 9 | public float focalDistance = 10.0f; 10 | public float aperture = 3; 11 | public bool debug = false; 12 | 13 | [Range(2, 8)] 14 | public int downsampleFactor = 4; 15 | 16 | [HideInInspector] 17 | public Shader shader; 18 | 19 | [HideInInspector] 20 | public Material material; 21 | 22 | private RenderTexture GetTemporaryTexture(int width, int height) { 23 | RenderTexture temporaryTexture = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32); 24 | temporaryTexture.wrapMode = TextureWrapMode.Clamp; 25 | // temporaryTexture.useMipMap = false; 26 | temporaryTexture.isPowerOfTwo = true; 27 | temporaryTexture.filterMode = FilterMode.Bilinear; 28 | return temporaryTexture; 29 | } 30 | 31 | void Awake() { 32 | GetComponent().depthTextureMode = DepthTextureMode.None; // Explicitly disable depthmap 33 | } 34 | 35 | void OnRenderImage(RenderTexture src, RenderTexture dest) { 36 | if (shader == null) { 37 | shader = Shader.Find("Post Effects/Depth of Field (Mobile)"); 38 | } 39 | 40 | if (material == null) { 41 | material = new Material(shader); 42 | } 43 | 44 | // Set depth of field variables 45 | if (focus != null) { 46 | Shader.SetGlobalFloat("_DepthFar", Vector3.Distance(transform.position, focus.position)); 47 | } else { 48 | Shader.SetGlobalFloat("_DepthFar", focalDistance); 49 | } 50 | Shader.SetGlobalFloat("_DepthAperture", aperture); 51 | 52 | if (debug) { 53 | Graphics.Blit(src, dest, material, 5); // Render depth from alpha 54 | return; 55 | } 56 | 57 | int scale = Screen.dpi >= 220 ? 2 : 1; // Multiply downsampleFactor by scale to compensate for retina 58 | 59 | int temporaryWidth = Mathf.NextPowerOfTwo(Screen.width / (downsampleFactor * scale)); 60 | int temporaryHeight = Mathf.NextPowerOfTwo(Screen.height / (downsampleFactor * scale)); 61 | if (temporaryWidth > temporaryHeight) { 62 | temporaryHeight = temporaryWidth; 63 | } else { 64 | temporaryWidth = temporaryHeight; 65 | } 66 | 67 | // Create temporary textures 68 | var grabTextureA = GetTemporaryTexture(temporaryWidth, temporaryHeight); 69 | var grabTextureB = GetTemporaryTexture(temporaryWidth / 2, temporaryHeight / 2); 70 | var grabTextureC = GetTemporaryTexture(temporaryWidth / 4, temporaryHeight / 4); 71 | var grabTextureD = GetTemporaryTexture(temporaryWidth / 2, temporaryHeight / 2); 72 | 73 | // Pass in textures 74 | material.SetTexture("_GrabTextureB", grabTextureB); 75 | material.SetTexture("_GrabTextureC", grabTextureC); 76 | material.SetTexture("_GrabTextureD", grabTextureD); 77 | 78 | Graphics.Blit(src, grabTextureA, material, 0); // Downsample 1 79 | Graphics.Blit(grabTextureA, grabTextureB, material, 1); // Downsample 2 80 | Graphics.Blit(grabTextureB, grabTextureC, material, 1); // Upsample 81 | Graphics.Blit(null, grabTextureD, material, 2); // Blend midground and background 82 | Graphics.Blit(src, dest, material, 3); // Blend foreground and background 83 | 84 | // Release textures 85 | RenderTexture.ReleaseTemporary(grabTextureA); 86 | RenderTexture.ReleaseTemporary(grabTextureB); 87 | RenderTexture.ReleaseTemporary(grabTextureC); 88 | RenderTexture.ReleaseTemporary(grabTextureD); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Scripts/DepthOfField.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 47057c447da1b4d52af473afc66ce2d4 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ea159044e6508470e825fe7239b595f9 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Shaders/DepthCG.cginc: -------------------------------------------------------------------------------- 1 | #ifndef DEPTH_INCLUDED 2 | #define DEPTH_INCLUDED 3 | 4 | #include "UnityCG.cginc" 5 | 6 | uniform half _DepthFar; 7 | uniform half _DepthAperture; 8 | 9 | inline float CalculateDepth(float4 vertex) { 10 | float z = mul(UNITY_MATRIX_MV, vertex).z; 11 | z = clamp(-z / _DepthFar, 0, 2); 12 | z = (1 - z) * _DepthAperture; 13 | return abs(z); 14 | } 15 | #endif -------------------------------------------------------------------------------- /Assets/DepthOfField/Shaders/DepthCG.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 77287651e3c624385bc4b85cb360934f 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Shaders/DepthOfField.shader: -------------------------------------------------------------------------------- 1 | Shader "Post Effects/Depth of Field (Mobile)" { 2 | Properties { 3 | _MainTex ("", 2D) = "white" {} 4 | _GrabTextureB ("", 2D) = "white" {} 5 | _GrabTextureC ("", 2D) = "white" {} 6 | _GrabTextureD ("", 2D) = "white" {} 7 | } 8 | 9 | SubShader { 10 | Pass { // Pass 0 - Blur (small kernel) 11 | ZTest Always Cull Off ZWrite Off 12 | Fog { Mode off } 13 | Blend Off 14 | 15 | CGPROGRAM 16 | #pragma vertex vert 17 | #pragma fragment frag 18 | #pragma fragmentoption ARB_precision_hint_fastest 19 | 20 | #include "UnityCG.cginc" 21 | 22 | uniform sampler2D _MainTex; 23 | uniform half4 _MainTex_TexelSize; 24 | 25 | struct v2f { 26 | half4 pos : SV_POSITION; 27 | half2 uv : TEXCOORD0; 28 | half2 uv2[4] : TEXCOORD1; 29 | }; 30 | 31 | v2f vert(appdata_img v) { 32 | v2f o; 33 | o.pos = mul (UNITY_MATRIX_MVP, v.vertex); 34 | o.uv = v.texcoord; 35 | 36 | #if UNITY_UV_STARTS_AT_TOP 37 | if (_MainTex_TexelSize.y < 0) { 38 | o.uv.y = 1 - o.uv.y; 39 | } 40 | #endif 41 | 42 | o.uv2[0] = o.uv + _MainTex_TexelSize.xy * half2(+1.5, +1.5); 43 | o.uv2[1] = o.uv + _MainTex_TexelSize.xy * half2(-1.5, -1.5); 44 | o.uv2[2] = o.uv + _MainTex_TexelSize.xy * half2(+1.5, -1.5); 45 | o.uv2[3] = o.uv + _MainTex_TexelSize.xy * half2(-1.5, +1.5); 46 | return o; 47 | } 48 | 49 | half4 frag(v2f i) : COLOR { 50 | half4 color = tex2D(_MainTex, i.uv) * 0.4; 51 | color += tex2D(_MainTex, i.uv2[0]) * 0.15; 52 | color += tex2D(_MainTex, i.uv2[1]) * 0.15; 53 | color += tex2D(_MainTex, i.uv2[2]) * 0.15; 54 | color += tex2D(_MainTex, i.uv2[3]) * 0.15; 55 | return color; 56 | } 57 | ENDCG 58 | } 59 | 60 | Pass { // Pass 1 - Blur (large kernel) 61 | ZTest Always Cull Off ZWrite Off 62 | Fog { Mode off } 63 | Blend Off 64 | 65 | CGPROGRAM 66 | #pragma vertex vert 67 | #pragma fragment frag 68 | #pragma fragmentoption ARB_precision_hint_fastest 69 | #pragma exclude_renderers flash 70 | 71 | #include "UnityCG.cginc" 72 | 73 | struct v2f { 74 | half4 pos : POSITION; 75 | half2 uv : TEXCOORD0; 76 | half4 uv01 : TEXCOORD1; 77 | half4 uv23 : TEXCOORD2; 78 | half4 uv45 : TEXCOORD3; 79 | half4 uv67 : TEXCOORD4; 80 | }; 81 | 82 | uniform sampler2D _MainTex; 83 | uniform half4 _MainTex_TexelSize; 84 | 85 | v2f vert(appdata_img v) { 86 | v2f o; 87 | o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 88 | o.uv = v.texcoord; 89 | 90 | #if UNITY_UV_STARTS_AT_TOP 91 | if (_MainTex_TexelSize.y < 0) { 92 | o.uv.y = 1 - o.uv.y; 93 | } 94 | #endif 95 | 96 | o.uv01 = o.uv.xyxy + _MainTex_TexelSize.xyxy * half4(1.5, +1.5, -1.5, -1.5); 97 | o.uv23 = o.uv.xyxy + _MainTex_TexelSize.xyxy * half4(1.5, -1.5, -1.5, +1.5); 98 | o.uv45 = o.uv.xyxy + _MainTex_TexelSize.xyxy * half4(0.0, +2.5, -0.0, -2.5); 99 | o.uv67 = o.uv.xyxy + _MainTex_TexelSize.xyxy * half4(2.5, -0.0, -2.5, +0.0); 100 | return o; 101 | } 102 | 103 | half4 frag(v2f i) : COLOR { 104 | half4 color = 0.25 * tex2D(_MainTex, i.uv); 105 | color += 0.12 * tex2D(_MainTex, i.uv01.xy); 106 | color += 0.12 * tex2D(_MainTex, i.uv01.zw); 107 | color += 0.12 * tex2D(_MainTex, i.uv23.xy); 108 | color += 0.12 * tex2D(_MainTex, i.uv23.zw); 109 | color += 0.0675 * tex2D(_MainTex, i.uv45.xy); 110 | color += 0.0675 * tex2D(_MainTex, i.uv45.zw); 111 | color += 0.0675 * tex2D(_MainTex, i.uv67.xy); 112 | color += 0.0675 * tex2D(_MainTex, i.uv67.zw); 113 | return color; 114 | } 115 | ENDCG 116 | } 117 | 118 | Pass { // Pass 2 - Blend between midground and background 119 | ZTest Always Cull Off ZWrite Off 120 | Fog { Mode off } 121 | Blend Off 122 | CGPROGRAM 123 | #pragma vertex vert_img 124 | #pragma fragment frag 125 | #pragma fragmentoption ARB_precision_hint_fastest 126 | 127 | #include "UnityCG.cginc" 128 | 129 | uniform sampler2D _GrabTextureB; 130 | uniform sampler2D _GrabTextureC; 131 | 132 | half4 frag(v2f_img i) : COLOR { 133 | half4 blurA = tex2D(_GrabTextureB, i.uv); 134 | half4 blurB = tex2D(_GrabTextureC, i.uv); 135 | return lerp(blurA, blurB, min(blurB.a, blurA.a)); 136 | } 137 | ENDCG 138 | } 139 | 140 | Pass { // Pass 3 - blend between focused and blurred 141 | ZTest Always Cull Off ZWrite Off 142 | Fog { Mode off } 143 | Blend Off 144 | 145 | CGPROGRAM 146 | #pragma vertex vert_img 147 | #pragma fragment frag 148 | #pragma fragmentoption ARB_precision_hint_fastest 149 | 150 | #include "UnityCG.cginc" 151 | 152 | uniform sampler2D _MainTex; 153 | uniform sampler2D _GrabTextureD; 154 | 155 | half4 frag(v2f_img i) : COLOR { 156 | half4 colorA = tex2D(_GrabTextureD, i.uv); 157 | half4 colorB = tex2D(_MainTex, i.uv); 158 | return lerp(colorB, colorA, colorB.a); 159 | } 160 | ENDCG 161 | } 162 | 163 | Pass { // Pass 4 164 | ZTest Always Cull Off ZWrite Off 165 | Fog { Mode off } 166 | Blend Off 167 | 168 | CGPROGRAM 169 | #pragma vertex vert 170 | #pragma fragment frag 171 | #pragma fragmentoption ARB_precision_hint_fastest 172 | 173 | #include "UnityCG.cginc" 174 | 175 | uniform sampler2D _MainTex; 176 | uniform half4 _MainTex_TexelSize; 177 | 178 | struct v2f { 179 | half4 pos : SV_POSITION; 180 | half2 uv : TEXCOORD0; 181 | half2 uv2[4] : TEXCOORD1; 182 | }; 183 | 184 | v2f vert(appdata_img v) { 185 | v2f o; 186 | o.pos = mul (UNITY_MATRIX_MVP, v.vertex); 187 | o.uv = v.texcoord; 188 | 189 | #if UNITY_UV_STARTS_AT_TOP 190 | if (_MainTex_TexelSize.y < 0) { 191 | o.uv.y = 1 - o.uv.y; 192 | } 193 | #endif 194 | 195 | o.uv2[0] = o.uv + _MainTex_TexelSize.xy * half2(+2.5, +2.5); 196 | o.uv2[1] = o.uv + _MainTex_TexelSize.xy * half2(-2.5, -2.5); 197 | o.uv2[2] = o.uv + _MainTex_TexelSize.xy * half2(+2.5, -2.5); 198 | o.uv2[3] = o.uv + _MainTex_TexelSize.xy * half2(-2.5, +2.5); 199 | return o; 200 | } 201 | 202 | half4 frag(v2f i) : COLOR { 203 | half4 sample = tex2D(_MainTex, i.uv) * 0.3; 204 | sample += tex2D(_MainTex, i.uv2[0]) * 0.175; 205 | sample += tex2D(_MainTex, i.uv2[1]) * 0.175; 206 | sample += tex2D(_MainTex, i.uv2[2]) * 0.175; 207 | sample += tex2D(_MainTex, i.uv2[3]) * 0.175; 208 | return sample; 209 | } 210 | ENDCG 211 | } 212 | 213 | Pass { // Pass 5 - render depth from alpha 214 | ZTest Always Cull Off ZWrite Off 215 | Fog { Mode off } 216 | Blend Off 217 | 218 | CGPROGRAM 219 | #pragma vertex vert_img 220 | #pragma fragment frag 221 | #pragma fragmentoption ARB_precision_hint_fastest 222 | 223 | #include "UnityCG.cginc" 224 | 225 | uniform sampler2D _MainTex; 226 | 227 | half4 frag(v2f_img i) : COLOR { 228 | half4 c = tex2D(_MainTex, i.uv); 229 | return half4(c.a, c.a, c.a, c.a); 230 | } 231 | ENDCG 232 | } 233 | } 234 | 235 | Fallback off 236 | } 237 | -------------------------------------------------------------------------------- /Assets/DepthOfField/Shaders/DepthOfField.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 68a3028b4ad6f4823ae93bfdd34ed471 3 | ShaderImporter: 4 | defaultTextures: [] 5 | userData: 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Barking Mouse Studio, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.2.0f3 2 | m_StandardAssetsVersion: 0 3 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAdsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/UnityAdsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAnalyticsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BarkingMouseStudio/depth-of-field/3a936d141ddbd5b69b3cffe5d109cdbd6f94ddc9/ProjectSettings/UnityAnalyticsManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Documentation 2 | --- 3 | 4 | ### Setting up the camera 5 | 6 | 1. Add DepthOfField.cs to the camera you wish to apply depth of field. While you can adjust parameters at any time we recommend finishing the setup so you can preview your changes. 7 | 8 | 2. Make sure you set your camera's clear flag to something other than "Depth". 9 | 10 | ### Modifying your shaders 11 | 12 | When in doubt we provide a number of example shaders in DepthOfField/Example/Shaders/ that demonstrates a fully working surface and vertex/fragment shaders. 13 | 14 | > NOTE: ShaderLab syntax is not supported since they do not support vertex functions. 15 | 16 | #### Surface Shaders 17 | 18 | 1. Add the DepthCG.cginc include just after your surface pragma. This will import the necessary helper function for calculating distance from the camera. If you already have includes, DepthCG should be first. 19 | 20 | ```glsl 21 | #pragma surface surf Lambert vertex:vert 22 | 23 | #include "DepthCG.cginc" // <= HERE 24 | ``` 25 | 26 | 2. Define a new float inside your `Input`. This will be passed from `vert` to `surf`. 27 | 28 | ```glsl 29 | struct Input { 30 | ... 31 | 32 | float depth; // <= HERE 33 | }; 34 | ``` 35 | 36 | 3. Define a `vert` function like so (skip to step 4 if you already have one): 37 | 38 | ```glsl 39 | void vert(inout appdata_full v, out Input o) { 40 | UNITY_INITIALIZE_OUTPUT(Input, o); // Unity helper (see UnityCG.cginc for definition) 41 | } 42 | ``` 43 | 44 | 4. Use the included helper to calculate the depth of the current vertex and add it to the output: 45 | 46 | ```glsl 47 | void vert(inout appdata_full v, out Input o) { 48 | ... 49 | 50 | o.depth = CalculateDepth(v.vertex); // <= HERE 51 | } 52 | ``` 53 | 54 | 5. Place `IN.depth` (calculated in `vert`) inside your SurfaceOutput's `Alpha`: 55 | 56 | ```glsl 57 | void surf(Input IN, inout SurfaceOutput o) { 58 | ... 59 | 60 | o.Alpha = IN.depth; // <= HERE 61 | } 62 | ``` 63 | 64 | #### Vertex Shaders 65 | 66 | 1. Add the DepthCG.cginc include just after your vert/frag pragmas. This will import the necessary helper function for calculating distance from the camera. If you already have includes, DepthCG should be first. 67 | 68 | ```glsl 69 | #pragma vertex vert 70 | #pragma fragment frag 71 | 72 | #include "DepthCG.cginc" // <= HERE 73 | ``` 74 | 75 | 2. Define depth float to pass to `frag`: 76 | 77 | ```glsl 78 | struct v2f { 79 | ... 80 | 81 | float depth : TEXCOORD2; // <= HERE 82 | }; 83 | ``` 84 | 85 | 3. In `vert`, calculate depth and place it in `v2f`: 86 | 87 | ```glsl 88 | v2f vert(appdata_base i) { 89 | v2f o; 90 | 91 | ... 92 | 93 | o.depth = CalculateDepth(i.vertex); // <= HERE 94 | 95 | ... 96 | 97 | return o; 98 | } 99 | ``` 100 | 101 | 4. In `frag`, place the depth calculation from `v2f` into the output color's alpha channel: 102 | 103 | ```glsl 104 | half4 frag(v2f i) : COLOR { 105 | half4 main_color; 106 | 107 | ... 108 | 109 | main_color.a = i.depth; // <= HERE 110 | 111 | ... 112 | 113 | return main_color; 114 | } 115 | ``` 116 | 117 | #### Transparency 118 | 119 | Transparency on mobile is expensive so its best to try and avoid it entirely. Most of the time you can use more polygons cheaper than alpha transparency. Note that the depth of field shader is only compatible with transparent-cutout. Alpha blending won't work without some crazy hacks and its too expensive anyway. 120 | 121 | For transparent-cutout and depth of field to play nicely you must use a vertex/fragment shader. The transparency shader is otherwise identical to the vertex-fragment shader example with two exceptions: 122 | 123 | 1. Make sure the appropriate render tags are set: 124 | 125 | ```glsl 126 | Tags { 127 | "Queue" = "Transparent" 128 | "RenderType" = "TransparentCutout" 129 | } 130 | ``` 131 | 132 | 2. Call the `clip` function in the fragment shader _before_ you override the output alpha with the depth: 133 | 134 | ```glsl 135 | // Clip based on the texture alpha 136 | clip(main_color.a - 0.5f); // You can vary the 0.5 if the cutout seems a bit rough 137 | 138 | // Place `vert` depth calculation into alpha channel 139 | main_color.a = i.depth; 140 | ``` 141 | 142 | An example is include in "Example/Materials/Transparency.mat". 143 | 144 | #### Terrain 145 | 146 | An example of a built-in terrain shader modified to work with the depth of field is included (see "Example/Materials/Tree.mat" or toggle the "Terrain" game object). 147 | 148 | The changes to apply to the terrain shaders are the same as above. A few things to keep in mind: 149 | 150 | - Sometimes the vertex function is inside of an external include file such as "SH_Vertex.cginc". You'll need to create your own "SH_Vertex.cginc" (just copy+paste) to override the internal Unity version. 151 | - In the fragment function make sure that you set the alpha _after_ the clip has been applied: 152 | 153 | ```glsl 154 | fixed4 frag(v2f input) : SV_Target { 155 | fixed4 c = tex2D( _MainTex, input.uv.xy); 156 | c.rgb *= 2.0f * input.color.rgb; 157 | 158 | clip (c.a - _Cutoff); 159 | 160 | // Override alpha, _after_ `clip` 161 | c.a = input.depth; // <= HERE 162 | 163 | return c; 164 | } 165 | ``` 166 | 167 | - Some terrain shaders use `ColorMask RGB`. This must be disabled (just remove/comment the line) as it prevents the alpha value from getting passed to the RenderTexture. 168 | - Usually only one of the shader passes needs to be modified. For example, in the example terrain shader, you don't need to modify the "ShadowCaster" pass since its only used for calculating shadows. 169 | 170 | #### Excluding meshes 171 | 172 | There are two methods for excluding meshes like GUIs or lens flares from the depth of field. Each approach has different advantages and drawbacks. 173 | 174 | The first method is to modify the shader you want to be in focus to force the depth to a fixed value outside of the normal range, like `-1.0f`. For example, in the excluded shader’s `surf` or `frag` function setting the alpha value (which holds the real depth value) to `-1.0f`: 175 | 176 | With a surface shader: 177 | 178 | ```glsl 179 | void surf(Input IN, inout SurfaceOutput o) { 180 | // ... 181 | o.Alpha = -1.0f; 182 | } 183 | ``` 184 | 185 | With a vertex/fragment shader: 186 | 187 | ```glsl 188 | half4 frag(v2f i) : COLOR { 189 | // ... 190 | 191 | main_color.a = -1.0f; 192 | return main_color; 193 | } 194 | ``` 195 | 196 | See Example/Images/Exclude1.jpg or toggle the "Exclude 1" game object for an example. 197 | 198 | Note that the cubes are in focus, despite their blurred surroundings. 199 | 200 | The second method for excluding a mesh from the depth of field requires two cameras. Everything you exclude will be rendered on top of everything else. The benefit to this approach is that there are no bleeding edges like in the first approach and can be useful for GUIs or lens flares. 201 | 202 | 1. Create a new layer called “DoF Exclude” by going to Edit > Project Settings > Tags and Layers 203 | 2. Assign the objects you want to exclude to this new layer. 204 | 3. On your main camera, uncheck “DoF Exclude” from the Culling Mask option. You should see the excluded objects disappear. 205 | 4. Create a new camera and parent it under your main camera (so they move together). 206 | 5. Set the secondary camera’s Clear Flags to depth only. Since we’ve disabled the Unity depth map on both cameras this camera should always render on top of the main camera. 207 | 6. Set the secondary camera’s Culling Mask to only “DoF Exclude”. 208 | 7. All other settings should probably match your main camera (such as Field of View, etc.) 209 | 210 | See Example/Images/Exclude2.jpg or toggle the "Exclude 2" game object and "Main Camera/Exclude Camera" for an example of this approach. 211 | 212 | Note how the cubes are overlapping everything in the main layer. 213 | --------------------------------------------------------------------------------