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 MountDoomTeam · Apr 04, 2013 at 03:20 PM · noiseperlin

easy c# Simplex Noise script code

am i accessing the second script correctly with the first? it returns various errors, it's in Standard Assets folder:

js access code:-----------------------------

 function  equation1 ( vertex: Vector3 ): float
 {
 var n=SimplexNoiseGenerator.noise(vertex.x,vertex.y,vertex.z);
 return n*55; 
 }    

noise code:-----------------------------

 using UnityEngine;
 using System.Collections;
  
 public class SimplexNoiseGenerator : MonoBehaviour {
     private int[] A = new int[3];
     private float s, u, v, w;
     private int i, j, k;
     private float onethird = 0.333333333f;
     private float onesixth = 0.166666667f;
     private int[] T;
     
     public SimplexNoiseGenerator() {
         if (T == null) {
             System.Random rand = new System.Random();
             T = new int[8];
             for (int q = 0; q < 8; q++)
                 T[q] = rand.Next();
         }
     }
     
     public SimplexNoiseGenerator(string seed) {
         T = new int[8];
         string[] seed_parts = seed.Split(new char[] {' '});
         
         for(int q = 0; q < 8; q++) {
             int b;
             try {
                 b = int.Parse(seed_parts[q]);
             } catch {
                 b = 0x0;
             }
             T[q] = b;
         }
     }
     
     public SimplexNoiseGenerator(int[] seed) { // {0x16, 0x38, 0x32, 0x2c, 0x0d, 0x13, 0x07, 0x2a}
         T = seed;
     }
     
     public string GetSeed() {
         string seed = "";
         
         for(int q=0; q < 8; q++) {
             seed += T[q].ToString();
             if(q < 7)
                 seed += " ";
         }
         
         return seed;
     }
     
     public float coherentNoise(float x, float y, float z, int octaves=1, int multiplier = 25, float amplitude = 0.5f, float lacunarity = 2, float persistence = 0.9f) {
         Vector3 v3 = new Vector3(x,y,z)/multiplier;
         float val = 0;
         for (int n = 0; n < octaves; n++) {
           val += noise(v3.x,v3.y,v3.z) * amplitude;
           v3 *= lacunarity;
           amplitude *= persistence;
         }
         return val;
     }
     
     public int getDensity(Vector3 loc) {
         float val = coherentNoise(loc.x, loc.y, loc.z);
         return (int)Mathf.Lerp(0,255,val);
     }
     
     // Simplex Noise Generator
     public float noise(float x, float y, float z) {
         s = (x + y + z) * onethird;
         i = fastfloor(x + s);
         j = fastfloor(y + s);
         k = fastfloor(z + s);
  
         s = (i + j + k) * onesixth;
         u = x - i + s;
         v = y - j + s;
         w = z - k + s;
  
         A[0] = 0; A[1] = 0; A[2] = 0;
  
         int hi = u >= w ? u >= v ? 0 : 1 : v >= w ? 1 : 2;
         int lo = u < w ? u < v ? 0 : 1 : v < w ? 1 : 2;
  
         return kay(hi) + kay(3 - hi - lo) + kay(lo) + kay(0);
     }
  
     float kay(int a) {
         s = (A[0] + A[1] + A[2]) * onesixth;
         float x = u - A[0] + s;
         float y = v - A[1] + s;
         float z = w - A[2] + s;
         float t = 0.6f - x * x - y * y - z * z;
         int h = shuffle(i + A[0], j + A[1], k + A[2]);
         A[a]++;
         if (t < 0) return 0;
         int b5 = h >> 5 & 1;
         int b4 = h >> 4 & 1;
         int b3 = h >> 3 & 1;
         int b2 = h >> 2 & 1;
         int b1 = h & 3;
  
         float p = b1 == 1 ? x : b1 == 2 ? y : z;
         float q = b1 == 1 ? y : b1 == 2 ? z : x;
         float r = b1 == 1 ? z : b1 == 2 ? x : y;
  
         p = b5 == b3 ? -p : p;
         q = b5 == b4 ? -q : q;
         r = b5 != (b4 ^ b3) ? -r : r;
         t *= t;
         return 8 * t * t * (p + (b1 == 0 ? q + r : b2 == 0 ? q : r));
     }
  
     int shuffle(int i, int j, int k) {
         return b(i, j, k, 0) + b(j, k, i, 1) + b(k, i, j, 2) + b(i, j, k, 3) + b(j, k, i, 4) + b(k, i, j, 5) + b(i, j, k, 6) + b(j, k, i, 7);
     }
  
     int b(int i, int j, int k, int B) {
         return T[b(i, B) << 2 | b(j, B) << 1 | b(k, B)];
     }
  
     int b(int N, int B) {
         return N >> B & 1;
     }
     
     int fastfloor(float n) {
         return n > 0 ? (int)n : (int)n - 1;
     }
 }
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
0
Best Answer

Answer by MountDoomTeam · Apr 04, 2013 at 05:38 PM

Lallander says : I run into this problem once in a great while too ("The Script needs to derive from MonoBehaviour!"). And when it happens I just relaunch.

it seems to be almost exactly the same as the unity built-in Perlin noise, in fact the picture shows you some artefacts that appear on both noise versions when I ask for exact values as with a sine function to calculate normals, it gives me nonsense results!, showing they are the same, except that the script above is a bit slower and has 3d I think and many options.

alt text


perlin.jpg (356.0 kB)
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

10 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

Related Questions

How would I go about blending Noise? 1 Answer

Help Perlin Noise for Cubes? 1 Answer

Perlin Noise Issue 0 Answers

What's wrong with this code? Perlin Noise 0 Answers

Perlin noise tutorials ? 2 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