Skip to content

Commit

Permalink
Small bugfix + Worley support added
Browse files Browse the repository at this point in the history
  • Loading branch information
matdudq committed Apr 21, 2022
1 parent 3aa3836 commit 1affcb9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions Editor/Utilities/FolderReferencePropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
path = "Assets" + path.Substring(Application.dataPath.Length);
folderAsset = AssetDatabase.LoadAssetAtPath(path, typeof(DefaultAsset));
guidSP.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(folderAsset));
property.serializedObject.ApplyModifiedProperties();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Tool was created to:
- Separate window for Noise Editor, which consists of Noise texture chanel preview and editor itself.
- Possibility to define noises with combined patterns and settings.
- Possibility to define 1/2/3-dimentional noises.
- Possibility to define noises of default, value and perlin type.
- Possibility to define noises of default, value, perlin and worley type.
- Possibility to manipulate of noise space (position/rotation/frequency).
- Possibility to define noise with custom octaves settings.
- Possibility to add custom patterns like wood or turbulence.
Expand Down
17 changes: 16 additions & 1 deletion Runtime/NoiseTextureJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ private class NoiseSettingsWithChannel
private GenerateDefaultNoiseMapJob defaultNoiseMapJob;
private GenerateValueNoiseMapJob valueNoiseMapJob;
private GeneratePerlinNoiseMapJob perlinNoiseMapJob;

private GenerateWorleyNoiseMapJob worleyNoiseMapJob;

private NativeArray<Color> texturePixels;
private int resolution;

Expand Down Expand Up @@ -116,6 +117,9 @@ private JobHandle ScheduleGenerateJobOfType(NoiseType noiseType, MonoBehaviour c
case NoiseType.Perlin:
return perlinNoiseMapJob.ScheduleAsync(pixelsCount, batchCount, context, onComplete);
break;
case NoiseType.Worley:
return worleyNoiseMapJob.ScheduleAsync(pixelsCount, batchCount, context, onComplete);
break;
default:
throw new ArgumentOutOfRangeException(nameof(noiseType), noiseType, null);
}
Expand All @@ -137,6 +141,11 @@ private void CreateJobs(NativeArray<Color> pixels)
{
noiseTextureData = pixels
};

worleyNoiseMapJob = new GenerateWorleyNoiseMapJob()
{
noiseTextureData = pixels
};
}

private void UpdateJobData(NoiseSettings noiseSettings, NoiseTextureChannel noiseTextureChannel)
Expand Down Expand Up @@ -169,6 +178,9 @@ private void UpdateJobData(NoiseSettings noiseSettings, NoiseTextureChannel nois
case NoiseType.Perlin:
perlinNoiseMapJob.data = generateNoiseMapJobData;
break;
case NoiseType.Worley:
worleyNoiseMapJob.data = generateNoiseMapJobData;
break;
}
}

Expand Down Expand Up @@ -291,6 +303,9 @@ private void ScheduleGenerateJobOfType(NoiseType noiseType, UnityEngine.Object c
case NoiseType.Perlin:
perlinNoiseMapJob.ScheduleEditorAsync(pixelsCount, batchCount, context, onComplete);
break;
case NoiseType.Worley:
worleyNoiseMapJob.ScheduleEditorAsync(pixelsCount, batchCount, context, onComplete);
break;
default:
throw new ArgumentOutOfRangeException(nameof(noiseType), noiseType, null);
}
Expand Down
82 changes: 82 additions & 0 deletions Runtime/NoiseTextureJobManager_GenerateWorleyNoiseMapJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

namespace DudeiNoise
{
internal partial class NoiseTextureJobManager
{
[BurstCompile]
private struct GenerateWorleyNoiseMapJob : IJobParallelFor
{
#region Variables

#region JobData

[ReadOnly]
public GenerateNoiseMapJobData data;

public NativeArray<Color> noiseTextureData;

#endregion JobData

#endregion Variables

#region Public methods

public void Execute(int index)
{
int2 currentLocal = new int2(index % data.resolution, (int)math.floor(index / (float) data.resolution));
Matrix4x4 noiseTRS = float4x4.TRS(data.positionOffset, quaternion.Euler(data.rotationOffset), data.scaleOffset);

float3 point00 = noiseTRS.MultiplyPoint3x4(new float3(-0.5f, -0.5f, 0.0f));
float3 point10 = noiseTRS.MultiplyPoint3x4(new float3(0.5f, -0.5f, 0.0f));
float3 point01 = noiseTRS.MultiplyPoint3x4(new float3(-0.5f, 0.5f, 0.0f));
float3 point11 = noiseTRS.MultiplyPoint3x4(new float3(0.5f, 0.5f, 0.0f));

float stepSize = 1.0f / (data.resolution - 1);

float3 point0 = math.lerp(point00, point01, currentLocal.y * stepSize);
float3 point1 = math.lerp(point10, point11, currentLocal.y * stepSize);

float3 currentPoint = math.lerp(point0, point1, currentLocal.x * stepSize);

float currentSample = noise.cellular(currentPoint).x;
float turbulenceSample = data.turbulenceEnabled ? math.abs(currentSample) : currentSample;
float sum = turbulenceSample;

float amplitude = 1f;
float range = amplitude;

for (int i = 1; i < data.octaves; i++)
{
currentPoint *= data.lacunarity;
currentSample = noise.cellular(currentPoint).x;
turbulenceSample = data.turbulenceEnabled ? math.abs(currentSample) : currentSample;

amplitude *= data.persistence;
range += amplitude;
sum += turbulenceSample * amplitude;
}

float sample = sum / range;
float sampleWithCustomPattern = WoodEffect(sample, data.woodPatternMultiplier);
float probe = sampleWithCustomPattern;

if (data.falloffEnabled)
{
probe -= GetFalloffProbe(currentLocal.x, currentLocal.y, data.falloffDensity, data.falloffShift, data.resolution);
probe = math.clamp(probe, 0, 1);
}

Color currentColor = noiseTextureData[index];
currentColor[(int) data.channelToOverride] = probe;
noiseTextureData[index] = currentColor;
}

#endregion Public methods
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Runtime/NoiseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public enum NoiseType

Default,
Value,
Perlin
Perlin,
Worley

#endregion Values
}
Expand Down

0 comments on commit 1affcb9

Please sign in to comment.