Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NoisePlugin.cs
Go to the documentation of this file.
1 // Copyright (c) 2011 Silicon Studio
2 
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using SiliconStudio.Paradox.Effects.Modules;
7 using SiliconStudio.Paradox.Games;
8 using SiliconStudio.Paradox.Graphics;
9 using SiliconStudio.Core.Mathematics;
10 
11 namespace SiliconStudio.Paradox.Effects
12 {
13  /// <summary>
14  /// Plugin used to render to a GBuffer from a MainPlugin.
15  /// </summary>
16  /// <remarks>
17  /// This plugin depends on <see cref="MainPlugin"/> parameters.
18  /// </remarks>
20  {
21  static readonly int[,] GradientCoords = new int[16, 3]
22  {
23  // 12 cube edges
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 },
25  // 4 more to make 16
26  { 1, 0, -1 }, { -1, 0, -1 }, { 0, -1, 1 }, { 0, 1, 1 }
27  };
28 
29  static readonly byte[,] SimplexCoords4 = new byte[64, 4]
30  {
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 }
38  };
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="NoisePlugin"/> class.
42  /// </summary>
43  public NoisePlugin() : this("Noise")
44  {
45  }
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="NoisePlugin"/> class.
49  /// </summary>
50  /// <param name="name">The name.</param>
51  public NoisePlugin(string name)
52  : base(name)
53  {
54  }
55 
56  public int Seed { get; set; }
57 
58 
59  private void ComputePermutations(int seed, byte[] localPermut)
60  {
61  var random = new Random(seed);
62  var availablePermutations = CreatePermutation(localPermut.Length);
63 
64  for (int i = 0; i < localPermut.Length; i++)
65  localPermut[i] = this.GetNextValue(random, availablePermutations);
66  }
67 
68  private List<byte> CreatePermutation(int size)
69  {
70  var availablePermutations = new List<byte>(size);
71  for (int i = 0; i < size; i++)
72  availablePermutations.Add((byte)i);
73  return availablePermutations;
74  }
75 
76  private byte GetNextValue(Random random, List<byte> permutations)
77  {
78  int value = random.Next(0, permutations.Count);
79  byte result = permutations[value];
80  permutations.RemoveAt(value);
81  return result;
82  }
83 
84  public unsafe override void Initialize()
85  {
86  base.Initialize();
87 
88  if (OfflineCompilation)
89  return;
90 
91  var permutationTable = new byte[256];
92  this.ComputePermutations(Seed, permutationTable);
93 
94  var pixels = new byte[256 * 256 * 4];
95  for (int i = 0; i < 256; i++)
96  {
97  for (int j = 0; j < 256; j++)
98  {
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); // Gradient x
102  pixels[offset + 1] = (byte)(GradientCoords[value & 0x0F, 1] * 64 + 64); // Gradient y
103  pixels[offset + 2] = (byte)(GradientCoords[value & 0x0F, 2] * 64 + 64); // Gradient z
104  pixels[offset + 3] = value; // Permuted index
105  }
106  }
107 
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) }));
110 
111  this.Parameters.Set(
112  NoiseBaseKeys.PermTexture, Texture2D.New(GraphicsDevice, 256, 256, PixelFormat.R8G8B8A8_UNorm, pixels));
113  }
114 
115  protected override void Destroy()
116  {
117  this.Parameters.Remove(SimplexNoiseKeys.SimplexTexture);
118  this.Parameters.Remove(NoiseBaseKeys.PermTexture);
119 
120  base.Destroy();
121  }
122  }
123 }
Plugin used to render to a GBuffer from a MainPlugin.
Definition: NoisePlugin.cs:19
NoisePlugin(string name)
Initializes a new instance of the NoisePlugin class.
Definition: NoisePlugin.cs:51
NoisePlugin()
Initializes a new instance of the NoisePlugin class.
Definition: NoisePlugin.cs:43
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.
Provides access to data organized in 3D.
Definition: DataBox.cs:12
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175