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 sahaaja · May 28, 2012 at 09:01 AM · shadermeshvertex

Vertex position problem in vertex shader

I am having strange problems in shaders with procedurally generated mesh. My actual scenario is a little bit complicated but I managed to track it down to following:

  1. Create square mesh with 4 vertices (-10, -10), (-10, 10), (10, 10) and (10, -10)

  2. Apply a material with fragment shader which simply uses x-coordinate of vertex for color output

  • for some reason vertex positions in fragment shader are not in range from -10 to 10 but from 0 to 1 instead!

    Illustration of expected result (lots > of full black & full white)

    Illustration of actual result (smooth > fade from black to white, why?

I would expect result to be like shown in first image because vertex position (both local position and world position since it doesn't have rotation, scale or transform at all) goes from -10 to 10, resulting in all black when value is from -10 to 0, smooth fade when value is 0 to 1, and full white when value is 1 to 10. However, the result is like the vertex position would simply go from 0 to 1. Why is that? (I have done lots of 3D programming with pure OpenGL and DirectX so I know the basics of rendering pipeline, and this behavior seems very odd for me)

I use following piece of code to generate mesh:

 Mesh mesh = new Mesh ();
 GetComponent<MeshFilter>().mesh = mesh;
 
 float w = 10.0f;
 float h = 10.0f;
         
 Vector3[] vertices = new Vector3[4];
 vertices[0] = new Vector3(-w, -h, 0);
 vertices[1] = new Vector3(-w,  h, 0);
 vertices[2] = new Vector3( w,  h, 0);
 vertices[3] = new Vector3( w, -h, 0);
 
 int[] indices = new int[6];
 indices[0] = 0;
 indices[1] = 1;
 indices[2] = 2;
 indices[3] = 2;
 indices[4] = 3;
 indices[5] = 0;
 mesh.vertices = vertices;
 mesh.triangles = indices;

Shader is following:

 Shader "Custom/LightShader" {
 SubShader {
 Pass {
 
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 struct v2f {
     float4 pos : SV_POSITION;
     float4 vpos : COLOR0;
 };
 
 v2f vert (appdata_base v)
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.vpos = v.vertex;
     //o.vpos = mul (_Object2World, v.vertex); // doesn't matter if I use this one - the result will be same
     return o;
 }
 
 half4 frag (v2f i) : COLOR
 {
     float d = i.vpos.x;
     return half4 (d,d,d,1);
 }
 ENDCG
 }
 }
 Fallback "VertexLit"
 }


Comment
Add comment · Show 1
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 MiiBond · Jun 09, 2013 at 11:16 PM 0
Share

Hi sahaaja. I think I'm seeing the same or very similar issue as you.

The values co$$anonymous$$g into the vertex shader don't seem to be in the local space of the model, as all of my experience tells me they should be. If I create a bunch of instances of a really simple custom mesh (defined with vertices in the range of 0.0-1.0) and then pass the raw vertex position through to the fragment shader to be used as the colour (not the $$anonymous$$VP transformed ones, of course), the colours of the meshes vary across the world as if they're already in world space.

I would expect that each mesh should be coloured the same way since they should have local position coords that should be equal to each other.

$$anonymous$$y normal data is being transformed the same way (and I'm actually trying to pass in vertex-specific data using the normals so I really don't want the data messed with before it hits the vertex shader). The uvs aren't being transformed so I may end up just using them for this data..

I don't see anything in the Unity docs that would suggest any pre-transforms being done to the vertex data in Unity. Can anyone shed any light on this?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How does appdata_base work? 0 Answers

How to set float or float4 UV in Mesh. 1 Answer

Create a Wireframe Shader with Shader Forge 1 Answer

Simple mesh deformation : Mesh.vertices or vertex shader with displacement texture ? 1 Answer

Raycast on mesh deformed by shader 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