- Home /
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 < octaveCount; k++)
{
amplitude *= persistance;
totalAmplitude += amplitude;
for(i = 0; i < width; i++)
for(j = 0; j < height; j++)
perlinNoise[i][j] += smooth[k][i][j] * weight;
}
//normalisation
for(k = 0; k < 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 < octaveCount; k++)
{
amplitude *= persistance;
totalAmplitude += amplitude;
for(i = 0; i < width; i++)
for(j = 0; j < height; j++)
perlinNoise[i][j] += smooth[k][i][j] * weight;
}
//normalisation
for(k = 0; k < 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.
Is there any reason why you're converting it? You can use C# functions from JS no problems.
I think the language is C, not C# - could be wrong though
It is C, not C#. I got the code from a non-Unity site. I will add the errors now...
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.
Your answer
Follow this Question
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