float sample(Vector2 position) { // Make a crazy big number and do some stuff to it. Then get only // the decimal part of this big number. unchecked { float t = position.X * xSample + position.Y * ySample; t = (1 + Mathf.Sin(t) * 0.5f) * scaler; return(t - Mathf.Truncate(t)); } }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <returns></returns> public static Half Sin(Half x) => (Half)M.Sin(x);
private void GenerateDome(int horizontalLines, int verticalClip, int verticalLines, out Vertex[] verts, out int[] tris) { // This function was a pain to create. It produces a UV sphere. // The top and bottom of the sphere is made using a triangle fan. // The top triangle fan is created followed by the the rest of the // sphere until the verticalClip is reached. Lastly the bottom cap // is added. // Define our vertice and triangle arrays along with our radius, // triangle index (tc) and vertex index (i). verts = new Vertex[((horizontalLines - verticalClip - 1) * verticalLines) + 2]; tris = new int[((((horizontalLines - verticalClip - 1) * verticalLines) * 2)) * 3]; float radius = 1f; int tc = 0; int i = 0; // Create our top triangle fan. for (int m = 0; m < verticalLines; m++) { tris[tc++] = 0; // Middle of fan. tris[tc++] = m + 1; // The next vertex. if (m != 0) { tris[tc++] = m; // The current vertex. } else { tris[tc++] = verticalLines; // The final vertex. } } verts[i++] = new Vertex(Vector3.UnitY * radius, Sky); // We would like to know what the lowest vertex is so we can place // our final bottom vertex at the same y position. float lowestY = float.PositiveInfinity; for (int m = 1; m < horizontalLines - verticalClip; m++) { for (int n = 0; n < verticalLines; n++) { // Create a X, Y and Z position from our m and n values. float x = Mathf.Sin(Mathf.PI * m / horizontalLines) * Mathf.Cos(2 * Mathf.PI * n / verticalLines); float z = Mathf.Sin(Mathf.PI * m / horizontalLines) * Mathf.Sin(2 * Mathf.PI * n / verticalLines); float y = Mathf.Cos(Mathf.PI * m / horizontalLines); // Update the lowest known Y for later. if (y < lowestY) { lowestY = y; } // Don't place triangles down if we are on an edge. if (m < horizontalLines - (verticalClip + 1) && m > 0) { // Standard triangles. if (n < verticalLines - 1) { tris[tc++] = i; tris[tc++] = i + 1; tris[tc++] = i + 1 + verticalLines; tris[tc++] = i; tris[tc++] = i + 1 + verticalLines; tris[tc++] = i + 0 + verticalLines; } else { // Triangles on the edges of each loop. tris[tc++] = i; tris[tc++] = i + 1 - verticalLines; tris[tc++] = i + 1; tris[tc++] = i; tris[tc++] = i + 1; tris[tc++] = i + 0 + verticalLines; } } Vector3 color = new Vector3(255, 255, 0); // Make the ground. if (m == horizontalLines - (verticalClip + 1)) { color = new Vector3(Ground.R, Ground.G, Ground.B); } // Make the sky by interperlating between the sky and horizon. else { float t = Mathf.Pow(1 - y, 3); if (t > 1) { t = 1; } if (t < 0) { t = 0; } color = Vector3.Lerp(new Vector3(Sky.R, Sky.G, Sky.B), new Vector3(Horizon.R, Horizon.G, Horizon.B), t); } // Lastly, create our shiny new vertex and do it all over again. verts[i++] = new Vertex(new Vector3(x, y, z) * radius, color / 255); } } // Finally, create our bottom triangle fan (floor). for (int m = 0; m < verticalLines; m++) { tris[tc] = i; tc++; tris[tc++] = (i - verticalLines) + m; if (m == verticalLines - 1) { tris[tc++] = i - verticalLines; } else { tris[tc++] = (i - verticalLines) + m + 1; } } verts[i++] = new Vertex(Vector3.UnitY * radius * lowestY, Ground); }