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
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?
You can't use GLSL in Unity. However, if you have a Mac, you can use a limited subset of GLSL with Quartz Composer.
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)
Here is a shader i've managed to find Code (csharp): Shader "Fast" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "Queue" = "Geometry" } Pass { GLSLPROGRAM #ifdef VERTEX varying vec2 TextureCoordinate; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; TextureCoordinate = gl_MultiTexCoord0.xy; } #endif #ifdef FRAGMENT uniform sampler2D _MainTex; varying vec2 TextureCoordinate; void main() { gl_FragColor = texture2D(_MainTex, TextureCoordinate); } #endif ENDGLSL } } }
Let me tell you a secret - editor on windows can be ran with OpenGL 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
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.
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
Here's Fast+Color, tested on HTC Desire Code (csharp): Shader "FastColor" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _LC("LC", Color) = (1,0,0,0) } SubShader { Tags { "Queue" = "Geometry" } Pass { GLSLPROGRAM #ifdef VERTEX varying vec2 TextureCoordinate; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; TextureCoordinate = gl_MultiTexCoord0.xy; } #endif #ifdef FRAGMENT uniform sampler2D _MainTex; varying vec2 TextureCoordinate; uniform vec4 _LC; void main() { vec4 c; c.xyz = texture2D(_MainTex, TextureCoordinate).xyz * _LC.xyz; c.w = 1.0; gl_FragColor = c; } #endif ENDGLSL } } }
Simple specular, tested on HTC Desire. Still need to do more testing with this one). Code (csharp): Shader "FastSpecular" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _LC("LC", Color) = (1,0,0,0) _LP("LP", Vector) = (1,1,1,1) } SubShader { Tags { "Queue" = "Geometry" } Pass { GLSLPROGRAM #ifdef VERTEX const float SpecularContribution = 0.3; const float DiffuseContribution = 1.0 - SpecularContribution; uniform vec4 _LP; varying vec2 TextureCoordinate; varying float LightIntensity; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; TextureCoordinate = gl_MultiTexCoord0.xy; vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex); vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal); vec3 lightVec = normalize(_LP.xyz - ecPosition); vec3 reflectVec = reflect(-lightVec, tnorm); vec3 viewVec = normalize(-ecPosition); float diffuse = max(dot(lightVec, tnorm), 0.0); float spec = 0.0; if (diffuse > 0.0) { spec = max(dot(reflectVec, viewVec), 0.0); spec = pow(spec, 16.0); } LightIntensity = DiffuseContribution * diffuse + SpecularContribution * spec; } #endif #ifdef FRAGMENT uniform sampler2D _MainTex; varying vec2 TextureCoordinate; uniform vec4 _LC; varying float LightIntensity; void main() { vec4 c; c.xyz = texture2D(_MainTex, TextureCoordinate).xyz * _LC.xyz; c *= LightIntensity; c.w = 1.0; gl_FragColor = c; } #endif ENDGLSL } } }
i've this error message : Shader warning in 'FastSpecular': No subshaders can run on this graphics card
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.
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
Did you just answer to a 4-year old thread to suggest something that was already mentioned multiple times in this thread?