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 inventor200 · Sep 09, 2015 at 06:47 PM · camerashadershaderstreesreplacement shader

Help with multi-purpose replacement shader for camera

Hello! So I'm designing a replacement shader that the camera can alternatively be set to during gameplay. The goal is to color objects based on their position in the map. Therefore, moving objects flash like a rainbow.

So far, it works on 3D objects, but it's a replacement shader. This means that once it's applied to the camera, EVERYTHING will be rendered this way. Unfortunately, billboarded trees don't even appear. I want to fix this.

In the code below, I tried to fuse Unity's default tree billboard shader with my trippy rainbow one. The idea was to see if billboarded trees could render normally in the rainbow shader mode before I change the tree's part.

The billboarded trees still are not showing up at all. Here's my code. I'm new to programming shaders, and I'd like to know what I'm doing wrong. At the end of the day, I want the shader to be capable of rendering everything in this rainbow fashion. Opaques, sprites, billboarded trees, etc.

 Shader "Custom/EI" {
     Properties {
         _Inv ("Inv", float) = 1 //Global float which controls positivity or negativity of colors. Will make an int soon.
         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {} //_MainTex taken from Unity tree billboard shader.
     }
     SubShader { 
         Pass { // First pass should be all 3D objects. This has been working so far
             Tags {"LightMode" = "ForwardBase" "RenderType" = "Opaque"}
             CGPROGRAM
             
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             
             float _Inv;
             
             struct vertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
             };
             struct vertexOutput {
                 float4 pos : SV_POSITION;
                 float4 col : COLOR;
             };
             
             float3 jnoise(float3 b) { //Returns a color based on given 3D coordnates
                 float x = saturate((sin(b.x)+1.1)/2);
                 float y = saturate((sin(b.y)+1.1)/2);
                 float z = saturate((cos(b.z)+1.1)/2);
                 float mag = 3/(x+y+z);
                 return float3(x,y,z)*mag*0.5f;
             }
             
             vertexOutput vert(vertexInput v) {
                 vertexOutput o;
                 float3 normalDirection = normalize(mul(float4(v.normal,0.0), _World2Object).xyz);
                 float3 viewdir = WorldSpaceViewDir(v.vertex);
                 
                 float3 diffuseReflection = dot(normalDirection, viewdir);
                 //Get normal of vertex relative to camera. d"n" means d-"normal"
                 float dn = clamp(diffuseReflection.x / distance(_WorldSpaceCameraPos, mul(_Object2World, v.vertex)), 0, 1);
                 if (_Inv == 1) dn = 1-dn;
                 float4 oo = mul(_Object2World, float4(0.0,0.0,0.0,1.0) ); //"oo" means "object origin"
                 o.col = float4(jnoise(oo.xyz),1) * float4(dn,dn,dn,1); //Coord color multiplied by normal shade.
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 return o;
             }
             
             float4 frag(vertexOutput i) : COLOR {
                 return i.col;
             }
             
             ENDCG
         }
         Pass { // Second pass taken directly from Unity's tree billboard shader. Pasted here to see
                 //if billboard trees would be rendered normally using replacement shader.
             Tags {"LightMode"="ForwardBase" "Queue" = "Transparent-100" "IgnoreProjector"="True" "RenderType"="TreeBillboard" }
             ColorMask rgb
             Blend SrcAlpha OneMinusSrcAlpha
             ZWrite Off Cull Off
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fog
             #include "UnityCG.cginc"
             #include "TerrainEngine.cginc"
 
             struct v2f {
                 float4 pos : SV_POSITION;
                 fixed4 color : COLOR0;
                 float2 uv : TEXCOORD0;
                 UNITY_FOG_COORDS(1)
             };
 
             v2f vert (appdata_tree_billboard v) {
                 v2f o;
                 TerrainBillboardTree(v.vertex, v.texcoord1.xy, v.texcoord.y);    
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv.x = v.texcoord.x;
                 o.uv.y = v.texcoord.y > 0;
                 o.color = v.color;
                 UNITY_TRANSFER_FOG(o,o.pos);
                 return o;
             }
 
             sampler2D _MainTex;
             fixed4 frag(v2f input) : SV_Target
             {
                 fixed4 col = tex2D( _MainTex, input.uv);
                 col.rgb *= input.color.rgb;
                 clip(col.a);
                 UNITY_APPLY_FOG(input.fogCoord, col);
                 return col;
             }
             ENDCG            
         }
     }
 }


Thank you to everyone who can offer any help!

Comment
Add comment · Show 2
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 inventor200 · Sep 10, 2015 at 10:49 PM 0
Share

bump because forum

avatar image inventor200 · Sep 21, 2015 at 03:56 PM 0
Share

Another bump

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dashadatsenko · Jan 23, 2020 at 01:03 PM

Hi! I am having similar issue. Did you manage to create the replacement shader for Tree Creator trees?

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 inventor200 · Jan 23, 2020 at 06:21 PM 0
Share

Sorry to say I have not managed to create the replacement, and this project has been binned and replaced by another attempt since posting this.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to hide Unity terrain trees when they are blocking the camera view? 0 Answers

Is there any way I can create a triangle shaped camera view? 1 Answer

Trees appearing as black square when oculus is plugged in 0 Answers

Custom vertex shader doesn't write to camera depth normals texture. 1 Answer

How to combine a camera rendering an edge detection image effect with another camera 1 Answer


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