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 pixartist · Dec 09, 2014 at 09:35 AM · shadervertexinterpolationfragmentuniform

pass value from vertex to fragment without interpolation ?!

Hi, I need to pass the actual vertex coordinate from my vertex to my fragment shader without interpolation. I tried using a uniform variable but for some reason it seems not to hold a valid value at all.

An example:

 Shader "Vertex Colored" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
 }
 
 SubShader {
     Pass {
 
         CGPROGRAM
 
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
         float3 _p1,_p2;
         struct v2f {
             float4 pos : SV_POSITION; 
             float3 color : COLOR0;
             float3 bc : TEXCOORD2;
             float3 p : TEXCOORD3;
         };
         float PtoLine(float3 a, float3 b, float3 p)
         {
             return length(cross(p-a, p-b))/length(b-a);
         }
         v2f vert (appdata_full v) 
         {
             
             v2f o;
             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
             _p1 = v.vertex.xyz;
             _p2 = v.tangent.xyz;
             o.p = v.vertex;
             o.color = v.color;
             o.bc = v.tangent.xyz;
             return o;
         }
         
         half4 frag (v2f i) : COLOR
         {
             //return half4(i.bc, 1);
             return half4(_p1,1);
             if(1>0) 
                 return half4(0,0,0,1);
             else 
                 return half4 (i.color, 1);
         }
         
         ENDCG 
 
     }
 }
 
 
 
 Fallback " VertexLit", 1
 }


This will ALWAYS render black, no matter what the vertex coordinate is. What is going on here ?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Dec 09, 2014 at 08:53 PM

I think this is impossible. Not just in Unity.

Fragments are created based on the 2 or 3 or 4 or more verts making the face. They never even know how many or which verts made them, much less being able to see their "parent" vert data. Hardware making a single interpolated value works great for this.

The one exception I know of is "flat" shading. where the color is passed straight from the 1st vert in the face (and the rest are ignored.)

Since the vert shader runs for all verts, setting a uniform variable is pointless. It would just be set to the most recently processed vert, probably not one that made this pixel.

Comment
Add comment · 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
0

Answer by LaireonGames · Sep 01, 2020 at 12:11 PM

Kept coming across this whilst trying to solve this myself and figured out a work around that can work in some situations.

Add more verts so nothing is shared. That way your faces can store data unique to that face and interpolation will always return the same value.

In my situation each triangle had the same central vert and was causing me the interpolation issues. Duplicating that central vert for each face lets me fix this issue (granted with some extra mesh data but its tiny my situation)

Comment
Add comment · 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

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

Vertex/Fragment shaders - transparency ignoring Render Queue 3 Answers

Refraction Shader. Strange Artifacts 0 Answers

Vertex shader breaks render to texture - camera preview and render texture not the same 1 Answer

How can I make shadows pixelated in fragment function? 1 Answer

Prevent ColorMask obscuring parts of an object's mesh 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