Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HarsanyiT · Jan 21, 2019 at 04:54 PM · shadereffectpost processingpost-processing

Custom Post process effect creates only a triangle on the screen.

Hi!

I'm new with shaders, so maybe this is a noob question, but I didn't found any solution on the web so:

I wrote this edge detection shader for use it in a post process effect. It is working "fine" if I use it with a monobehaviour on the camera (pasted lower), but not if I try to use it in a custom post process effect. (code at the buttom) In that case it is creates only a triangle. I would be happy with the monobehaviour solution but in that case it is overwrites the other effects.

WithMono:

alt text

With Post:

alt text

The shader:

 Shader "Hidden/EdgeDetection"
 {
     Properties
     {
         _EdgeColor("EdgeColor", COLOR) = (0,0,0,1)
         _SampleDistance("SampleDistance", float) = 1
         _DepthSensitivity("DepthSensitivity", float) = 0.1
         _NormalSensitivity("NormalSensitivity",float) = 0.1
         _FadeToDistance("FadeToDistance", float) = 1
     }
         SubShader
     {
         // No culling or depth
         Cull Off ZWrite Off ZTest Always
 
         Pass
         {
             CGPROGRAM
             #pragma vertex VertexToFragment
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             //vars
             sampler2D _Depth;
             sampler2D _CameraDepthNormalsTexture;
             float2 _CameraDepthNormalsTexture_TexelSize;
             float4 _EdgeColor : COLOR;
             float _DepthSensitivity;
             float _NormalSensitivity;
             float _SampleDistance;
             sampler2D _MainTex;
             float _FadeToDistance;
             
 
             struct VertInput
             {
                 float4 pos : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct VertexOutput
             {
                 float4 clipPos : SV_POSITION;
                 float2 uv : texcoord;
             };
 
             VertexOutput VertexToFragment (VertInput input)
             {
                 VertexOutput output;
                 output.clipPos = UnityObjectToClipPos(input.pos);
                 output.uv = input.uv;    
                 return output;
             }
  
 
             fixed4 frag (VertexOutput i) : SV_Target
             {                
                 fixed4 col = tex2D(_CameraDepthNormalsTexture, i.uv);
                 fixed4 texColor = tex2D(_MainTex, i.uv);
 
                 float depth;
                 float3 normal;
                 DecodeDepthNormal(col, depth, normal);
 
                 float2 uvs[4];
                 float depthDistance = _SampleDistance * (1-depth*0.7);
                 uvs[0] = i.uv + float2(_CameraDepthNormalsTexture_TexelSize.x, _CameraDepthNormalsTexture_TexelSize.y)*depthDistance;
                 uvs[1] = i.uv + float2(_CameraDepthNormalsTexture_TexelSize.x, -_CameraDepthNormalsTexture_TexelSize.y)*depthDistance;
                 uvs[2] = i.uv + float2(-_CameraDepthNormalsTexture_TexelSize.x, -_CameraDepthNormalsTexture_TexelSize.y)*depthDistance;
                 uvs[3] = i.uv + float2(-_CameraDepthNormalsTexture_TexelSize.x, _CameraDepthNormalsTexture_TexelSize.y)*depthDistance;
 
                 //Neighbours - get datas
                 fixed4 nCols[4];
                 float nDepths[4];
                 float3 nNormals[4];
                 
                 nCols[0] = tex2D(_CameraDepthNormalsTexture, uvs[0]);
                 nCols[1] = tex2D(_CameraDepthNormalsTexture, uvs[1]);
                 nCols[2] = tex2D(_CameraDepthNormalsTexture, uvs[2]);
                 nCols[3] = tex2D(_CameraDepthNormalsTexture, uvs[3]);
 
                 DecodeDepthNormal(nCols[0], nDepths[0], nNormals[0]);
                 DecodeDepthNormal(nCols[1], nDepths[1], nNormals[1]);
                 DecodeDepthNormal(nCols[2], nDepths[2], nNormals[2]);
                 DecodeDepthNormal(nCols[3], nDepths[3], nNormals[3]);
                 
                 float avgDepth = (nDepths[0] + nDepths[1] + nDepths[2] + nDepths[3])*0.25;
                 bool isEdge = abs(avgDepth - depth) > _DepthSensitivity*(1-depth);
                 bool isFade = _FadeToDistance >= 0.5;
                 
                 //is edge by depth
                 if (isEdge) {
                     if (isFade) {
                         texColor.rgb = lerp(_EdgeColor.rgb, texColor.rgb, depth);
                     }
                     else {
                         texColor.rgb = _EdgeColor.rgb;
                     }
                     
                 }
                 else {
                     //is edge by normals
                     float3 avgNormal = nNormals[0] + nNormals[1] + nNormals[2] + nNormals[3];
                     avgNormal = normalize(avgNormal);
                     float normalDif = 1-((dot(normal, avgNormal)+1)*0.5);
                     isEdge = normalDif > _NormalSensitivity*(1+depth*10);
 
                     if (isEdge) {
                         if (isFade) {
                             texColor.rgb = lerp(_EdgeColor.rgb, texColor.rgb, depth);
                         }
                         else {
                             texColor.rgb = _EdgeColor.rgb;
                         }
                     }
                 }
                 
                 return texColor;
             }
             
             ENDCG
         }
     }
 }
 

The Mono:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class EdgeDetectEffect : MonoBehaviour
 {
     public Material postProcessingMat;
 
     private void Start()
     {
         GetComponent<Camera>().depthTextureMode = DepthTextureMode.DepthNormals;
     }
 
     private void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
         Graphics.Blit(source, destination, postProcessingMat);
     }
 }
 

The Post Process:

 using System;
 using UnityEngine;
 using UnityEngine.Rendering.PostProcessing;
 
 [Serializable]
 [PostProcess(typeof(EdgeDetectRenderer), PostProcessEvent.BeforeStack, "Hidden/EdgeDetection")]
 public sealed class EdgeDetect : PostProcessEffectSettings
 {
       
 }
 
 public sealed class EdgeDetectRenderer : PostProcessEffectRenderer<EdgeDetect>
 {
     public override DepthTextureMode GetCameraFlags()
     {
         return DepthTextureMode.DepthNormals;
     }
 
     public override void Render(PostProcessRenderContext context)
     {
         var sheet = context.propertySheets.Get(Shader.Find("Hidden/EdgeDetection"));        
         context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
     }
 }

edgeon.png (185.2 kB)
edgeoff.png (116.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HarsanyiT · Jan 22, 2019 at 10:13 AM

Done!

If I add the ImageEffectOpaque attribute to the OnRenderImage method in the mono, than it is works like charm.

  using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class EdgeDetectEffect : MonoBehaviour
 {
     public Material postProcessingMat;
 
     private void Start()
     {
         GetComponent<Camera>().depthTextureMode = DepthTextureMode.DepthNormals;
     }
 
     [ImageEffectOpaque]
     private void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
         Graphics.Blit(source, destination, postProcessingMat);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Oct 09, 2020 at 01:17 AM 0
Share

Did you ever get the Post Processing Stack way of doing it to work? I see the same triangle...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you use a shader file once written? 1 Answer

Post processing vertex distortion effect 0 Answers

Old TV Set post effect 2 Answers

How can I create glowing trail effect? 0 Answers

How on earth did they create Elizabeth's tears? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges