#myCode
1 messages · Page 1 of 1 (latest)
#pragma kernel CSMain
#include "noiseSimplex.cginc"
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
struct Data
{
double3 up;
double3 forward;
double3 left;
double3 btmL;
double3 offset;
double3 interval;
double radius;
};
RWStructuredBuffer<float3> _vertices;
RWStructuredBuffer<float2> _uvs;
RWStructuredBuffer<Data> _data;
uniform int _res;
[numthreads(1024,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
int x = id.x % _res;
int y = id.x / _res;
double3 planePos = _data[0].btmL + double3(_data[0].interval.x*x, 0, _data[0].interval.z*y);
double3 p = (planePos.x * _data[0].forward + planePos.z * _data[0].left)/_data[0].radius + _data[0].up;
double x2 = p.x * p.x;
double y2 = p.y * p.y;
double z2 = p.z * p.z;
double3 s;
s.x = p.x * sqrt(1 - y2 / 2 - z2 / 2 + y2 * z2 / 3);
s.y = p.y * sqrt(1 - x2 / 2 - z2 / 2 + x2 * z2 / 3);
s.z = p.z * sqrt(1 - x2 / 2 - y2 / 2 + x2 * y2 / 3);
_uvs[id.x] = planePos;
float val = 1;
float amp = 0.05;
float freq = 1;
int octaves = 15;
for (int i = 0; i < octaves; i++)
{
val += snoise(s.xyz * freq) * amp;
amp /= 2;
freq *= 2;
}
_vertices[id.x] = s *val* _data[0].radius - _data[0].offset;
}
This is the code for the terrain generation, it takes the information where the mesh is located on a 2d plane, and then gets a matrix to transfrom it into a 3d cube, and then transforms it into a sphere
And finally mutiplies the radius with the height of the terrain generation as you can see under the double3s the cube gets transformed to spehrespace requiring sqrt