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 MrSplosion · Dec 03, 2011 at 08:21 PM · javascriptconvert

Convert to JavaScript

Could someone please convert this into JavaScript? I don't understand what all these letters mean such as "0xaacff006" or this "(n<<13) | (n>>19)" because JavaScript doesn't have bitwise opperators.

 float Noise2d(int x, int y) {
   unsigned n = x;
   n ^= 0xaacff006;
   n *= 0xdc3deee5;
   n = (n<<13) | (n>>19);
   n += y;
   n ^= 0x0a87317a;
   n *= 0x38656b5e;
   n = (n<<13) | (n>>19);
   n += 0xa6c5636a;
   n *= 0x7d4677b3;
   n = (n<<13) | (n>>19);
   n ^= 0x57a90182;
   n *= 0xe09358ab;
   const float inv_2_31 = 1.0f / 2147483648.0f;
   float res = 1.0f - n * inv_2_31;
   return res;
 }

Thanks!

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 gfr · Dec 03, 2011 at 10:32 PM 1
Share

Don't just ask others to do your work for you, ask about specific problems you have.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Dec 03, 2011 at 11:00 PM

This function translated to Unityscript becomes:

function Noise2d(x: int, y: int): float {
  var n: uint = x;
  n ^= 0xaacff006;
  n *= 0xdc3deee5;
  n = (n<<13) | (n>>19);
  n += y;
  n ^= 0x0a87317a;
  n *= 0x38656b5e;
  n = (n<<13) | (n>>19);
  n += 0xa6c5636a;
  n *= 0x7d4677b3;
  n = (n<<13) | (n>>19);
  n ^= 0x57a90182;
  n *= 0xe09358ab;
  var inv_2_31 = 1.0f / 2147483648.0f;
  var res = 1.0f - n * inv_2_31;
  return res;
}
But there's a big problem: it compiles, but doesn't work! This function gives Overflow Errors in several lines, because the runtime functions check overflow in most operations.
If the values of x and y are relatively small (-32000..32000) the hex constants may be reduced to 16 bits, and with some care they will never overflow. The resultant script is:

function Noise2d(x: int, y: int): float {
  var n: uint = x;
  n ^= 0xf006;
  n *= 0xeee5;
  n = ((n<<13) ^ (n>>19)) & 0xffff;
  n += y;
  n ^= 0x317a;
  n *= 0x6b5e;
  n = ((n<<13) ^ (n>>19)) & 0xffff;
  n += 0x636a;
  n *= 0x77b3;
  n = ((n<<13) ^ (n>>19)) & 0xffff;
  n ^= 0x0182;
  n = (n * 0x58ab) & 0xffff;
  return 1.0 - n / 32768.0;
}
This function works fine, and returns pseudo-random values between -1 and +1.
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 aldonaletto · Dec 04, 2011 at 12:28 PM 0
Share

By the way, things like 0x7d4677b3 are numbers written in hexadecimal, and n<<13 means shift n 13 bits to the left (n>>19 means shift to right, of course). This script apparently was adapted from some microcontroller's C code, where these weird instructions are very frequent.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trouble converting player controller from JS to C# 1 Answer

Script Convert 1 Answer

Trouble with converting some JS code to C# 1 Answer

Convert string to material 0 Answers

I need help with my health script 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