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 alexvm · Mar 03, 2017 at 06:17 PM · meshmaterialshaders

Get mesh size in frag/vert shader

I have a shader generating the following effect:

alt text

It's a simple frag/vert shader which changes the alpha value from 0 - {user defined} based on the position from the center of the mesh.

My pass block looks like this:

     struct appdata
             {
                 float4 vertex : POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
                 float distance : float2;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float4 _MainTex_TexelSize;
             fixed4 _Color;
             fixed  _Scale;
             fixed  _Radius;
             fixed _Iris;
 
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.distance = distance(float3(0, 0, 0), v.vertex);
                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 float4 c = _Color;
                 float alpha = i.distance / 2;
                 c.a = _Color.a * alpha; // / _MainTex_TexelSize.x;
                 return c;
             }

You can see in the frag function I have a hard coded '2'. This is supposed to be the radius of the mesh and I would like to make it not hard coded so the mesh can be scaled and the alpha blend scales with the size of the mesh. Currently if I scale the mesh to radius 4 it will look like this:

alt text

What value could I use in my shader to get a normalized value representing this size? Is there a {0 - 1} or {-1 - 1} value I could use somehow?

New to shaders so apologies if the question is not clear..

capture.png (80.9 kB)
capture.png (32.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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by tanoshimi · Mar 03, 2017 at 07:40 PM

The shader doesn't know the size of the mesh it's rendering - it works with vertices and pixels. But you can provide this information via script.

Declare a new float variable in the Properties block at the top of your shader:

 _MeshRadius ("Mesh Radius", Float) = 1

And declare a matching variable within the CG code of your subshader.

 uniform float _MeshRadius;

Now you can replace your hardcoded value with a reference to the _MeshRadius variable.

To change the parameter, you can either explicity set a value in the material inspector, or through code:

 material.SetFloat("_MeshRadius", 3f);
Comment
Add comment · Show 4 · 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 Bunny83 · Mar 03, 2017 at 09:35 PM 0
Share

Right, I'm just wondering because he already has a "_Scale", "_Radius" and "_Iris" variable but none of them is actually used.

avatar image tanoshimi Bunny83 · Mar 03, 2017 at 10:49 PM 0
Share

True. And we don't know if those are exposed in the properties block or set anywhere else. I'm guessing this is some Frankenstein code that has been cobbled together from various places!

avatar image alexvm tanoshimi · Mar 03, 2017 at 11:01 PM 0
Share

We all start somewhere ;)

avatar image alexvm · Mar 03, 2017 at 11:01 PM 0
Share

This is what I ended up doing. You can see in the code that I had a radius property setup (I should have tidied my code up..) which I set on awake of the gameobject. I now understand shaders work on pixels and vertices as you say so the concept of mesh size is not available.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Create a hole in a mesh using a shader 0 Answers

How do I debug the following error DirectX 11 shader input signature is null ? 1 Answer

How do I only show parts of objects that overlap [2D] 1 Answer

How do I properly apply a transparent texture to a mesh? 2 Answers

Apply new complex fbx to saved prefab 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