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 lakylekidd · Nov 14, 2014 at 04:59 PM · shadertextureperlin noise

Perlin Noise to Texture in Shader

Hello all,

I am trying to apply some perlin noise to a texture procedurally in a shader. I am new to shader language and I am experimenting to be able to generate some procedural terrains. I've managed to code the following:

 Shader "Custom/PerlinNoise" 
 {
     Properties 
     {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         samplerPerlinPerm2D("Perlin Perm", 2D) = "white" {}
         samplerPerlinGrad2D("Perlin Grad", 2D) = "white" {}
     }
 
     SubShader 
     {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert vertex:vert
 
         sampler2D _MainTex;
         uniform sampler2D samplerPerlinPerm2D;
         uniform sampler2D samplerPerlinGrad2D;
 
         struct Input 
         {
             float2 uv_MainTex;
             float3 vert;
         };
 
         void vert(inout appdata_full v, out Input o)
         {
             UNITY_INITIALIZE_OUTPUT(Input,o);
             o.vert = v.vertex.xyz;
             v.vertex.y += 1;
         }
 
         float perlinNoise(float2 p, float seed)
         {
             // Calculate 2D integer coordinates i and fraction p.
             float2 i = floor(p);
             float2 f = p - i;
 
             // Get weights from the coordinate fraction
             float2 w = f * f * f * (f * (f * 6 - 15) + 10);
             float4 w4 = float4(1, w.x, w.y, w.x * w.y);
 
             // Get the four randomly permutated indices from the noise lattice nearest to
             // p and offset these numbers with the seed number.
             float4 perm = tex2D(samplerPerlinPerm2D, i / 256) + seed;
 
             // Permutate the four offseted indices again and get the 2D gradient for each
             // of the four permutated coordinates-seed pairs.
             float4 g1 = tex2D(samplerPerlinGrad2D, perm.xy) * 2 - 1;
             float4 g2 = tex2D(samplerPerlinGrad2D, perm.zw) * 2 - 1;
 
             // Evaluate the four lattice gradients at p
             float a = dot(g1.xy, f);
             float b = dot(g2.xy, f + float2(-1,  0));
             float c = dot(g1.zw, f + float2( 0, -1));
             float d = dot(g2.zw, f + float2(-1, -1));
 
             // Bi-linearly blend between the gradients, using w4 as blend factors.
             float4 grads = float4(a, b - a, c - a, a - b - c + d);
             float n = dot(grads, w4);
 
             // Return the noise value, roughly normalized in the range [-1, 1]
             return n * 1.5;
         }
 
         void surf (Input IN, inout SurfaceOutput o) 
         {
             half4 c = tex2D (_MainTex, IN.uv_MainTex);
             float n = pow(perlinNoise(IN.vert.xz, 5), 2);
             o.Albedo = float3(n, n, n);//c.rgb;
             o.Alpha = c.a;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 

Using two images and the perlin code retrieved from: http://www.decarpentier.nl/scape-procedural-basics However I get some odd looking texture.

Can you please help me as to what I am doing wrong? I don't really understand this. The image generated looks all distorted. I would upload an image but for some reason this editor wont let me.

Thank you,

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

2 People are following this question.

avatar image avatar image

Related Questions

Transparency and texture atlases, shader calls affecting performance? 2 Answers

Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers

Change particle tint color via script 1 Answer

Object space normal map warning the texture is not a normal map 1 Answer

My mesh didn’t appear in Unity 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