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 Jazzer008 · Aug 31, 2013 at 12:28 PM · shaderinputuvsurfacexyz

uv.xyz in a surface shader?

Still pretty new to shader language, was wondering if I could grab uv world coordinates from an input in the surf function.

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

Answer by fherbst · Aug 31, 2013 at 12:55 PM

What do you mean by UV world coordonates? If you want to use the vertex position as uv, just do that (e.g. worldPos.xyz or vertex.xyz depending on your Input struct). If you want to access the third component you might have assigned in a modelling software, change your Input struct to hold a float4 for uv_MainTex instead of a float2.

See the examples "Slicing via world position" and "Normal extrusion with vertex modifier" here.

Comment
Add comment · Show 3 · 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 Jazzer008 · Aug 31, 2013 at 01:12 PM 0
Share

I mean exactly what it says on the tin. :D

The GameObject the shader is applied to is 3D, a certain uv coordinate on the model will also have a relative xyz position within the game world.

avatar image Jazzer008 · Aug 31, 2013 at 01:25 PM 0
Share

For what it's worth though, worldPos works just fine for what I'm doing, thanks! : )

avatar image fherbst · Aug 31, 2013 at 02:10 PM 0
Share

Glad that it helped - note that there's a difference between "relative to world" (worldPos) and "relative to object" (vertex pos).

avatar image
0

Answer by MountDoomTeam · Sep 04, 2013 at 08:18 AM

I have an example for you, it is from the improved noise project files from scrawkblog, he uses it to map 3d noise onto a texture, in the 3d Perlin shader

             half4 frag (v2f i) : COLOR
             {
                 //uncomment this for fractal noise
                 float n = fBm(i.uv.xyz, 4);
             
                 return half4(n,n,n,1);
             }



 Shader "Noise/ImprovedPerlinNoise3D" 
 {
     SubShader 
     {
         Pass 
         {
     
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 3.0
             #include "UnityCG.cginc"
             
             uniform sampler2D _PermTable2D, _Gradient3D;
             uniform float _Frequency, _Lacunarity, _Gain;
             
             struct v2f 
             {
                 float4 pos : SV_POSITION;
                 float4 uv : TEXCOORD;
                 //float2 texcoord : TEXCOORD0;
             };
             
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.vertex;
                 return o;
             }
             
             float3 fade(float3 t)
             {
                 return t * t * t * (t * (t * 6 - 15) + 10); // new curve
                 //return t * t * (3 - 2 * t); // old curve
             }
             
             float4 perm2d(float2 uv)
             {
                 return tex2D(_PermTable2D, uv);
             }
             
             float gradperm(float x, float3 p)
             {
                 float3 g = tex2D(_Gradient3D, float2(x, 0) ).rgb *2.0 - 1.0;
                 return dot(g, p);
             }
                         
             float inoise(float3 p)
             {
                 float3 P = fmod(floor(p), 256.0);    // FIND UNIT CUBE THAT CONTAINS POINT
                   p -= floor(p);                      // FIND RELATIVE X,Y,Z OF POINT IN CUBE.
                 float3 f = fade(p);                 // COMPUTE FADE CURVES FOR EACH OF X,Y,Z.
             
                 P = P / 256.0;
                 const float one = 1.0 / 256.0;
                 
                 // HASH COORDINATES OF THE 8 CUBE CORNERS
                 float4 AA = perm2d(P.xy) + P.z;
              
                 // AND ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
                   return lerp( lerp( lerp( gradperm(AA.x, p ),  
                                          gradperm(AA.z, p + float3(-1, 0, 0) ), f.x),
                                    lerp( gradperm(AA.y, p + float3(0, -1, 0) ),
                                          gradperm(AA.w, p + float3(-1, -1, 0) ), f.x), f.y),
                                          
                              lerp( lerp( gradperm(AA.x+one, p + float3(0, 0, -1) ),
                                          gradperm(AA.z+one, p + float3(-1, 0, -1) ), f.x),
                                    lerp( gradperm(AA.y+one, p + float3(0, -1, -1) ),
                                          gradperm(AA.w+one, p + float3(-1, -1, -1) ), f.x), f.y), f.z);
             }
             
             // fractal sum, range -1.0 - 1.0
             float fBm(float3 p, int octaves)
             {
                 float freq = _Frequency, amp = 0.5;
                 float sum = 0;    
                 for(int i = 0; i < octaves; i++) 
                 {
                     sum += inoise(p * freq) * amp;
                     freq *= _Lacunarity;
                     amp *= _Gain;
                 }
                 return sum;
             }
             
             // fractal abs sum, range 0.0 - 1.0
             float turbulence(float3 p, int octaves)
             {
                 float sum = 0;
                 float freq = _Frequency, amp = 1.0;
                 for(int i = 0; i < octaves; i++) 
                 {
                     sum += abs(inoise(p*freq))*amp;
                     freq *= _Lacunarity;
                     amp *= _Gain;
                 }
                 return sum;
             }
             
             // Ridged multifractal, range 0.0 - 1.0
             // See "Texturing & Modeling, A Procedural Approach", Chapter 12
             float ridge(float h, float offset)
             {
                 h = abs(h);
                 h = offset - h;
                 h = h * h;
                 return h;
             }
             
             float ridgedmf(float3 p, int octaves, float offset)
             {
                 float sum = 0;
                 float freq = _Frequency, amp = 0.5;
                 float prev = 1.0;
                 for(int i = 0; i < octaves; i++) 
                 {
                     float n = ridge(inoise(p*freq), offset);
                     sum += n*amp*prev;
                     prev = n;
                     freq *= _Lacunarity;
                     amp *= _Gain;
                 }
                 return sum;
             }
             
             half4 frag (v2f i) : COLOR
             {
                 //uncomment this for fractal noise
                 float n = fBm(i.uv.xyz, 4);
 
                 //uncomment this for turbulent noise
                 //float n = turbulence(i.uv.xyz, 4);
                 
                 //uncomment this for ridged multi fractal
                 //float n = ridgedmf(i.uv.xyz, 4, 1.0);
             
                 return half4(n,n,n,1);
             }
             
             ENDCG
     
         }
     }
     Fallback "VertexLit"
 }
 
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 ABerlemont · Feb 11, 2015 at 02:47 PM 0
Share

I don't understand how to use this in Unity. Why is the half4 frag() out of the shader code ? Help ? :)

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

18 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

Related Questions

uv not being passed through from vertex to surface shader 1 Answer

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

UV offset in surface shader. 1 Answer

Second UV set as coordinates for a different texture? 3 Answers

TexGen'ed UVs - how to access in surface shader? 1 Answer


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