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 Flafla2 1 · Apr 14, 2011 at 04:42 PM · conversionsyntax-errorbce0044perlin

Unity3d C to Javascript conversion?

Hi, I am working on a game that requires perlin noise, and the code that I found was written in C. Now, I don't know much about C, but it looked fairly similar to JS, so I had my try at converting it. However, I was rewarded with a multitude of errors. Here is the "converted" version. Do you guys know what I did wrong? Here is the code:

generateUniformNoise() { baseNoise[][]; //an array for our uniform noise for(i = 0; i < width; i++) for(j = 0; j < height; j++) //random float between 0 and 1 baseNoise[i][j] = random(); return baseNoise; } generateSmoothNoise(baseNoise, k) { samplePeriod = 1 << k; // calculates 2 ^ k sampleFrequency = 1.0f / samplePeriod; for(i = 0; i < width; i++) { //calculate the horizontal sampling indices sample_i0 = (i / samplePeriod) * samplePeriod; sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around horizontal_blend = (i sample_i0) * sampleFrequency; for(j = 0; j < height; j++) { //calculate the vertical sampling indices sample_j0 = (j / samplePeriod) * samplePeriod; sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around vertical_blend = (j sample_j0) * sampleFrequency; //blend the top two corners top = interpolate(baseNoise[sample_i0][sample_j0], baseNoise[sample_i1][sample_j0], horizontal_blend); //blend the bottom two corners bottom = interpolate(baseNoise[sample_i0][sample_j1], baseNoise[sample_i1][sample_j1], horizontal_blend); //final blend smooth[i][j] = interpolate(top, bottom, vertical_blend); } } return smooth; } interpolate(x0, x1, alpha) //alpha lies between 0 and 1 { return (1 alpha) * x0 + alpha * x1; } generatePerlin(baseNoise, octaveCount) { smooth[]; //an array of 2D arrays containing persistance = 0.5f; //generate smooth noise for(i = 0; i < octaveCount; i++) smooth[i] = generateSmoothNoise(baseNoise, i); perlinNoise[][]; //an array of floats initialised to 0

 amplitude = 1.0f;
 totalAmplitude = 0.0f;
 //blend noise together
 for(k = 0; k &lt; octaveCount; k++)
 {
     amplitude *= persistance;
     totalAmplitude += amplitude;

     for(i = 0; i &lt; width; i++)
         for(j = 0; j &lt; height; j++)
             perlinNoise[i][j] += smooth[k][i][j] * weight;
 }
 //normalisation
 for(k = 0; k &lt; octaveCount; k++)
 {
     perlinNoise[i][j] /= totalAmplitude;
 }
 return perlinNoise;

}

//and here is the half-translated code-

function generateUniformNoise() { var baseNoise; //an array for our uniform noise for(i = 0; i < 100; i++) for(j = 0; j < 100; j++) //random float between 0 and 1 baseNoise[i,j] = Random.Range(0.0,1.0); return baseNoise; } function generateSmoothNoise(baseNoise, k) { var samplePeriod = 1 << k; // calculates 2 ^ k var sampleFrequency = 1.0f / samplePeriod; for(i = 0; i < width; i++) { //calculate the horizontal sampling indices var sample_i0 = (i / samplePeriod) samplePeriod; var sample_i1 = (sample_i0+samplePeriod)%width; //wrap around var horizontal_blend = (isample_i0)sampleFrequency; for(j = 0; j < height; j++) { //calculate the vertical sampling indices var sample_j0 = (j / samplePeriod) samplePeriod; var sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around var vertical_blend = (j sample_j0) sampleFrequency; //blend the top two corners var topc = interpolate(baseNoise[sample_i0][sample_j0], baseNoise[sample_i1][sample_j0], horizontal_blend); //blend the bottom two corners var bottomc = interpolate(baseNoise[sample_i0][sample_j1], baseNoise[sample_i1][sample_j1], horizontal_blend); //final blend smooth[i][j] = interpolate(topc, bottomc, vertical_blend); } } return smooth; }

function interpolate(x0, x1, alpha) //alpha lies between 0 and 1 { return (1 alpha) x0 + alpha x1; }

function generatePerlin(baseNoise, octaveCount) { var smooth[]; //an array of 2D arrays containing var persistance = 0.5f; //generate smooth noise for(i = 0; i < octaveCount; i++) smooth[i] = generateSmoothNoise(baseNoise, i); var perlinNoise[][]; //an array of floats initialised to 0

 var amplitude = 1.0f;
 var totalAmplitude = 0.0f;
 //blend noise together
 for(k = 0; k &lt; octaveCount; k++)
 {
     amplitude *= persistance;
     totalAmplitude += amplitude;

     for(i = 0; i &lt; width; i++)
         for(j = 0; j &lt; height; j++)
             perlinNoise[i][j] += smooth[k][i][j] * weight;
 }
 //normalisation
 for(k = 0; k &lt; octaveCount; k++)
 {
     perlinNoise[i][j] /= totalAmplitude;
 }
 return perlinNoise;

}

Here are the errors I got -


Assets/Terragen_2.0.js(22,52): BCE0044: expecting :, found ')'.
Assets/Terragen_2.0.js(22,53): BCE0044: expecting ), found '*'.
Assets/Terragen_2.0.js(22,54): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Terragen_2.0.js(28,60): BCE0044: expecting :, found ')'.
Assets/Terragen_2.0.js(28,62): BCE0044: expecting ), found '*'.
Assets/Terragen_2.0.js(28,63): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Terragen_2.0.js(45,26): BCE0044: expecting :, found ')'.
Assets/Terragen_2.0.js(45,28): BCE0044: expecting ), found '*'.
Assets/Terragen_2.0.js(45,29): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Terragen_2.0.js(50,19): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Terragen_2.0.js(55,24): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Terragen_2.0.js(55,26): UCE0001: ';' expected. Insert a semicolon at the end.

Comment
Add comment · Show 4
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 Wibbs 1 · Apr 14, 2011 at 04:55 PM 0
Share

What errors are you getting?

avatar image Eric5h5 · Apr 14, 2011 at 06:40 PM 0
Share

Is there any reason why you're converting it? You can use C# functions from JS no problems.

avatar image Wibbs 1 · Apr 14, 2011 at 09:55 PM 0
Share

I think the language is C, not C# - could be wrong though

avatar image Flafla2 1 · Apr 14, 2011 at 10:15 PM 0
Share

It is C, not C#. I got the code from a non-Unity site. I will add the errors now...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Apr 14, 2011 at 10:28 PM

Hi, I am working on a game that requires perlin noise

Use Mathf.PerlinNoise instead? It's undocumented but it accept two floats and return a float noise value. It's deterministic so it will return the same output for same inputs.

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

No one has followed this question yet.

Related Questions

error with script plz help! 2 Answers

error help 2 Answers

OnCollisionEnter - simple question 1 Answer

HELP ME PLEASE!----------------------------!? 1 Answer

scripting problem with if statements 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