VoxelTerrain.NoiseCube.GetInterpolatedValue C# (CSharp) Method

GetInterpolatedValue() public method

Computes interpolated value of a noise using trilinear interpolation.
public GetInterpolatedValue ( double x, double y, double z ) : float
x double Coordinate along width.
y double Coordinate along height.
z double Coordinate along depth.
return float
        public float GetInterpolatedValue(double x, double y, double z)
        {
            int w = width - 1;
            int h = height - 1;
            int d = depth - 1;

            x = x < 0 ? x % w + w : x % w;
            y = y < 0 ? y % h + h : y % h;
            z = z < 0 ? z % d + d : z % d;

            int ix = (int)x;
            int iy = (int)y;
            int iz = (int)z;

            double dx = x - ix;
            double dy = y - iy;
            double dz = z - iz;

            int ixi = ix + 1;
            int iyi = iy + 1;
            int izi = iz + 1;

            ixi = ixi == width ? 0 : ixi;
            iyi = iyi == height ? 0 : iyi;
            izi = izi == depth ? 0 : izi;

            double c1 = values[ix, iy, iz];
            double c2 = values[ix, iy, izi];
            double c3 = values[ixi, iy, izi];
            double c4 = values[ixi, iy, iz];
            double c5 = values[ix, iyi, iz];
            double c6 = values[ix, iyi, izi];
            double c7 = values[ixi, iyi, izi];
            double c8 = values[ixi, iyi, iz];

            double c14 = c4 * dx + c1 * (1 - dx);
            double c23 = c3 * dx + c2 * (1 - dx);
            double c58 = c8 * dx + c5 * (1 - dx);
            double c67 = c7 * dx + c6 * (1 - dx);

            double c1423 = c23 * dz + c14 * (1 - dz);
            double c5867 = c67 * dz + c58 * (1 - dz);

            double result = c5867 * dy + c1423 * (1 - dy);

            return (float)result;
        }
    }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Generates mesh using some random values interpolated with trilinear interpolation.
        /// Uses coordinates transformed with warp values.
        /// </summary>
        /// <param name="width">Mesh widht.</param>
        /// <param name="height">Mesh height.</param>
        /// <param name="depth">Mesh depth.</param>
        public void GenerateFromNoiseCubeWithWarp(int width, int height, int depth)
        {
            Voxel[, ,] voxels = new Voxel[width, height, depth];

            Vector3 center = new Vector3(width / 2, height / 2, depth / 2);

            NoiseCube n0 = new NoiseCube(16, 16, 16);
            NoiseCube n1 = new NoiseCube(16, 16, 16);
            NoiseCube n2 = new NoiseCube(16, 16, 16);
            NoiseCube n3 = new NoiseCube(16, 16, 16);
            NoiseCube n4 = new NoiseCube(16, 16, 16);

            Parallel.For(0, width, (x) =>
            {
                for (int y = 0; y < height; y++)
                {
                    for (int z = 0; z < depth; z++)
                    {
                        Vector3 position = new Vector3(x, y, z);
                        float weight     = center.Y - y;

                        float warp = n0.GetInterpolatedValue(x * 0.004, y * 0.004, z * 0.004);
                        float cx   = x + warp * 8;
                        float cy   = y + warp * 8;
                        float cz   = z + warp * 8;

                        weight += n1.GetInterpolatedValue(cx / 32.04, cy / 32.01, cz / 31.97) * 64.0f;
                        weight += n2.GetInterpolatedValue(cx / 8.01, cy / 7.96, cz / 7.98) * 4.0f;
                        weight += n3.GetInterpolatedValue(cx / 4.01, cy / 4.04, cz / 3.96) * 2.0f;
                        weight += n4.GetInterpolatedValue(cx / 2.02, cy / 1.98, cz / 1.97) * 1.0f;

                        voxels[x, y, z] = new Voxel()
                        {
                            Position = position,
                            Weight   = weight
                        };
                    }
                }
            });

            ComputeNormal(voxels);
            ComputeAmbient(voxels);
            CreateGeometryBuffer(ComputeTriangles(voxels, container.Settings.LevelOfDetail));
        }
All Usage Examples Of VoxelTerrain.NoiseCube::GetInterpolatedValue