Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved How Do I smooth Only Part Of The Terrain With A Shader?

Discussion in 'Scripting' started by theonerm2_unity, Mar 20, 2022.

  1. theonerm2_unity

    theonerm2_unity

    Joined:
    Sep 7, 2018
    Posts:
    130


    There's a video of what it does now.

    I want to make it so it only smooths part of it.

    my code for the c# script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. [ExecuteInEditMode]
    6. public class SmoothTheTerrainViaShader : MonoBehaviour
    7. {
    8.    public Material blurMat;
    9.     public bool smooth;
    10.     public int iterations;
    11.    public RenderTexture texture;
    12.     public Terrain terrain1;
    13.     // Update is called every frame, if the MonoBehaviour is enabled
    14.  
    15.     private void Update()
    16.     {
    17.         if (smooth)
    18.         {
    19.             ApplyBlur(Vector2Int.zero, terrain1);
    20.         }
    21.     }
    22.  
    23.  
    24.  
    25.  
    26.     public void ApplyBlur(Vector2Int destiny, Terrain terrain)
    27.     {
    28.         RenderTexture source = terrain.terrainData.heightmapTexture;
    29.         RenderTexture destination = terrain.terrainData.heightmapTexture;
    30.         Debug.Log(destiny);
    31.         Debug.Log(new Vector2(source.width, source.height));
    32.         for (int i = 0; i < iterations; i++)
    33.         {
    34.            
    35.  
    36.             Material material = blurMat;
    37.             material.SetFloat("_BlurSize", 0.016f);
    38.             material.SetInt("_Samples", 1);
    39.             material.SetFloat("_Gauss", 0);
    40.             material.SetFloat("_StandardDeviation", 0.3f);
    41.  
    42.            
    43.             var temporaryTexture = RenderTexture.GetTemporary(source.width, source.height);
    44.             temporaryTexture.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R16_UNorm;
    45.             Graphics.Blit(source, texture, new Vector2(1f, 1f), new Vector2(1,1));
    46.  
    47.             Graphics.Blit(source, temporaryTexture, material, 0);
    48.             Graphics.Blit(temporaryTexture, destination, material, 1);
    49.             RenderTexture.active = temporaryTexture;
    50.             RectInt rect = new RectInt(new Vector2Int(0, 0), new Vector2Int(source.width, source.height));
    51.    
    52.  
    53.  
    54.             terrain.terrainData.CopyActiveRenderTextureToHeightmap(rect, new Vector2Int(0,0), TerrainHeightmapSyncControl.HeightOnly);
    55.             RenderTexture.ReleaseTemporary(temporaryTexture);
    56.  
    57.         }
    58.     }
    59.  
    60.  
    61.  
    62. }
    63.  
    The shader I'm using.

    Code (CSharp):
    1. Shader "Ricky/Terrain/Blur"{
    2.     //show values to edit in inspector
    3.     Properties{
    4.         [HideInInspector] _MainTex("Texture", 2D) = "white" {}
    5.  
    6.         _BlurSize("Blur Size", Range(0,0.5)) = 0
    7.         [KeywordEnum(Low, Medium, High)] _Samples("Sample amount", Float) = 0
    8.         [Toggle(GAUSS)] _Gauss("Gaussian Blur", float) = 0
    9.         [PowerSlider(3)]_StandardDeviation("Standard Deviation (Gauss only)", Range(0.00, 0.3)) = 0.02
    10.     }
    11.  
    12.         SubShader{
    13.             // markers that specify that we don't need culling
    14.             // or reading/writing to the depth buffer
    15.             Cull Off
    16.             ZWrite Off
    17.             ZTest Always
    18.  
    19.  
    20.             //Vertical Blur
    21.             Pass{
    22.                 CGPROGRAM
    23.                 //include useful shader functions
    24.                 #include "UnityCG.cginc"
    25.  
    26.                 //define vertex and fragment shader
    27.                 #pragma vertex vert
    28.                 #pragma fragment frag
    29.  
    30.                 #pragma multi_compile _SAMPLES_LOW _SAMPLES_MEDIUM _SAMPLES_HIGH
    31.                 #pragma shader_feature GAUSS
    32.  
    33.                 //texture and transforms of the texture
    34.                 sampler2D _MainTex;
    35.                 float _BlurSize;
    36.                 float _StandardDeviation;
    37.  
    38.                 #define PI 3.14159265359
    39.                 #define E 2.71828182846
    40.  
    41.             #if _SAMPLES_LOW
    42.                 #define SAMPLES 10
    43.             #elif _SAMPLES_MEDIUM
    44.                 #define SAMPLES 30
    45.             #else
    46.                 #define SAMPLES 100
    47.             #endif
    48.  
    49.                 //the object data that's put into the vertex shader
    50.                 struct appdata {
    51.                     float4 vertex : POSITION;
    52.                     float2 uv : TEXCOORD0;
    53.                 };
    54.  
    55.                 //the data that's used to generate fragments and can be read by the fragment shader
    56.                 struct v2f {
    57.                     float4 position : SV_POSITION;
    58.                     float2 uv : TEXCOORD0;
    59.                 };
    60.  
    61.                 //the vertex shader
    62.                 v2f vert(appdata v) {
    63.                     v2f o;
    64.                     //convert the vertex positions from object space to clip space so they can be rendered
    65.                     o.position = UnityObjectToClipPos(v.vertex);
    66.                     o.uv = v.uv;
    67.                     return o;
    68.                 }
    69.  
    70.                 //the fragment shader
    71.                 fixed4 frag(v2f i) : SV_TARGET{
    72.                 #if GAUSS
    73.                     //failsafe so we can use turn off the blur by setting the deviation to 0
    74.                     if (_StandardDeviation == 0)
    75.                     return tex2D(_MainTex, i.uv);
    76.                 #endif
    77.                 //init color variable
    78.                 float4 col = 0;
    79.                 float4 Ocol = 0;
    80.  
    81.             #if GAUSS
    82.                 float sum = 0;
    83.             #else
    84.                 float sum = SAMPLES;
    85.             #endif
    86.                 //iterate over blur samples
    87.                 for (float index = 0; index < SAMPLES; index++) {
    88.                     //get the offset of the sample
    89.                     float offset = (index / (SAMPLES - 1) - 0.5) * _BlurSize;
    90.                     //get uv coordinate of sample
    91.                     float2 uv = i.uv + float2(0, offset);
    92.                 #if !GAUSS
    93.                     //simply add the color if we don't have a gaussian blur (box)
    94.                     col += tex2D(_MainTex, uv);
    95.                 #else
    96.                     //calculate the result of the gaussian function
    97.                     float stDevSquared = _StandardDeviation * _StandardDeviation;
    98.                     float gauss = (1 / sqrt(2 * PI * stDevSquared)) * pow(E, -((offset * offset) / (2 * stDevSquared)));
    99.                     //add result to sum
    100.                     sum += gauss;
    101.                     //multiply color with influence from gaussian function and add it to sum color
    102.                     col += tex2D(_MainTex, uv) * gauss;
    103.                 #endif
    104.                 }
    105.                 //divide the sum of values by the amount of samples
    106.                
    107.                     col = col / sum;
    108.                     return col;
    109.                
    110.  
    111.  
    112.             }
    113.  
    114.             ENDCG
    115.         }
    116.  
    117.             //Horizontal Blur
    118.             Pass{
    119.                 CGPROGRAM
    120.                 //include useful shader functions
    121.                 #include "UnityCG.cginc"
    122.  
    123.                 #pragma multi_compile _SAMPLES_LOW _SAMPLES_MEDIUM _SAMPLES_HIGH
    124.                 #pragma shader_feature GAUSS
    125.  
    126.                 //define vertex and fragment shader
    127.                 #pragma vertex vert
    128.                 #pragma fragment frag
    129.  
    130.                 //texture and transforms of the texture
    131.                 sampler2D _MainTex;
    132.                 float _BlurSize;
    133.                 float _StandardDeviation;
    134.  
    135.                 #define PI 3.14159265359
    136.                 #define E 2.71828182846
    137.  
    138.             #if _SAMPLES_LOW
    139.                 #define SAMPLES 10
    140.             #elif _SAMPLES_MEDIUM
    141.                 #define SAMPLES 30
    142.             #else
    143.                 #define SAMPLES 100
    144.             #endif
    145.  
    146.                 //the object data that's put into the vertex shader
    147.                 struct appdata {
    148.                     float4 vertex : POSITION;
    149.                     float2 uv : TEXCOORD0;
    150.                 };
    151.  
    152.                 //the data that's used to generate fragments and can be read by the fragment shader
    153.                 struct v2f {
    154.                     float4 position : SV_POSITION;
    155.                     float2 uv : TEXCOORD0;
    156.                 };
    157.  
    158.                 //the vertex shader
    159.                 v2f vert(appdata v) {
    160.                     v2f o;
    161.                     //convert the vertex positions from object space to clip space so they can be rendered
    162.                     o.position = UnityObjectToClipPos(v.vertex);
    163.                     o.uv = v.uv;
    164.                     return o;
    165.                 }
    166.  
    167.                 //the fragment shader
    168.                 fixed4 frag(v2f i) : SV_TARGET{
    169.                 #if GAUSS
    170.                     //failsafe so we can use turn off the blur by setting the deviation to 0
    171.                     if (_StandardDeviation == 0)
    172.                     return tex2D(_MainTex, i.uv);
    173.                 #endif
    174.                 //calculate aspect ratio
    175.                 float invAspect = _ScreenParams.y / _ScreenParams.x;
    176.                 //init color variable
    177.                 float4 col = 0;
    178.                 float4 Ocol = 0;
    179.             #if GAUSS
    180.                 float sum = 0;
    181.             #else
    182.                 float sum = SAMPLES;
    183.             #endif
    184.                 //iterate over blur samples
    185.                 for (float index = 0; index < SAMPLES; index++) {
    186.                     //get the offset of the sample
    187.                     float offset = (index / (SAMPLES - 1) - 0.5) * _BlurSize * invAspect;
    188.                     //get uv coordinate of sample
    189.                     float2 uv = i.uv + float2(offset, 0);
    190.  
    191.                 #if !GAUSS
    192.                     //simply add the color if we don't have a gaussian blur (box)
    193.                     col += tex2D(_MainTex, uv);
    194.                 #else
    195.                     //calculate the result of the gaussian function
    196.                     float stDevSquared = _StandardDeviation * _StandardDeviation;
    197.                     float gauss = (1 / sqrt(2 * PI * stDevSquared)) * pow(E, -((offset * offset) / (2 * stDevSquared)));
    198.                     //add result to sum
    199.                     sum += gauss;
    200.                     //multiply color with influence from gaussian function and add it to sum color
    201.                    
    202.                     col += tex2D(_MainTex, uv) * gauss;
    203.                 #endif
    204.                 }
    205.                 //divide the sum of values by the amount of samples
    206.                
    207.                     col = col / sum;
    208.                     return col;
    209.                
    210.             }
    211.  
    212.             ENDCG
    213.         }
    214.         }
    215. }
    Any help would be great. I can't find much about this on google or anything. And it's all new to me.
     
  2. theonerm2_unity

    theonerm2_unity

    Joined:
    Sep 7, 2018
    Posts:
    130
    I did it


    Thanks for all your guy's help.