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. Dismiss Notice

Rewrite GLSL to CG.

Discussion in 'Shaders' started by den.jmpr, Jan 23, 2013.

  1. den.jmpr

    den.jmpr

    Joined:
    Jan 18, 2013
    Posts:
    8
    Hello, my name is Denis. I found some shader in GLSL for Unity and i want to rewrite it in CG.

    This is code:

    Code (csharp):
    1. Shader "Custom/ShaderMB"
    2. {    
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "Queue" = "Geometry" }
    10.         Pass
    11.             {            
    12.                 GLSLPROGRAM                          
    13.                 #ifdef VERTEX  
    14.                
    15.                 varying vec2 the_uv;
    16.                
    17.                 void main()
    18.                 {          
    19.                     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    20.                     the_uv = gl_MultiTexCoord0.st;
    21.                 }
    22.                 #endif  
    23.  
    24.                 #ifdef FRAGMENT
    25.                 #include "UnityCG.glslinc"
    26.                 uniform sampler2D _MainTex;
    27.                 varying vec2 the_uv;
    28.                
    29.                 vec3 deform( in vec2 p, float scale )
    30.                 {
    31.                     vec2 uv;
    32.                    
    33.                     float mtime = scale+_Time.y;
    34.                     float a = atan(p.y,p.x);
    35.                     float r = sqrt(dot(p,p));
    36.                     float s = r * (1.0+0.5*cos(mtime*1.7));
    37.                
    38.                     uv.x = .1*mtime +.05*p.y+.05*cos(-mtime+a*3.0)/s;
    39.                     uv.y = .1*mtime +.05*p.x+.05*sin(-mtime+a*3.0)/s;
    40.                
    41.                     float w = 0.8-0.2*cos(mtime+3.0*a);
    42.                
    43.                     vec3 res = texture2D(_MainTex,uv).xyz*w;
    44.                     return  res*res;
    45.                
    46.                 }
    47.                
    48.                 void main(void)
    49.                 {
    50.                     vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / _ScreenParams.xy;
    51.                     vec3 total = vec3(0.0);
    52.                     float w = 0.0;
    53.                     for( int i=0; i<20; i++ )
    54.                     {
    55.                         vec3 res = deform(p,w);
    56.                         total += res;
    57.                         w += 0.02;
    58.                     }
    59.                     total /= 20.0;
    60.                
    61.                     gl_FragColor = vec4( 3.0*total,1.0);
    62.                 }
    63.            
    64.                 #endif                          
    65.                 ENDGLSL        
    66.             }
    67.      }
    68.     FallBack "Diffuse"
    69. }
    I'm stuck at
    Code (csharp):
    1. vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / _ScreenParams.xy;
    . Could somebody explain me, how it will be in CG?

    Please, help!
     
  2. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    gl_FragCoord = VPOS semantic in cg.

    EDIT: VPOS is for shader model 3.0. If you need it in 2.0, you must calculate it manually and pass it to your fragment shader via a TEXCOORD.
     
    Last edited: Jan 23, 2013
  4. den.jmpr

    den.jmpr

    Joined:
    Jan 18, 2013
    Posts:
    8
    And next question... How to setup VPOS? gl_FragCoord is read-only and already contains the window relative coordinate.
     
  5. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    This link explains how to generate VPOS if semantic itself is not supported.

    EDIT: Check the gamedev link provided at the bottom too.
     
    Last edited: Jan 23, 2013
    twobob likes this.