Skip to content

Commit

Permalink
added all 4 biomes to sphere
Browse files Browse the repository at this point in the history
  • Loading branch information
sharond106 committed Sep 22, 2021
1 parent 3e689b3 commit e468e02
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function main() {
icosphere = new Icosphere(vec3.fromValues(0, 0, 0), 1, prevTesselations);
icosphere.create();
}
renderer.render(camera, test, [
renderer.render(camera, planet, [
icosphere,
//square,
//cube
Expand Down
254 changes: 216 additions & 38 deletions src/shaders/planet-vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ uniform mat4 u_ModelInvTr; // The inverse transpose of the model matrix.
uniform mat4 u_ViewProj; // The matrix that defines the camera's transformation.
// We've written a static matrix for you to use for HW2,
// but in HW3 you'll have to generate one yourself
uniform int u_Time;

in vec4 vs_Pos; // The array of vertex positions passed to the shader

Expand All @@ -30,14 +31,14 @@ out vec4 fs_Nor; // The array of normals that has been transformed by
out vec4 fs_LightVec; // The direction in which our virtual light lies, relative to each vertex. This is implicitly passed to the fragment shader.
out vec4 fs_Col; // The color of each vertex. This is implicitly passed to the fragment shader.

const vec4 lightPos = vec4(2, 0, 3, 1); //The position of our virtual light, which is used to compute the shading of
const vec4 lightPos = vec4(4, 7, 7, 1); //The position of our virtual light, which is used to compute the shading of
//the geometry in the fragment shader.

float random1( vec3 p ) {
return fract(sin((dot(p, vec3(127.1,
311.7,
191.999)))) *
43758.5453);
18.5453);
}

// Returns random vec3 in range [0, 1]
Expand Down Expand Up @@ -77,7 +78,7 @@ float surflet(vec3 p, vec3 corner) {
}

float perlin(vec3 p) {
p = p * 2.5;
p = p * 4.5;
float surfletSum = 0.f;
for (int dx = 0; dx <= 1; dx++) {
for (int dy = 0; dy <= 1; dy++) {
Expand All @@ -86,88 +87,198 @@ float perlin(vec3 p) {
}
}
}
return surfletSum;
return surfletSum / 4.;
}

float perlinAdded(vec4 p) {
float perlinTerrace(vec4 p) {
float noise = perlin(vec3(p)) + .5 * perlin(2.f * vec3(p)) + 0.25 * perlin(4.f * vec3(p));
float rounded = (round(noise * 30.f) / 30.f);
float terrace = (noise + sin(190.*noise + 4.)*.008);
//terrace = rounded;
//terrace *= random1(vec3(terrace));
//noise = mix(noise, terrace, random1(vec3(terrace)));
return terrace;
}

float perlinMountains(vec4 p) {
float noise = perlin(vec3(p)) * 4. + .5 * perlin(2.f * vec3(p)) * 4. + 0.25 * perlin(4.f * vec3(p)) * 4.;
//noise = noise / (1.f + .5 + .25); // this and next line for valleys
//noise = pow(noise, .2);

float terrace = round(noise * 12.f) / 12.f;
noise = mix(noise, terrace, random1(vec3(terrace)));
return noise;
}

vec4 perlinNormal(vec4 p) {
float xNeg = perlinAdded((p + vec4(-.00001, 0, 0, 0)));
float xPos = perlinAdded((p + vec4(.00001, 0, 0, 0)));
float xNeg = perlinTerrace((p + vec4(-.00001, 0, 0, 0)));
float xPos = perlinTerrace((p + vec4(.00001, 0, 0, 0)));
float xDiff = xPos - xNeg;
float yNeg = perlinTerrace((p + vec4(0, -.00001, 0, 0)));
float yPos = perlinTerrace((p + vec4(0, .00001, 0, 0)));
float yDiff = yPos - yNeg;
float zNeg = perlinTerrace((p + vec4(0, 0, -.00001, 0)));
float zPos = perlinTerrace((p + vec4(0, 0, .00001, 0)));
float zDiff = zPos - zNeg;
return vec4(vec3(xDiff, yDiff, zDiff), 0);
}


float fbmRandom( vec3 p ) {
return fract(sin((dot(p, vec3(127.1,
311.7,
191.999)))) *
18.5453);
}

float smootherStep(float a, float b, float t) {
t = t*t*t*(t*(t*6.0 - 15.0) + 10.0);
return mix(a, b, t);
}

float interpNoise3D(float x, float y, float z) {
x *= 2.;
y *= 2.;
z *= 2.;
float intX = floor(x);
float fractX = fract(x);
float intY = floor(y);
float fractY = fract(y);
float intZ = floor(z);
float fractZ = fract(z);
float v1 = fbmRandom(vec3(intX, intY, intZ));
float v2 = fbmRandom(vec3(intX + 1., intY, intZ));
float v3 = fbmRandom(vec3(intX, intY + 1., intZ));
float v4 = fbmRandom(vec3(intX + 1., intY + 1., intZ));

float v5 = fbmRandom(vec3(intX, intY, intZ + 1.));
float v6 = fbmRandom(vec3(intX + 1., intY, intZ + 1.));
float v7 = fbmRandom(vec3(intX, intY + 1., intZ + 1.));
float v8 = fbmRandom(vec3(intX + 1., intY + 1., intZ + 1.));

float i1 = smootherStep(v1, v2, fractX);
float i2 = smootherStep(v3, v4, fractX);
float result1 = smootherStep(i1, i2, fractY);
float i3 = smootherStep(v5, v6, fractX);
float i4 = smootherStep(v7, v8, fractX);
float result2 = smootherStep(i3, i4, fractY);
return smootherStep(result1, result2, fractZ);
}

float fbm(vec4 p, float oct, float freq) {
float total = 0.;
float persistence = 0.5f;
float octaves = oct;
for(float i = 1.; i <= octaves; i++) {
float freq = pow(freq, i);
float amp = pow(persistence, i);
total += interpNoise3D(p.x * freq, p.y * freq, p.z * freq) * amp;
}
return total;
}

float fbm2(vec4 p) {
float total = 0.;
float persistence = 0.5f;
float octaves = 4.;
for(float i = 1.; i <= octaves; i++) {
float freq = pow(2.f, i);
float amp = pow(persistence, i);
total += interpNoise3D(p.x * freq, p.y * freq, p.z * freq) * amp;
}
return total;
}

vec4 fbmNormal(vec4 p, float oct, float freq) {
float xNeg = fbm((p + vec4(-.00001, 0, 0, 0)), oct, freq);
float xPos = fbm((p + vec4(.00001, 0, 0, 0)), oct, freq);
float xDiff = xPos - xNeg;
float yNeg = perlinAdded((p + vec4(0, -.00001, 0, 0)));
float yPos = perlinAdded((p + vec4(0, .00001, 0, 0)));
float yNeg = fbm((p + vec4(0, -.00001, 0, 0)), oct, freq);
float yPos = fbm((p + vec4(0, .00001, 0, 0)), oct, freq);
float yDiff = yPos - yNeg;
float zNeg = perlinAdded((p + vec4(0, 0, -.00001, 0)));
float zPos = perlinAdded((p + vec4(0, 0, .00001, 0)));
float zNeg = fbm((p + vec4(0, 0, -.00001, 0)), oct, freq);
float zPos = fbm((p + vec4(0, 0, .00001, 0)), oct, freq);
float zDiff = zPos - zNeg;
return vec4(vec3(xDiff, yDiff, zDiff), 0);
}

float worley(vec3 p) {
p *= 3.5;
p *= 1.5;
vec3 pInt = floor(p);
vec3 pFract = fract(p);
float minDist = 1.0;
float secondDist = 1.0;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
// if (random1(vec3(x, y, z)) < .6) {
// continue;
// }
vec3 neighbor = vec3(float(x), float(y), float(z));
vec3 voronoi = random3(pInt + neighbor);
//voronoi = 0.5 + 0.5 * sin(0.01 * float(u_Time) + 13.2831 * voronoi);
vec3 diff = neighbor + voronoi - pFract;
float dist = length(diff);
if (dist < minDist) {
secondDist = minDist;
minDist = dist;
} else if (dist < secondDist) {
secondDist = dist;
}
//minDist = min(minDist, dist);
}
}
}
}
//return 1.0 - minDist;
return (-1. * minDist + 1. * secondDist);
}

float worley2(vec3 p) {
vec3 pInt = floor(p);
vec3 pFract = fract(p);
float minDist = 1.0;
float secondDist = 1.0;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
vec3 neighbor = vec3(float(x), float(y), float(z));
vec3 voronoi = random3(pInt + neighbor);
vec3 diff = neighbor + voronoi - pFract;
float dist = length(diff);
if (dist < minDist) {
secondDist = minDist;
minDist = dist;
} else if (dist < secondDist) {
secondDist = dist;
}
}
}
}
return 1.0 - minDist;
}

vec4 worleyAdded(vec4 pos) {
vec3 offset = vec3(pow(worley(vec3(pos)), 2.f)) / vec3(3.0); // Can divide by different factors or not at all for plateus!!!!!!!!!!!!!!

//offset.x = clamp(offset.x, 0.2, .4); // Can change these values!!!!!!!!!!!!!!!!!!
//offset.y = clamp(offset.y, 0.2, .4);
//offset.z = clamp(offset.z, 0.2, .4);
//offset = offset + random3(offset) / vec3(50.0);
vec3 offset = vec3((worley(vec3(pos * 6.)))) / .5; // Can divide by different factors or not at all for plateus!!!!!!!!!!!!!!
offset.x = clamp(offset.x, .1, .15);
offset.y = clamp(offset.y, .1, .15);
offset.z = clamp(offset.z, .1, .15);

// if (offset.x > 0. && offset.y > 0. && offset.z > 0.) {
// offset = vec3(mix(vec3(fbm2(pos)), offset, .85));
// }
offset/=5.;
return vec4(offset, 0.);
}

vec4 worleyNormal(vec4 p) {
float xNeg = worleyAdded((p + vec4(-.00001, 0, 0, 0))).x;
float xPos = worleyAdded((p + vec4(.00001, 0, 0, 0))).x;
float xDiff = (xPos - xNeg);
if (p.x == 0.2) {
xDiff = vs_Nor.x;
}

float yNeg = worleyAdded((p + vec4(0, -.00001, 0, 0))).y;
float yPos = worleyAdded((p + vec4(0, .00001, 0, 0))).y;
float yDiff = (yPos - yNeg);
if (p.y == 0.2) {
yDiff = vs_Nor.y;
}

float zNeg = worleyAdded((p + vec4(0, 0, -.00001, 0))).z;
float zPos = worleyAdded((p + vec4(0, 0, .00001, 0))).z;
float zDiff = (zPos - zNeg);
if (p.z == 0.2) {
zDiff = vs_Nor.z;
}

return (vec4(vec3(xDiff, yDiff, zDiff), 0));
}

Expand All @@ -176,20 +287,87 @@ void main()
fs_Col = vs_Col;
mat3 invTranspose = mat3(u_ModelInvTr);
fs_Nor = vec4(invTranspose * vec3(vs_Nor), 0);

float terrainMap = worley2(vec3(fbm(vs_Pos, 1., 1.2))) * 2. - .5;
vec4 noisePos = vs_Pos;
if (terrainMap < .02 || terrainMap > .97) {
// interpolate between 1 and 4
// terraces
noisePos = vs_Pos + vs_Nor * perlinTerrace(vs_Pos);
} else if (terrainMap < .27) {
// terrain 1
// terraces
noisePos = vs_Pos + vs_Nor * perlinTerrace(vs_Pos);
} else if (terrainMap < .32) {
// interpolate between 1 and 2
// big mountains2
noisePos = vs_Pos + vs_Nor * fbm(vs_Pos, 6., 2.) / 5.;
} else if (terrainMap < .52) {
// terrain 2
// big mountains2
noisePos = vs_Pos + vs_Nor * fbm(vs_Pos, 6., 2.) / 5.;
} else if (terrainMap < .55) {
// interpolate between 2 and 3
// big mountains
noisePos = vs_Pos + vs_Nor * (perlinMountains(vs_Pos * 2.) + fbm(vs_Pos * 2., 6., 2.)) / 3.;
} else if (terrainMap < .7) {
// terrain 3
// big mountains
noisePos = vs_Pos + vs_Nor * (perlinMountains(vs_Pos * 2.) + fbm(vs_Pos * 2., 6., 2.)) / 3.;
} else if (terrainMap < .75) {
// interpolate between 3 and 4
// cracked floor
vec4 worl = worleyAdded(vs_Pos);
noisePos = vs_Pos + vs_Nor * worl;
} else {
// terain 4
// cracked floor
vec4 worl = worleyAdded(vs_Pos);
noisePos = vs_Pos + vs_Nor * worl;
}
// // cracked floor
// vec4 worl = worleyAdded(vs_Pos);
// vec4 worleyNoise = vs_Pos + vs_Nor * worl;

vec4 worleyNoise = vs_Pos + vs_Nor * worleyAdded(vs_Pos);
vec4 perlinNoise = vs_Pos + vs_Nor * perlinAdded(vs_Pos);
// // terraces
// vec4 perlinTerrace = vs_Pos + vs_Nor * perlinTerrace(vs_Pos);

// // big mountains
// vec4 perlinMountains = vs_Pos + vs_Nor * (perlinMountains(vs_Pos * 2.) + fbm(vs_Pos * 2., 6.)) / 2.;
// // big mountains2
// vec4 fbmNoise = vs_Pos + vs_Nor * fbm(vs_Pos, 6.);

// // hills
// vec4 hills = vs_Pos + vs_Nor * mix(fbm2(vs_Pos), worley(vec3(worley(vec3(vs_Pos)))) / 4., .9);

vec4 modelposition = u_Model * worleyNoise;
//vec4 modelposition = u_Model * perlinNoise; // Temporarily store the transformed vertex positions for use below
vec4 modelposition = u_Model * noisePos;
//vec4 modelposition = u_Model * perlinTerrace;
//vec4 modelposition = u_Model * perlinMountains;
//vec4 modelposition = u_Model * (fbmNoise);
fs_Pos = modelposition;

//fs_Nor = normalize(worleyNormal(vs_Pos));
//fs_Nor = perlinNormal(vs_Pos);
//fs_Nor = normalize(perlinNormal(vs_Pos));
//fs_Nor = normalize(fbmNormal(vs_Pos, 6.));
//fs_Nor = vec4(invTranspose * vec3(fs_Nor), 0);
vec3 normal = normalize(normalize(vec3(worleyNormal(vs_Pos))));
vec3 normal = normalize(normalize(vec3(vs_Nor)));
vec3 tangent = normalize(cross(vec3(0.0, 1.0, 0.0), normal));
vec3 bitangent = normalize(cross(normal, tangent));

float xNeg = worleyAdded((vs_Pos + vec4(tangent, 0) * vec4(-.00001, 0, 0, 0))).x;
float xPos = worleyAdded((vs_Pos + vec4(tangent, 0) * vec4(.00001, 0, 0, 0))).x;
float xDiff = (xPos - xNeg);

float yNeg = worleyAdded((vs_Pos + vec4(bitangent, 0) * vec4(0, -.00001, 0, 0))).y;
float yPos = worleyAdded((vs_Pos + vec4(bitangent, 0) * vec4(0, .00001, 0, 0))).y;
float yDiff = (yPos - yNeg);

float zNeg = worleyAdded((vs_Pos + vs_Nor * vec4(0, 0, -.00001, 0))).z;
float zPos = worleyAdded((vs_Pos + vs_Nor * vec4(0, 0, .00001, 0))).z;
float zDiff = (zPos - zNeg);

//fs_Nor = (vec4(vec3(xDiff, yDiff, zDiff), 0));

mat4 transform;
transform[0] = vec4(tangent, 0.0);
transform[1] = vec4(bitangent, 0.0);
Expand Down
Loading

0 comments on commit e468e02

Please sign in to comment.