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 JohnWatson · Apr 03, 2014 at 09:10 AM · shadertextureshaderlabdof

[Unity Free]Add textures to existing Unity DOF Shader

Hello again,

I don't own Unity Pro, so the standard 'Depth of Field' option is not available to me. So I have set out to either finding a Shader online or making it myself, I have found a Shader in the Asset store made by Unity but it doesn't support textures so I tried to add that Property, but with no luck. Honestly I'm just really not sure what I'm doing.

Error Messages:

![Missing Image][1]

Original Code:

 Shader "Hidden/Render Depth" {
 SubShader {
     Tags { "RenderType"="Opaque" }
     Pass {
         Fog { Mode Off }
         
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 struct v2f {
     float4 pos : POSITION;
     #ifdef UNITY_MIGHT_NOT_HAVE_DEPTH_TEXTURE
     float2 depth : TEXCOORD0;
     #endif
 };
 
 v2f vert( appdata_base v ) {
     v2f o;
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
     UNITY_TRANSFER_DEPTH(o.depth);
     return o;
 }
 
 half4 frag(v2f i) : COLOR {
     UNITY_OUTPUT_DEPTH(i.depth);
 }
 
 ENDCG
 
     }
 }
 
 Fallback Off
 
 }
 



Modified Code:

     //============================================================================//
     //=========================Titel: 5_DOF-texture.shader========================//
     //===================Autor: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX===================//
     //==============================Datum: 03.04.2014=============================//
     //=================Beschreibung: Depth of Field-Texture Shader================//
     //============================================================================//
     
     Shader "Selfish Studio/5 - DOF Texture" {
         Properties { //Define any changeable properties here
         _Color ("Color Tint", Color) = (1.0,1.0,1.0,1.0)
         _MainTex ("Diffuse Texture", 2D) = "white" {}
         }
         SubShader { //Main Shader
             Pass {
                 Tags { "LightMode"="ForwardBase" }    
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #include "UnityCG.cginc"
                 
                 //User defined variables
                 uniform sampler2D _MainTex;
                 uniform float4 _MainTex_ST;
                 uniform float4 _Color;
                 
                 //Unity defined variables
                 uniform float4 _LightColor0;
     
                 //Base input structs
                 struct vertexInput{
                     float4 vertex : POSITION;
                     float3 normal : NORMAL;
                     float4 texcoord : TEXCOORD1;
                     #ifdef UNITY_MIGHT_NOT_HAVE_DEPTH_TEXTURE
                     float2 depth : TEXCOORD0;
                     #endif
                 };
                 struct vertexOutput{
                     float4 pos : SV_POSITION;
                     float4 tex : TEXCOORD1;
                     float4 posWorld : TEXCOORD2;
                     float3 normalDir : TEXCOORD3;
                 };
                 
                 //vertex function
                 vertexOutput vert(vertexInput v){
                     vertexOutput o;
                     
                     UNITY_TRANSFER_DEPTH(o.depth);
                     o.posWorld = mul(_Object2World, v.vertex);
                     o.normalDir = normalize( mul( float4( v.normal, 0.0 ), _World2Object ).xyz );
                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                     o.tex = v.texcoord;
                         
                     return o;
                 
                 //fragment function
                 float4 frag(vertexOutput i) : COLOR
                 {
                     UNITY_OUTPUT_DEPTH(i.depth);
                     float3 normalDirection = i.normalDir;
                     float3 viewDirection = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz );
                     float3 lightDirection;
                     float atten;
                     
                     if(_WorldSpaceLightPos0.w == 0.0){
                         atten = 1.0;
                         lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                     }
                     else{
                         float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
                         float distance = length(fragmentToLightSource);
                         atten = 1.0/distance;
                         lightDirection = normalize(fragmentToLightSource);
                     }
                     //Lighting
                     float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDirection, lightDirection));
                     float3 lightFinal = diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
                     
                     //Textures
                     float4 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                         
                     return float4(tex.xyz * lightFinal * _Color.xyz, 1.0 * i.depth);
                         
                 }
     
                 ENDCG
     
             }
         }
 
     Fallback "Diffuse"
 }

Thanks in Advance :D

2014-04-03 16_37_56-unity - shaders.unity - new unity project 2 - pc, mac & linux standalone_.png (14.0 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

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

Creating a shader that overlaps a diffuse with a TexGen ObjectLinear cutout 0 Answers

Shaderlab metallic map 0 Answers

What is the right way to modify depth of field focal distance via script at runtime? 1 Answer

Shader: 1 Base Texture Overlaid by 1 Transparent Texture. 0 Answers

Script vs Shader for animated texture 5 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