fCraft.Noise.Blend C# (CSharp) Method

Blend() public static method

public static Blend ( [ data1, [ data2, [ blendMap ) : void
data1 [
data2 [
blendMap [
return void
        public static unsafe void Blend( [NotNull] float[,] data1, [NotNull] float[,] data2, [NotNull] float[,] blendMap )
        {
            if ( data1 == null )
                throw new ArgumentNullException( "data1" );
            if ( data2 == null )
                throw new ArgumentNullException( "data2" );
            if ( blendMap == null )
                throw new ArgumentNullException( "blendMap" );
            fixed ( float* ptr1 = data1, ptr2 = data2, ptrBlend = blendMap ) {
                for ( int i = 0; i < data1.Length; i++ ) {
                    ptr1[i] += ptr1[i] * ptrBlend[i] + ptr2[i] * ( 1 - ptrBlend[i] );
                }
            }
        }

Usage Example

        void GenerateHeightmap()
        {
            ReportProgress(10, "Heightmap: Priming");
            heightmap = new float[args.MapWidth, args.MapLength];

            noise.PerlinNoise(heightmap, args.FeatureScale, args.DetailScale, args.Roughness, 0, 0);

            if (args.UseBias && !args.DelayBias)
            {
                ReportProgress(2, "Heightmap: Biasing");
                Noise.Normalize(heightmap);
                ApplyBias();
            }

            Noise.Normalize(heightmap);

            if (args.LayeredHeightmap)
            {
                ReportProgress(10, "Heightmap: Layering");

                // needs a new Noise object to randomize second map
                float[,] heightmap2 = new float[args.MapWidth, args.MapLength];
                new Noise(rand.Next(), NoiseInterpolationMode.Bicubic).PerlinNoise(heightmap2, 0, args.DetailScale, args.Roughness, 0, 0);
                Noise.Normalize(heightmap2);

                // make a blendmap
                blendmap = new float[args.MapWidth, args.MapLength];
                int blendmapDetailSize = (int)Math.Log(Math.Max(args.MapWidth, args.MapLength), 2) - 2;
                new Noise(rand.Next(), NoiseInterpolationMode.Cosine).PerlinNoise(blendmap, 3, blendmapDetailSize, 0.5f, 0, 0);
                Noise.Normalize(blendmap);
                float cliffSteepness = Math.Max(args.MapWidth, args.MapLength) / 6f;
                Noise.ScaleAndClip(blendmap, cliffSteepness);

                Noise.Blend(heightmap, heightmap2, blendmap);
            }

            if (args.MarbledHeightmap)
            {
                ReportProgress(1, "Heightmap: Marbling");
                Noise.Marble(heightmap);
            }

            if (args.InvertHeightmap)
            {
                ReportProgress(1, "Heightmap: Inverting");
                Noise.Invert(heightmap);
            }

            if (args.UseBias && args.DelayBias)
            {
                ReportProgress(2, "Heightmap: Biasing");
                Noise.Normalize(heightmap);
                ApplyBias();
            }
            Noise.Normalize(heightmap);
        }