IRCSS

CPU Perlin C# bake

Nov 12th, 2018
233
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class bakePerlinNoise : MonoBehaviour {
  6.  
  7.     public Vector2Int resolution = new Vector2Int(128,128);
  8.     public float Tiling = 0.05f;
  9.     public Texture2D perlin;
  10.  
  11.     void Start() { Bake(); }
  12.  
  13.     void Bake()
  14.     {
  15.         perlin = new Texture2D(resolution.x, resolution.y)
  16.         {
  17.             name = "Perlin", anisoLevel = 4, filterMode = FilterMode.Trilinear, wrapMode = TextureWrapMode.Repeat,
  18.         };
  19.         Vector4 Shift = new Vector4(Random.value * 214.21f, Random.value * 21.62f, Random.value * 4.21f, Random.value * 523.21f);
  20.         for (int x = 0; x< resolution.x; x++)
  21.         {
  22.             for(int y = 0; y < resolution.y; y++)
  23.             {
  24.                 Color toSet = new Color(Mathf.PerlinNoise(x* Tiling, y* Tiling),
  25.                     Mathf.PerlinNoise((x + Shift.x)* Tiling, ( y + Shift.y)* Tiling),
  26.                     Mathf.PerlinNoise((x+ Shift.z)* Tiling, ( y + Shift.w) * Tiling));
  27.                 perlin.SetPixel(x, y, toSet);
  28.  
  29.             }
  30.         }
  31.         perlin.Apply();
  32.  
  33.         Shader.SetGlobalTexture("_Perlin", perlin);
  34.     }
  35. }
RAW Paste Data