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 Sam 6 · Apr 10, 2011 at 05:11 PM · shaderterraincolormain

Main Color on terrain splatmap shader

Asked this in Shader lab section on the forums but I thought I would post here too. Is it possible to add the Main color attribute to the Bootcamp 4 splat terrain shader? I can figure out how to do it.

Shader "Misc/Mesh Terrain 4 Splats" { Properties { _Control ("SplatMap (RGBA)", 2D) = "red" {} _Splat0 ("Layer 0 (R)", 2D) = "white" {} _Splat1 ("Layer 1 (G)", 2D) = "white" {} _Splat2 ("Layer 2 (B)", 2D) = "white" {} _Splat3 ("Layer 3 (A)", 2D) = "white" {} _BaseMap ("BaseMap (RGB)", 2D) = "white" {} }

// Fragment program SubShader { Tags { "RenderType" = "Opaque" } Pass { Tags { "LightMode" = "Always" }

     CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
     #pragma fragmentoption ARB_precision_hint_fastest
     #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF

     #include "UnityCG.cginc"

     struct appdata_lightmap {
         float4 vertex : POSITION;
         float2 texcoord : TEXCOORD0;
         float2 texcoord1 : TEXCOORD1;
     };

     struct v2f_vertex {
         float4 pos : SV_POSITION;
         float4 uv[3] : TEXCOORD0;
     };

     uniform sampler2D _Control;
     uniform float4 _Control_ST;

     #ifdef LIGHTMAP_ON
     uniform float4 unity_LightmapST;
     uniform sampler2D unity_Lightmap;
     #endif

     uniform sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
     uniform float4 _Splat0_ST,_Splat1_ST,_Splat2_ST,_Splat3_ST;

     v2f_vertex vert (appdata_lightmap v) 
     {
         v2f_vertex o;
         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
         o.uv[0].xy = TRANSFORM_TEX (v.texcoord.xy, _Control);
     #ifdef LIGHTMAP_ON
         o.uv[0].zw = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
     #else
         o.uv[0].zw = half2(0,0);
     #endif
         o.uv[1].xy = TRANSFORM_TEX (v.texcoord.xy, _Splat0);
         o.uv[1].zw = TRANSFORM_TEX (v.texcoord.xy, _Splat1);
         o.uv[2].xy = TRANSFORM_TEX (v.texcoord.xy, _Splat2);
         o.uv[2].zw = TRANSFORM_TEX (v.texcoord.xy, _Splat3);

         return o;
     }

     half4 frag (v2f_vertex i) : COLOR
     {
         half4 splat_control = tex2D(_Control, i.uv[0].xy);
         half3 splat_color = splat_control.r * tex2D (_Splat0, i.uv[1].xy).rgb;
         splat_color += splat_control.g * tex2D (_Splat1, i.uv[1].zw).rgb;
         splat_color += splat_control.b * tex2D (_Splat2, i.uv[2].xy).rgb;
         splat_color += splat_control.a * tex2D (_Splat3, i.uv[2].zw).rgb;
         #ifdef LIGHTMAP_ON
         splat_color *= DecodeLightmap (tex2D (unity_Lightmap, i.uv[0].zw));
         #endif

         return half4 (splat_color, 0.0);
     }
     ENDCG
 }

}

// Fixed function SubShader { Tags { "RenderType" = "Opaque" } Pass { Tags { "LightMode" = "Vertex" } SetTexture [_BaseMap] { constantColor(0,0,0,0) combine texture, constant } } Pass { Tags { "LightMode" = "VertexLM" } SetTexture [unity_Lightmap] { combine texture } SetTexture [_BaseMap] { constantColor(0,0,0,0) combine texture previous, constant } } Pass { Tags { "LightMode" = "VertexLMRGBM" } SetTexture [unity_Lightmap] { combine texture texture alpha DOUBLE } SetTexture [_BaseMap] { constantColor(0,0,0,0) combine texture * previous DOUBLE, constant }

 }

} }

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
1
Best Answer

Answer by taoa · Apr 12, 2011 at 08:31 AM

You could add a MainColor attribute to this shader like so:

Shader "Misc/Mesh Terrain 4 Splats" { Properties { _Control ("SplatMap (RGBA)", 2D) = "red" {} _Splat0 ("Layer 0 (R)", 2D) = "white" {} _Splat1 ("Layer 1 (G)", 2D) = "white" {} _Splat2 ("Layer 2 (B)", 2D) = "white" {} _Splat3 ("Layer 3 (A)", 2D) = "white" {} _BaseMap ("BaseMap (RGB)", 2D) = "white" {} _Color ("Main Color", Color) = (1,1,1,1) }

// Fragment program SubShader { Tags { "RenderType" = "Opaque" } Pass { Tags { "LightMode" = "Always" }

     CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
     #pragma fragmentoption ARB_precision_hint_fastest
     #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF

     #include "UnityCG.cginc"

     struct appdata_lightmap {
         float4 vertex : POSITION;
         float2 texcoord : TEXCOORD0;
         float2 texcoord1 : TEXCOORD1;
     };

     struct v2f_vertex {
         float4 pos : SV_POSITION;
         float4 uv[3] : TEXCOORD0;
     };

     uniform sampler2D _Control;
     uniform float4 _Control_ST;

     #ifdef LIGHTMAP_ON
     uniform float4 unity_LightmapST;
     uniform sampler2D unity_Lightmap;
     #endif

     uniform sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
     uniform float4 _Splat0_ST,_Splat1_ST,_Splat2_ST,_Splat3_ST;

     v2f_vertex vert (appdata_lightmap v) 
     {
         v2f_vertex o;
         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
         o.uv[0].xy = TRANSFORM_TEX (v.texcoord.xy, _Control);
     #ifdef LIGHTMAP_ON
         o.uv[0].zw = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
     #else
         o.uv[0].zw = half2(0,0);
     #endif
         o.uv[1].xy = TRANSFORM_TEX (v.texcoord.xy, _Splat0);
         o.uv[1].zw = TRANSFORM_TEX (v.texcoord.xy, _Splat1);
         o.uv[2].xy = TRANSFORM_TEX (v.texcoord.xy, _Splat2);
         o.uv[2].zw = TRANSFORM_TEX (v.texcoord.xy, _Splat3);

         return o;
     }

     uniform float4 _Color;

     half4 frag (v2f_vertex i) : COLOR
     {
         half4 splat_control = tex2D(_Control, i.uv[0].xy);
         half3 splat_color = splat_control.r * tex2D (_Splat0, i.uv[1].xy).rgb;
         splat_color += splat_control.g * tex2D (_Splat1, i.uv[1].zw).rgb;
         splat_color += splat_control.b * tex2D (_Splat2, i.uv[2].xy).rgb;
         splat_color += splat_control.a * tex2D (_Splat3, i.uv[2].zw).rgb;
         splat_color *= _Color.rgb;
         #ifdef LIGHTMAP_ON
         splat_color *= DecodeLightmap (tex2D (unity_Lightmap, i.uv[0].zw));
         #endif

         return half4 (splat_color, 0.0);
     }
     ENDCG
 }

} }

HOWEVER: I'm not sure if there's any specifics to the Bootcamp demo terrain, but if it's anything like normal terrains you make in the editor, you can NOT access the terrain's material/rendering properties, may it be either via the editor itself or via the scripting API. It's completely locked inside the engine. This would means even though this shader now supports a general color multiplier, you won't be able to change it in any way. So the color value will always be the one you hardcoded as a default value in the Properties block (here white). Again, if it turns out this isn't like a normal terrain, then there's no problem and you should be able to play with this shader like any other shader.

P.S.: I care not for fixed-pipeline shaders, hence I did not even try to implement this shader's version with the added Main Color property.

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 Sam 6 · Apr 15, 2011 at 01:11 AM 0
Share

Thank you very much. Now I can use this for my night day cycle terrain!

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

No one has followed this question yet.

Related Questions

Can someone add a Main Color attribute to this shader? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Why Does My Scene Do This? 1 Answer

Shader Texture Change 3 Answers

Changing Replacement Shaders at Runtime 2 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