4 using System.Collections.Generic;
6 using SiliconStudio.Paradox.Effects.Modules;
7 using SiliconStudio.Paradox.Games;
8 using SiliconStudio.Paradox.Graphics;
9 using SiliconStudio.Core.Mathematics;
11 namespace SiliconStudio.
Paradox.Effects
21 static readonly
int[,] GradientCoords =
new int[16, 3]
24 { 0, 1, 1 }, { 0, 1, -1 }, { 0, -1, 1 }, { 0, -1, -1 }, { 1, 0, 1 }, { 1, 0, -1 }, { -1, 0, 1 }, { -1, 0, -1 }, { 1, 1, 0 }, { 1, -1, 0 }, { -1, 1, 0 }, { -1, -1, 0 },
26 { 1, 0, -1 }, { -1, 0, -1 }, { 0, -1, 1 }, { 0, 1, 1 }
29 static readonly byte[,] SimplexCoords4 =
new byte[64, 4]
31 { 0, 64, 128, 192 }, { 0, 64, 192, 128 }, { 0, 0, 0, 0 }, { 0, 128, 192, 64 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 64, 128, 192, 0 }, { 0, 128, 64, 192 },
32 { 0, 0, 0, 0 }, { 0, 192, 64, 128 }, { 0, 192, 128, 64 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 64, 192, 128, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
33 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 64, 128, 0, 192 }, { 0, 0, 0, 0 }, { 64, 192, 0, 128 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
34 { 0, 0, 0, 0 }, { 128, 192, 0, 64 }, { 128, 192, 64, 0 }, { 64, 0, 128, 192 }, { 64, 0, 192, 128 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 128, 0, 192, 64 },
35 { 0, 0, 0, 0 }, { 128, 64, 192, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
36 { 128, 0, 64, 192 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 192, 0, 64, 128 }, { 192, 0, 128, 64 }, { 0, 0, 0, 0 }, { 192, 64, 128, 0 }, { 128, 64, 0, 192 },
37 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 192, 64, 0, 128 }, { 0, 0, 0, 0 }, { 192, 128, 0, 64 }, { 192, 128, 64, 0 }
56 public int Seed {
get; set; }
59 private void ComputePermutations(
int seed, byte[] localPermut)
61 var random =
new Random(seed);
62 var availablePermutations = CreatePermutation(localPermut.Length);
64 for (
int i = 0; i < localPermut.Length; i++)
65 localPermut[i] = this.GetNextValue(random, availablePermutations);
68 private List<byte> CreatePermutation(
int size)
70 var availablePermutations =
new List<byte>(
size);
71 for (
int i = 0; i <
size; i++)
72 availablePermutations.Add((byte)i);
73 return availablePermutations;
76 private byte GetNextValue(Random random, List<byte> permutations)
78 int value = random.Next(0, permutations.Count);
79 byte result = permutations[value];
80 permutations.RemoveAt(value);
88 if (OfflineCompilation)
91 var permutationTable =
new byte[256];
92 this.ComputePermutations(Seed, permutationTable);
94 var pixels =
new byte[256 * 256 * 4];
95 for (
int i = 0; i < 256; i++)
97 for (
int j = 0; j < 256; j++)
99 int offset = (i * 256 + j) * 4;
100 var value = (byte)permutationTable[(j + permutationTable[i]) & 0xFF];
101 pixels[offset] = (byte)(GradientCoords[value & 0x0F, 0] * 64 + 64);
102 pixels[offset + 1] = (byte)(GradientCoords[value & 0x0F, 1] * 64 + 64);
103 pixels[offset + 2] = (byte)(GradientCoords[value & 0x0F, 2] * 64 + 64);
104 pixels[offset + 3] = value;
108 fixed (
void* ptr = SimplexCoords4)
109 this.Parameters.Set(SimplexNoiseKeys.SimplexTexture, Texture2D.New(
GraphicsDevice, 64, 1, 1, PixelFormat.R8G8B8A8_UNorm,
new [] {
new DataBox((IntPtr)ptr, 64 * 4, 0) }));
112 NoiseBaseKeys.PermTexture, Texture2D.New(GraphicsDevice, 256, 256, PixelFormat.R8G8B8A8_UNorm, pixels));
117 this.Parameters.Remove(SimplexNoiseKeys.SimplexTexture);
118 this.Parameters.Remove(NoiseBaseKeys.PermTexture);
Plugin used to render to a GBuffer from a MainPlugin.
NoisePlugin(string name)
Initializes a new instance of the NoisePlugin class.
NoisePlugin()
Initializes a new instance of the NoisePlugin class.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
unsafe override void Initialize()
Provides access to data organized in 3D.
_In_ size_t _In_ size_t size