Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 0tacun · Mar 13, 2014 at 07:33 PM · shadercgportingssaounitycg

Need help porting shader to CG

Hi all,

my knowledge about shaders is very limited... Anyway I need some help porting a SSAO shader from this guy : http://www.theorangeduck.com/page/pure-depth-ssao to unity CG

This is what I have done so far... I don't know if I'm on the right approuch:

 Shader "Custom/SSAOTest" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _DepthTextureSampler("Depth Texture", 2D) = "black" {}
         _RandomTextureSampler("Random Texture", 2D) = "grey" {}
     }
     
     CGINCLUDE
     #include "UnityCG.cginc"
     
     sampler2D _MainTex;
     sampler2D _DepthTextureSampler;
     sampler2D _RandomTextureSampler;
     
     struct v2f {
         float4 pos : POSITION;
         float2 uv[8] : TEXCOORD0;
     };
 
     float3 normal_from_depth(float depth, float2 texcoords) {
         const float2 offset1 = float2(0.0,0.001);
         const float2 offset2 = float2(0.001,0.0);
 
         float depth1 = tex2D(_DepthTextureSampler, texcoords + offset1).r;
         float depth2 = tex2D(_DepthTextureSampler, texcoords + offset2).r;
 
         float3 p1 = float3(offset1, depth1 - depth);
         float3 p2 = float3(offset2, depth2 - depth);
 
         float3 normal = cross(p1, p2);
         normal.z = -normal.z;
 
         return normalize(normal);
     }
     
     
     //PS_OUTPUT ps_ssao(VS_OUT_SSAO In)
         half4 frag (v2f In) : COLOR
     { 
         //PS_OUTPUT Output;
 
         const float total_strength = 1.0;
         const float base = 0.2;
 
         const float area = 0.0075;
         const float falloff = 0.000001;
 
         const float radius = 0.0002;
 
         const int samples = 16;
         float3 sample_sphere[samples] = {
             float3( 0.5381, 0.1856,-0.4319), float3( 0.1379, 0.2486, 0.4430),
             float3( 0.3371, 0.5679,-0.0057), float3(-0.6999,-0.0451,-0.0019),
             float3( 0.0689,-0.1598,-0.8547), float3( 0.0560, 0.0069,-0.1843),
             float3(-0.0146, 0.1402, 0.0762), float3( 0.0100,-0.1924,-0.0344),
             float3(-0.3577,-0.5301,-0.4358), float3(-0.3169, 0.1063, 0.0158),
             float3( 0.0103,-0.5869, 0.0046), float3(-0.0897,-0.4940, 0.3287),
             float3( 0.7119,-0.0154,-0.0918), float3(-0.0533, 0.0596,-0.5411),
             float3( 0.0352,-0.0631, 0.5460), float3(-0.4776, 0.2847,-0.0271)
             };
 
         float3 random = normalize( tex2D(_RandomTextureSampler, In.uv/*In.Tex0 * 4.0)*/).rgb );
 
         float depth = tex2D(_DepthTextureSampler, In.uv).r;
 
         float3 position = float3(In.uv, depth);
         float3 normal = normal_from_depth(depth, In.uv);
 
         float radius_depth = radius/depth;
         float occlusion = 0.0;
         
         for(int i=0; i < samples; i++) {
             float3 ray = radius_depth * reflect(sample_sphere[i], random);
             float3 hemi_ray = position + sign(dot(ray,normal)) * ray;
 
             float occ_depth = tex2D(_DepthTextureSampler, saturate(hemi_ray.xy)).r;
             float difference = depth - occ_depth;
 
             occlusion += step(falloff, difference) * (1.0-smoothstep(falloff, area, difference));
         }
 
         float ao = 1.0 - total_strength * occlusion * (1.0 / samples);
         Output.RGBColor = saturate(ao + base);
 
         return Output;
     }
     
     ENDCG
     
     
     SubShader {
         blend off
         pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 3.0
             ENDCG
         }
     }
 }


Errors occur in line 66 and 95:

-Shader error in 'Custom/SSAOTest': Program 'vert', invalid type in type constructor at line 66

-Shader error in 'Custom/SSAOTest': Program 'frag', invalid type in type constructor at line 66

-Shader error in 'Custom/SSAOTest': Shader program had errors at line 95

I'll would be glad for any help

Best 0tacun

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Shader Not Blending Textures on Mobile 1 Answer

Shader error in ... : Syntax error at line 30 1 Answer

CG Programming, Points Lights attenuation and light range 3 Answers

Multiple tex2D commands in fragment shader needed, is there a workaround? 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