RaenirSalazar

Untitled

Jan 1st, 2021
1,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4.  
  5. namespace ImprovedPerlinNoiseProject
  6. {
  7.     public class ExampleGPU_2D : MonoBehaviour
  8.     {
  9.  
  10.         public NOISE_STLYE m_stlye = NOISE_STLYE.FBM;
  11.  
  12.         public int m_seed = 0;
  13.  
  14.         public float m_frequency = 1.0f;
  15.  
  16.         public float m_lacunarity = 2.0f;
  17.  
  18.         public float m_gain = 0.5f;
  19.         public Vector2 m_scale = new Vector2(1, 1);
  20.         public Vector2 m_offset = new Vector2(0, 0);
  21.  
  22.         private Renderer m_renderer;
  23.  
  24.         private GPUPerlinNoise m_perlin;
  25.  
  26.         public int width = 1408;
  27.         public int height = 512;
  28.  
  29.         void Start()
  30.         {
  31.             m_perlin = new GPUPerlinNoise(m_seed);
  32.  
  33.             m_perlin.LoadResourcesFor2DNoise();
  34.  
  35.             m_renderer = GetComponent<Renderer>();
  36.  
  37.             m_renderer.material.SetTexture("_PermTable1D", m_perlin.PermutationTable1D);
  38.             m_renderer.material.SetTexture("_Gradient2D", m_perlin.Gradient2D);
  39.  
  40.             if ((float)Screen.width / Screen.height < (float)width / height)
  41.             {
  42.                 Camera.main.orthographicSize = (float)width / 2 * (float)Screen.height / Screen.width;
  43.             }
  44.             else
  45.             {
  46.                 Camera.main.orthographicSize = (float)height / 2;
  47.             }
  48.             Camera.main.orthographicSize = Camera.main.orthographicSize * 10;
  49.         }
  50.  
  51.         public void SaveTextureFromShader()
  52.         {
  53.             RenderTexture renderTexture = RenderTexture.GetTemporary(width, height);
  54.             Graphics.Blit(null, renderTexture, m_renderer.material);
  55.             Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
  56.             texture.filterMode = FilterMode.Bilinear;
  57.             texture.wrapMode = TextureWrapMode.Clamp;
  58.             RenderTexture.active = renderTexture;
  59.             texture.ReadPixels(new Rect(Vector2.zero, new Vector2(width, height)), 0, 0);
  60.             RenderTexture.active = null;
  61.             RenderTexture.ReleaseTemporary(renderTexture);
  62.  
  63.             byte[] bytes = texture.EncodeToPNG();
  64.             File.WriteAllBytes(Application.dataPath + "/Temp/FromShader.png", bytes);
  65.         }
  66.  
  67.         void Update()
  68.         {
  69.             float aspect = (float)width / height;
  70.             m_scale.x = 1;
  71.             m_scale.y = 1;
  72.  
  73.             if (aspect > 1)
  74.             {
  75.                 m_scale.x = aspect;
  76.             }
  77.             else
  78.             {
  79.                 m_scale.y = (float)height / width;
  80.             }
  81.  
  82.             m_renderer.material.SetFloat("_Frequency", m_frequency);
  83.             m_renderer.material.SetFloat("_Lacunarity", m_lacunarity);
  84.             m_renderer.material.SetFloat("_Gain", m_gain);
  85.             m_renderer.material.SetFloat("_NoiseStyle", (float)m_stlye);
  86.             m_renderer.material.SetVector("_Scale", m_scale);
  87.             m_renderer.material.SetVector("_Offset", m_offset);
  88.  
  89.             m_renderer.transform.localScale = new Vector3(width, 1, height);
  90.             if ((float)Screen.width / Screen.height < (float)width / height)
  91.             {
  92.                 Camera.main.orthographicSize = (float)width / 2 * (float)Screen.height / Screen.width;
  93.             }
  94.             else
  95.             {
  96.                 Camera.main.orthographicSize = (float)height / 2;
  97.             }
  98.  
  99.             Camera.main.orthographicSize = Camera.main.orthographicSize * 10;
  100.  
  101.             // Save();
  102.             if (Input.GetKeyDown(KeyCode.Space))
  103.             {
  104.                 SaveTextureFromShader();
  105.             }
  106.         }
  107.  
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment