Hi I was wondering how I could make it so a Texture 2D being made by perlin noise so that it is densest near the middle and thins out toward the edges?
Here is a picture of what its like now.
Ive been trying to do this but how would I modify the amount of perlin?
Here is a very basic example for modifying the amount of noise applied to a texture using a square-border method. If the pixel coordinate is within a border, the perlin value is modified based on its distance from the edge i.e. closer to the edge, apply less perlin.
Basically you calculate a modifier that is a percentage between the position and the border.
if pos < border, percentage = pos / border
if pos > size - border, percentage = ( border - ( pos - ( size - border ) ) ) / border
In a new scene, attach the script to an empty gameObject. Create a plane, drag the plane into myRenderer. Hit Play. While running, change the value of border in the inspector, then left-click in the scene to see the result.
PerlinBorderFade.cs :
using UnityEngine;
using System.Collections;
public class PerlinBorderFade : MonoBehaviour
{
public Renderer myRenderer;
public int size = 512;
public int border = 64;
public float perlinScale = 12.34f;
void Start()
{
GenerateTexture();
}
void Update()
{
if ( Input.GetMouseButtonDown( 0 ) )
{
GenerateTexture();
}
}
void GenerateTexture()
{
if ( border * 2 > size )
border = size / 2;
if ( border < 0 )
border = 0;
Color[] textureColours = new Color[ size * size ];
for ( int y = 0; y < size; y++ )
{
for ( int x = 0; x < size; x++ )
{
float modifier = 1f;
if ( x < border )
{
modifier *= CalculatePercentage( x );
}
else if ( x > size - border )
{
modifier *= CalculatePercentage( border - ( x - ( size - border ) ) );
}
if ( y < border )
{
modifier *= CalculatePercentage( y );
}
else if ( y > size - border )
{
modifier *= CalculatePercentage( border - ( y - ( size - border ) ) );
}
float perlinValue = EvaluatePerlinNoise( x, y );
perlinValue *= modifier;
textureColours[ (y * size) + x ] = GetColour( perlinValue );
}
}
Texture2D texture = new Texture2D( size, size );
texture.SetPixels( textureColours );
texture.Apply();
if ( myRenderer )
{
myRenderer.material.mainTexture = texture;
}
}
float CalculatePercentage( int offset )
{
return (float)offset / (float)border;
}
float EvaluatePerlinNoise( int x, int y )
{
return Mathf.PerlinNoise(
( (float)x / (float)size ) * perlinScale,
( (float)y / (float)size ) * perlinScale
);
}
Color GetColour( float val )
{
return new Color( val, val, val );
}
}