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 /
  • Help Room /
avatar image
0
Question by alu0101207957 · Mar 24 at 12:40 PM · shadermapbordermetaballs

Question about metaballs to create a influence map

I have this shader code, and i want use more than 4 colours, how can i do it. Now, the shader picks 4 main channels and creates an insolate edge usign it, how can i extend it to use more than 4 colours

Shader "Hidden/Metaballs" { Properties { _MainTex("Texture", 2D) = "white" {} }

 SubShader
 {
     Cull Off ZWrite Off ZTest Always
 
     //Draw metaball
     Pass
     {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
         struct appdata { float4 p : POSITION; float2 uv : TEXCOORD0; };
         struct v2f { float2 uv : TEXCOORD0; float4 p : SV_POSITION; };
         v2f vert(appdata v) { v2f o; o.p = UnityObjectToClipPos(v.p); o.uv = v.uv; return o; }
 
 
         struct Emitter
         {
             float3 position;
             float radius;
             float4 channels;
         };
 
         sampler2D _MainTex;
         float _Metaball_Smooth;
         int _EmitterCount;
         StructuredBuffer<Emitter> _Emitters;
 
         //smin from Inigo Quilez adapted for a float4
         float4 smin(float4 a, float4 b, float k)
         {
             float4 res = exp2(-k * a) + exp2(-k * b);
             return -log2(res) / k;
         }
 
         float4 frag(v2f i) : SV_Target
         {
             float4 col = 0.0;
             float2 pos = i.uv * 2.0 - 1.0;
             for (int j = 0; j < _EmitterCount; j++)
             {
                 Emitter emitter = _Emitters[j];
 
                 float dist = distance(pos, emitter.position.xz);
                 float factor = dist - emitter.radius;
                 col = lerp(col, smin(col, factor, _Metaball_Smooth), emitter.channels);
             }
             return col;
         }
         ENDCG
     }
 
 
     //Select main color
     Pass
     {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
         struct appdata { float4 p : POSITION; float2 uv : TEXCOORD0; };
         struct v2f { float2 uv : TEXCOORD0; float4 p : SV_POSITION; };
         v2f vert(appdata v) { v2f o; o.p = UnityObjectToClipPos(v.p); o.uv = v.uv; return o; }
 
 
         sampler2D _MainTex;
         float _Metaball_Smooth;
 
         float4 frag(v2f i) : SV_Target
         {
             float4 col = tex2D(_MainTex, i.uv);
             float minValue = min(min(col.r, col.g), min(col.b, col.a)) + 0.01;
             float alpha = step(minValue, -(1.0 / _Metaball_Smooth));
             if (col.r < minValue)
                 return float4(0.9, 0, 0, 0) * alpha;
             else if (col.g < minValue)
                 return float4(0, 0.9, 0, 0) * alpha;
             else if (col.b < minValue)
                 return float4(0, 0, 0.9, 0) * alpha;
             else if (col.a < minValue)
                 return float4(0, 0, 0, 0.9) * alpha;
             return float4(0, 0, 0, 0);
         }
         ENDCG
     }
 
     //Blur
     Pass
     {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
         struct appdata { float4 p : POSITION; float2 uv : TEXCOORD0; };
         struct v2f { float2 uv : TEXCOORD0; float4 p : SV_POSITION; };
         v2f vert(appdata v) { v2f o; o.p = UnityObjectToClipPos(v.p); o.uv = v.uv; return o; }
 
 
         sampler2D _MainTex;
         float4 _MainTex_TexelSize;
         int _BlurItterations;
         float2 _BlurRange;
 
         float4 frag(v2f i) : SV_Target
         {
             float4 col = float4(0.0, 0.0, 0.0, 0.0);
             float sum = 0.0;
 
             for (int j = 0; j < _BlurItterations; j++)
             {
                 float t = ((float)(_BlurItterations - j) / _BlurItterations);
                 col += tex2D(_MainTex, i.uv + _BlurRange * j * _MainTex_TexelSize.xy) * t;
                 col += tex2D(_MainTex, i.uv - _BlurRange * j * _MainTex_TexelSize.xy) * t;
                 sum += t * 2;
             }
             return col / sum;
         }
         ENDCG
     }
 
        
     //Isolate Edges
     Pass
     {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
 
         #include "UnityCG.cginc"
 
         struct appdata
         {
             float4 vertex : POSITION;
             float2 uv : TEXCOORD0;
         };
 
         struct v2f
         {
             float2 uv : TEXCOORD0;
             float4 vertex : SV_POSITION;
         };
 
         v2f vert(appdata v)
         {
             v2f o;
             o.vertex = UnityObjectToClipPos(v.vertex);
             o.uv = v.uv;
             return o;
         }
 
         sampler2D _MainTex;
         float _EdgeThickness;
 
         float4 _RedChannelColor;
         float4 _GreenChannelColor;
         float4 _BlueChannelColor;
         float4 _AlphaChannelColor;
 
         float4 frag(v2f i) : SV_Target
         {
             float4 col = tex2D(_MainTex, i.uv);
             float4 gradient = (1.0 - col) * smoothstep(0.5, 0.55, col);
             float4 edge = smoothstep(0.4 - _EdgeThickness, 0.45 - _EdgeThickness, gradient);
 
             float4 result = gradient + edge;
 
             return _RedChannelColor * result.r +
                 _GreenChannelColor * result.g +
                 _BlueChannelColor * result.b +
                 _AlphaChannelColor * result.a;
         }
         ENDCG
     }
 }

}

alt text

i want add more colors to regions

05tqohi.png (285.7 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

250 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 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

Display border on Unity Terrain or Mesh 0 Answers

in-game map 0 Answers

Rotate custom skybox like Unity skybox 0 Answers

How can I have a uniform struct in a Shader? 0 Answers

Make realtime shader movement appear at intervals but update as if in realtime? 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