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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Scripting textures.

Discussion in 'Shaders' started by unity_qVcVUUYUTy628A, Mar 2, 2018.

?

.

  1. .

    1 vote(s)
    100.0%
  2. .

    0 vote(s)
    0.0%
  1. unity_qVcVUUYUTy628A

    unity_qVcVUUYUTy628A

    Joined:
    Feb 20, 2018
    Posts:
    33
    Hello,
    I have two questions:
    1. Can I script texture inside Shader code (I mean complicated patterns on high resolution texture) or it will be heavy for performance and I should do it somewhere outside and then import to Unity?
    2. If I can do it inside the Shader, how can i access resolution of sampler2D? (For example to create blank sampler with desired resolution to work with)
     
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    314
    You can create procedural textures directly inside pixel (fragment) shader or compute shader. Read about Perlin Noise or just use basic HLSL functions to create own patterns.
     
  3. unity_qVcVUUYUTy628A

    unity_qVcVUUYUTy628A

    Joined:
    Feb 20, 2018
    Posts:
    33
    I just did some google about Perlin Noise and it is exactly what I was looking for, I must dig deeper into it, thank you so much, you saved my life :D
     
  4. unity_qVcVUUYUTy628A

    unity_qVcVUUYUTy628A

    Joined:
    Feb 20, 2018
    Posts:
    33
    This Perlin Noise is kinda "const" value, right? I mean the gradient vectors are always the same, but we can scroll around this grid surface to get different fragment of randomnes, right?
     
  5. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    314
    Short code, powerful effect (you can generate thousands of patterns).
    Code (CSharp):
    1. Shader "FBM generator"
    2. {
    3.     Properties
    4.     {
    5.         _a ("Value1", Range(-10, 10)) = 2.2
    6.         _b ("Value2", Range(-10, 10)) = -2.6
    7.         _c ("Value3", Range(-10, 10)) = -4.3
    8.         _d ("Value4", Range(-10, 10)) = -3.9
    9.         _e ("Value5", Range(-10, 10)) = 1.2
    10.         _f ("Value6", Range(-10, 10)) = -1.8
    11.         _g ("Value7", Range(-10, 10)) = 0.1
    12.         _h ("Value8", Range(-10, 10)) = 3.2
    13.         _color1 ("Color 1", Color) = (0,0,0,1)
    14.         _color2 ("Color 2", Color) = (1,0,0,1)
    15.         _color3 ("Color 3", Color) = (0,0,0,1)
    16.         _color4 ("Color 4", Color) = (1,0,1,1)
    17.         _octaves ("Octaves", Int) = 4
    18.     }
    19.     SubShader
    20.     {
    21.         Pass
    22.         {
    23.             CGPROGRAM
    24.             #pragma vertex vertex_shader
    25.             #pragma fragment pixel_shader
    26.  
    27.             float _a,_b,_c,_d,_e,_f,_g,_h;
    28.             float4 _color1,_color2,_color3,_color4;
    29.             int _octaves;
    30.            
    31.             struct structure
    32.             {
    33.                 float4 vertex:SV_POSITION;
    34.                 float2 uv : TEXCOORD0;
    35.             };
    36.  
    37.             float hash (float2 n)
    38.             {
    39.                 return frac(sin(dot(n,float2(123.456789,987.654321)))*54321.9876 );
    40.             }
    41.  
    42.             float noise(float2 p)
    43.             {
    44.                 float2 i = floor(p);
    45.                 float2 u = smoothstep(0.0,1.0,frac(p));
    46.                 float a = hash(i+float2(0,0));
    47.                 float b = hash(i+float2(1,0));
    48.                 float c = hash(i+float2(0,1));
    49.                 float d = hash(i+float2(1,1));
    50.                 float r = lerp(lerp(a,b,u.x),lerp(c,d,u.x),u.y);
    51.                 return r*r;
    52.             }
    53.  
    54.             float fbm( float2 p ,float2x2 m)
    55.             {
    56.                 float f = 0.0;
    57.                 float d = 0.5;
    58.                 float e = 3.0;
    59.                 for (int i=0;i<_octaves;++i)
    60.                 {
    61.                     f += d*noise(p); p = p*e; p = mul(p,m);
    62.                     d*=0.5; e*=0.95;
    63.                 }
    64.                 return f;
    65.             }
    66.            
    67.             void vertex_shader(float4 vertex:POSITION,float2 uv:TEXCOORD0,out structure vs)
    68.             {
    69.                 vs.vertex = UnityObjectToClipPos(vertex);
    70.                 vs.uv = uv;
    71.             }
    72.  
    73.             void pixel_shader(in structure ps, out float4 color:SV_Target0)
    74.             {
    75.                 float2 uv = float2(2.0*ps.uv.xy-1.0);
    76.                 float2x2 m1 = float2x2(_a,_b,_c,_d);
    77.                 float2x2 m2 = float2x2(_e,_f,_g,_h);
    78.                 float3 t1 = lerp(_color1,_color2,fbm(uv,m1));
    79.                 float3 t2 = lerp(_color3,_color4,fbm(uv,m2));
    80.                 color =  float4(max(t1,t2),1.0);
    81.             }
    82.             ENDCG
    83.         }
    84.     }
    85. }
     
    MadeFromPolygons likes this.
  6. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    314
    hash -> generate pseudorandom value between 0.0 and 1.0;
    noise -> bilinear interpolation;
    fbm -> Fractional Brownian Motion.
     
    MadeFromPolygons likes this.
  7. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,874

    Thank you for this