Search Unity

GLSL shader

Discussion in 'Shaders' started by Deleted User, Jan 23, 2010.

  1. Deleted User

    Deleted User

    Guest

    Hello guys

    Could anyone please give me a simple GLSL shader that would work in unity?
    It doesn't matter what shader it is, for as long as it is GLSL and works in unity.
    I just need to see the structure of it because I can't seem to find any examples :(

    Thanks
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The top of this page on GLSL in the manual seems to suggest that you shouldn't use it unless you have a Mac-specific application that Cg can't do for you.

    Why do you want to use GLSL and not Cg?
     
  3. Deleted User

    Deleted User

    Guest

    I don't want to use it in my games, I'm just curious to try them. I always like to try new things. :)
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can't use GLSL in Unity. However, if you have a Mac, you can use a limited subset of GLSL with Quartz Composer.
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325

    :?: :?: :?:
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Ahem... shows what I know, then :oops:
     
    CliffracerX likes this.
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you would have to build standalone if you are on windows and then use the force opengl command line argument to start the player up in opengl on windows (neither editor nor webbuild can be forced to opengl so you will not see the effect in the editor / webplayer at any point)
     
  8. robotive

    robotive

    Joined:
    Apr 17, 2009
    Posts:
    59
    Here is a shader i've managed to find :)

    Code (csharp):
    1.  
    2. Shader "Fast" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.    
    7.     SubShader {
    8.         Tags { "Queue" = "Geometry" }
    9.        
    10.         Pass {
    11.             GLSLPROGRAM
    12.            
    13.             #ifdef VERTEX
    14.            
    15.             varying vec2 TextureCoordinate;
    16.            
    17.             void main()
    18.             {
    19.                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    20.                 TextureCoordinate = gl_MultiTexCoord0.xy;
    21.             }
    22.            
    23.             #endif
    24.            
    25.             #ifdef FRAGMENT
    26.                        
    27.             uniform sampler2D _MainTex;
    28.             varying vec2 TextureCoordinate;
    29.            
    30.             void main()
    31.             {
    32.                 gl_FragColor = texture2D(_MainTex, TextureCoordinate);
    33.             }
    34.            
    35.             #endif
    36.            
    37.             ENDGLSL
    38.         }
    39.     }
    40. }
    41.  
     
  9. Tct1856

    Tct1856

    Joined:
    Jan 22, 2010
    Posts:
    104
    Let me tell you a secret - editor on windows can be ran with OpenGL :D

    You just need to run editor itself with -force-opengl, you should see <OpenGL> in the title bar....if succeeded

    This was done so it would be possible to test GLSL shaders in the editor :) Though I think, that mode is a bit buggy, so don't yell if something doesn't work there :)
     
    CliffracerX likes this.
  10. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Thanks so much! There is a dearth of GLSL shaders for Unity. This is a great starting point, so if it proves useful I'll definitely be posting some more interesting shaders.
     
  11. robotive

    robotive

    Joined:
    Apr 17, 2009
    Posts:
    59
    no problems, Daniel

    I'll also post to this thread, when i'll start working on shaders for our game.

    watched a couple of Apple WWDC videos about games optimization... they where doing great things with GLSL shaders, and showed some interesting tips for optimizing shaders. i was really impressed.

    i want to use GLSL shaders for iOS games, to squeeze every last bit of performance :)
     
  12. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Here's Fast+Color, tested on HTC Desire

    Code (csharp):
    1.  
    2. Shader "FastColor" {
    3.         Properties {
    4.                 _MainTex ("Base (RGB)", 2D) = "white" {}
    5.                 _LC("LC", Color) = (1,0,0,0)
    6.         }
    7.        
    8.         SubShader {
    9.                 Tags { "Queue" = "Geometry" }
    10.                
    11.                 Pass {
    12.                    
    13.                         GLSLPROGRAM
    14.                        
    15.                         #ifdef VERTEX
    16.                        
    17.                         varying vec2 TextureCoordinate;
    18.                        
    19.                         void main()
    20.                         {
    21.                                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    22.                                 TextureCoordinate = gl_MultiTexCoord0.xy;
    23.                         }
    24.                        
    25.                         #endif
    26.                        
    27.                         #ifdef FRAGMENT
    28.                                                
    29.                         uniform sampler2D _MainTex;
    30.                         varying vec2 TextureCoordinate;
    31.                         uniform vec4 _LC;
    32.                        
    33.                         void main()
    34.                         {
    35.                                  vec4 c;
    36.                                  c.xyz = texture2D(_MainTex, TextureCoordinate).xyz * _LC.xyz;
    37.                                  c.w = 1.0;
    38.                                  gl_FragColor = c;
    39.                         }
    40.                        
    41.                         #endif
    42.                        
    43.                         ENDGLSL
    44.                 }
    45.         }
    46. }
     
    Last edited: Dec 8, 2010
  13. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Simple specular, tested on HTC Desire.
    Still need to do more testing with this one).

    Code (csharp):
    1. Shader "FastSpecular" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _LC("LC", Color) = (1,0,0,0)
    5.         _LP("LP", Vector) = (1,1,1,1)
    6.     }
    7.    
    8.     SubShader {
    9.         Tags { "Queue" = "Geometry" }
    10.        
    11.         Pass {
    12.            
    13.             GLSLPROGRAM
    14.            
    15.             #ifdef VERTEX
    16.            
    17.             const float SpecularContribution = 0.3;
    18.             const float DiffuseContribution  = 1.0 - SpecularContribution;
    19.  
    20.             uniform vec4 _LP;
    21.             varying vec2 TextureCoordinate;
    22.             varying float LightIntensity;
    23.  
    24.             void main()
    25.             {
    26.                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    27.                 TextureCoordinate = gl_MultiTexCoord0.xy;
    28.                
    29.                 vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
    30.                 vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
    31.                 vec3 lightVec = normalize(_LP.xyz - ecPosition);
    32.                
    33.                 vec3 reflectVec = reflect(-lightVec, tnorm);
    34.                 vec3 viewVec    = normalize(-ecPosition);
    35.                 float diffuse   = max(dot(lightVec, tnorm), 0.0);
    36.                
    37.                 float spec      = 0.0;
    38.                 if (diffuse > 0.0)
    39.                 {
    40.                     spec = max(dot(reflectVec, viewVec), 0.0);
    41.                     spec = pow(spec, 16.0);
    42.                 }
    43.                
    44.                 LightIntensity = DiffuseContribution * diffuse + SpecularContribution * spec;
    45.             }
    46.            
    47.             #endif
    48.            
    49.             #ifdef FRAGMENT
    50.                        
    51.             uniform sampler2D _MainTex;
    52.             varying vec2 TextureCoordinate;
    53.             uniform vec4 _LC;
    54.             varying float LightIntensity;
    55.  
    56.             void main()
    57.             {
    58.                  vec4 c;
    59.                  c.xyz = texture2D(_MainTex, TextureCoordinate).xyz * _LC.xyz;
    60.                  c *= LightIntensity;
    61.                  c.w = 1.0;
    62.                  gl_FragColor = c;
    63.             }
    64.            
    65.             #endif
    66.            
    67.             ENDGLSL
    68.         }
    69.     }
    70. }
     
  14. LumaPxxx

    LumaPxxx

    Joined:
    Oct 3, 2010
    Posts:
    339
    @gnoblin

    thank you so much!
    your shader helped me a lot.
     
  15. khaos

    khaos

    Joined:
    Nov 23, 2009
    Posts:
    7
    i've this error message :

    Shader warning in 'FastSpecular': No subshaders can run on this graphics card

    :(
     
  16. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    I have an error too (the material is pink in the editor), but it works on the device (android).
    I have the same problem like this with all es 2.0 shaders.
     
  17. Internet

    Internet

    Joined:
    Dec 23, 2012
    Posts:
    2
    To solve the pink sphere and "No subshaders can run on this graphics card" issues, you have to force Unity to use OpenGL by creating a shortcut with the following launch parameter, like so:

    "C:\Program Files\Unity\Editor\Unity.exe" -force-opengl
     
  18. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Did you just answer to a 4-year old thread to suggest something that was already mentioned multiple times in this thread? :D :D