-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPerlinNoiseTexture.cs
70 lines (57 loc) · 2.22 KB
/
PerlinNoiseTexture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Meta Platforms, Inc. and affiliates.
using UnityEngine;
namespace MRMotifs.PassthroughTransitioning
{
public class PerlinNoiseTexture : MonoBehaviour
{
[Header("Texture Resolution")]
[Tooltip("Width of the texture in pixels.")]
[SerializeField]
private int pixWidth = 1024;
[Tooltip("Height of the texture in pixels.")]
[SerializeField]
private int pixHeight = 1024;
[Header("Texture Pattern")]
[Tooltip("The x origin of the sampled area in the plane.")]
[SerializeField]
private float xOrg = 0.2f;
[Tooltip("The y origin of the sampled area in the plane.")]
[SerializeField]
private float yOrg = 0.5f;
[Tooltip("The number of cycles of the basic noise pattern that are repeated over the width and height of the texture.")]
[SerializeField]
private float scale = 10.0f;
private Texture2D m_noiseTex;
private Color[] m_pix;
private Renderer m_rend;
private static readonly int s_noiseTex = Shader.PropertyToID("_NoiseTex");
private void Awake()
{
m_rend = GetComponent<Renderer>();
if (m_rend == null)
{
Debug.LogError("Renderer component missing from this GameObject. Please add a Renderer component.");
return;
}
m_noiseTex = new Texture2D(pixWidth, pixHeight);
m_pix = new Color[m_noiseTex.width * m_noiseTex.height];
CalcNoise();
m_noiseTex.SetPixels(m_pix);
m_noiseTex.Apply();
m_rend.material.SetTexture(s_noiseTex, m_noiseTex);
}
private void CalcNoise()
{
for (var y = 0; y < m_noiseTex.height; y++)
{
for (var x = 0; x < m_noiseTex.width; x++)
{
var xCoord = xOrg + x / (float)m_noiseTex.width * scale;
var yCoord = yOrg + y / (float)m_noiseTex.height * scale;
var sample = Mathf.PerlinNoise(xCoord, yCoord);
m_pix[y * m_noiseTex.width + x] = new Color(sample, sample, sample);
}
}
}
}
}