Skip to content

Commit f6807d0

Browse files
StarwasterStarwaster
authored andcommitted
Adding shaders
- Attempting to replace missing shaders. - Additional error handling when loading shaders
1 parent 7296471 commit f6807d0

13 files changed

Lines changed: 706 additions & 16 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,5 @@ pip-log.txt
212212
*.mo
213213

214214
#Mr Developer
215-
.mr.developer.cfg
215+
.mr.developer.cfg
216+
Reflection-Plugin-Continued.userprefs

Reflection-Plugin-Continued.csproj

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,42 @@
4545
<Compile Include="ReflectiveShaderModule.cs" />
4646
</ItemGroup>
4747
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
48+
<ItemGroup>
49+
<EmbeddedResource Include="Resources\Reflect-BumpNolight.shader">
50+
<Link>Reflect-BumpNolight.shader</Link>
51+
<LogicalName>Reflective.Bumped_Unlit</LogicalName>
52+
</EmbeddedResource>
53+
<EmbeddedResource Include="Resources\Reflect-BumpSpec.shader">
54+
<Link>Reflect-BumpSpec.shader</Link>
55+
<LogicalName>Reflective.Bumped_Specular</LogicalName>
56+
</EmbeddedResource>
57+
<EmbeddedResource Include="Resources\Reflect-BumpVertexLit.shader">
58+
<Link>Reflect-BumpVertexLit.shader</Link>
59+
<LogicalName>Reflective.Bumped_VertexLit</LogicalName>
60+
</EmbeddedResource>
61+
<EmbeddedResource Include="Resources\Reflect-Bumped.shader">
62+
<Link>Reflect-Bumped.shader</Link>
63+
<LogicalName>Reflective.Bumped_Diffuse</LogicalName>
64+
</EmbeddedResource>
65+
<EmbeddedResource Include="Resources\Reflect-Diffuse.shader">
66+
<Link>Reflect-Diffuse.shader</Link>
67+
<LogicalName>Reflective.Diffuse</LogicalName>
68+
</EmbeddedResource>
69+
<EmbeddedResource Include="Resources\Reflect-Glossy.shader">
70+
<Link>Reflect-Glossy.shader</Link>
71+
<LogicalName>Reflective.Specular</LogicalName>
72+
</EmbeddedResource>
73+
<EmbeddedResource Include="Resources\Reflect-Parallax.shader">
74+
<Link>Reflect-Parallax.shader</Link>
75+
<LogicalName>Reflective.Parallax_Diffuse</LogicalName>
76+
</EmbeddedResource>
77+
<EmbeddedResource Include="Resources\Reflect-ParallaxSpec.shader">
78+
<Link>Reflect-ParallaxSpec.shader</Link>
79+
<LogicalName>Reflective.Parallax_Specular</LogicalName>
80+
</EmbeddedResource>
81+
<EmbeddedResource Include="Resources\Reflect-VertexLit.shader">
82+
<Link>Reflect-VertexLit.shader</Link>
83+
<LogicalName>Reflective.VertexLit</LogicalName>
84+
</EmbeddedResource>
85+
</ItemGroup>
4886
</Project>
6.19 KB
Binary file not shown.

ReflectiveShaderModule.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,36 @@ private void ReplaceShader(UnityEngine.Renderer pRenderer)
134134
{
135135
Debug.Log((object)string.Format("RP: Renderer found: {0}", (object)this._rShader));
136136
Material material;
137+
string shaderName = this.ShaderName;
138+
139+
shaderName.Replace (" ", "_");
140+
shaderName.Replace ("/", ".");
141+
137142
if (this._rShader == null)
138143
{
139144
Debug.Log((object)("RP: null shader. Trying to retrieve ReflectionPlugin.Shaders." + this.ShaderName));
140145
Assembly assembly = Assembly.GetExecutingAssembly();
141-
StreamReader shaderStreamReader = new StreamReader(assembly.GetManifestResourceStream(/*"ReflectionPlugin.Shaders." + */this.ShaderName));
142-
if (shaderStreamReader != null)
143-
{
144-
material = new Material(shaderStreamReader.ReadToEnd())
145-
{
146-
mainTexture = pRenderer.material.mainTexture
147-
};
148-
}
149-
else
150-
{
151-
material = new Material(Shader.Find("Reflective/VertexList"))
152-
{
153-
mainTexture = pRenderer.material.mainTexture
154-
};
155-
}
146+
try
147+
{
148+
Debug.Log ("[ReflectionPlugin] Looking for resource " + shaderName);
149+
150+
StreamReader shaderStreamReader = new StreamReader(assembly.GetManifestResourceStream(/*"ReflectionPlugin.Shaders." + */shaderName));
151+
152+
Debug.Log ("[ReflectionPlugin] Got " + shaderName);
153+
154+
material = new Material(shaderStreamReader.ReadToEnd())
155+
{
156+
mainTexture = pRenderer.material.mainTexture
157+
};
158+
}
159+
catch (Exception e)
160+
{
161+
Debug.Log ("ReflectionPlugin caught exception " + e.ToString() + " (" + e.Message + ")");
162+
material = new Material(Shader.Find("Reflective/VertexList"))
163+
{
164+
mainTexture = pRenderer.material.mainTexture
165+
};
166+
}
156167
}
157168
else
158169
{
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
Shader "Reflective/Bumped Unlit" {
2+
Properties {
3+
_Color ("Main Color", Color) = (1,1,1,1)
4+
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
5+
_MainTex ("Base (RGB), RefStrength (A)", 2D) = "white" {}
6+
_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
7+
_BumpMap ("Normalmap", 2D) = "bump" {}
8+
}
9+
10+
Category {
11+
Tags { "RenderType"="Opaque" }
12+
LOD 250
13+
14+
// ------------------------------------------------------------------
15+
// Shaders
16+
17+
SubShader {
18+
// Always drawn reflective pass
19+
Pass {
20+
Name "BASE"
21+
Tags {"LightMode" = "Always"}
22+
CGPROGRAM
23+
#pragma vertex vert
24+
#pragma fragment frag
25+
26+
#include "UnityCG.cginc"
27+
28+
struct v2f {
29+
float4 pos : SV_POSITION;
30+
float2 uv : TEXCOORD0;
31+
float2 uv2 : TEXCOORD1;
32+
float3 I : TEXCOORD2;
33+
float3 TtoW0 : TEXCOORD3;
34+
float3 TtoW1 : TEXCOORD4;
35+
float3 TtoW2 : TEXCOORD5;
36+
};
37+
38+
uniform float4 _MainTex_ST, _BumpMap_ST;
39+
40+
v2f vert(appdata_tan v)
41+
{
42+
v2f o;
43+
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
44+
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
45+
o.uv2 = TRANSFORM_TEX(v.texcoord,_BumpMap);
46+
47+
o.I = -WorldSpaceViewDir( v.vertex );
48+
49+
TANGENT_SPACE_ROTATION;
50+
o.TtoW0 = mul(rotation, _Object2World[0].xyz * unity_Scale.w);
51+
o.TtoW1 = mul(rotation, _Object2World[1].xyz * unity_Scale.w);
52+
o.TtoW2 = mul(rotation, _Object2World[2].xyz * unity_Scale.w);
53+
54+
return o;
55+
}
56+
57+
uniform sampler2D _BumpMap;
58+
uniform sampler2D _MainTex;
59+
uniform samplerCUBE _Cube;
60+
uniform fixed4 _ReflectColor;
61+
uniform fixed4 _Color;
62+
63+
fixed4 frag (v2f i) : SV_Target
64+
{
65+
// Sample and expand the normal map texture
66+
fixed3 normal = UnpackNormal(tex2D(_BumpMap, i.uv2));
67+
68+
fixed4 texcol = tex2D(_MainTex,i.uv);
69+
70+
// transform normal to world space
71+
half3 wn;
72+
wn.x = dot(i.TtoW0, normal);
73+
wn.y = dot(i.TtoW1, normal);
74+
wn.z = dot(i.TtoW2, normal);
75+
76+
// calculate reflection vector in world space
77+
half3 r = reflect(i.I, wn);
78+
79+
fixed4 c = UNITY_LIGHTMODEL_AMBIENT * texcol;
80+
c.rgb *= 2;
81+
fixed4 reflcolor = texCUBE(_Cube, r) * _ReflectColor * texcol.a;
82+
return c + reflcolor;
83+
}
84+
ENDCG
85+
}
86+
}
87+
88+
// ------------------------------------------------------------------
89+
// No vertex or fragment programs
90+
91+
SubShader {
92+
Pass {
93+
Tags {"LightMode" = "Always"}
94+
Name "BASE"
95+
BindChannels {
96+
Bind "Vertex", vertex
97+
Bind "Normal", normal
98+
}
99+
SetTexture [_Cube] {
100+
constantColor [_ReflectColor]
101+
combine texture * constant
102+
}
103+
}
104+
}
105+
}
106+
107+
FallBack "VertexLit", 1
108+
109+
}

Resources/Reflect-BumpSpec.shader

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Shader "Reflective/Bumped Specular" {
2+
Properties {
3+
_Color ("Main Color", Color) = (1,1,1,1)
4+
_SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
5+
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
6+
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
7+
_MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
8+
_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
9+
_BumpMap ("Normalmap", 2D) = "bump" {}
10+
}
11+
12+
SubShader {
13+
Tags { "RenderType"="Opaque" }
14+
LOD 400
15+
CGPROGRAM
16+
#pragma surface surf BlinnPhong
17+
#pragma target 3.0
18+
//input limit (8) exceeded, shader uses 9
19+
#pragma exclude_renderers d3d11_9x
20+
21+
sampler2D _MainTex;
22+
sampler2D _BumpMap;
23+
samplerCUBE _Cube;
24+
25+
fixed4 _Color;
26+
fixed4 _ReflectColor;
27+
half _Shininess;
28+
29+
struct Input {
30+
float2 uv_MainTex;
31+
float2 uv_BumpMap;
32+
float3 worldRefl;
33+
INTERNAL_DATA
34+
};
35+
36+
void surf (Input IN, inout SurfaceOutput o) {
37+
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
38+
fixed4 c = tex * _Color;
39+
o.Albedo = c.rgb;
40+
41+
o.Gloss = tex.a;
42+
o.Specular = _Shininess;
43+
44+
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
45+
46+
float3 worldRefl = WorldReflectionVector (IN, o.Normal);
47+
fixed4 reflcol = texCUBE (_Cube, worldRefl);
48+
reflcol *= tex.a;
49+
o.Emission = reflcol.rgb * _ReflectColor.rgb;
50+
o.Alpha = reflcol.a * _ReflectColor.a;
51+
}
52+
ENDCG
53+
}
54+
55+
FallBack "Reflective/Bumped Diffuse"
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Shader "Reflective/Bumped VertexLit" {
2+
Properties {
3+
_Color ("Main Color", Color) = (1,1,1,1)
4+
_SpecColor ("Spec Color", Color) = (1,1,1,1)
5+
_Shininess ("Shininess", Range (0.1, 1)) = 0.7
6+
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
7+
_MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
8+
_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
9+
_BumpMap ("Normalmap", 2D) = "bump" {}
10+
}
11+
12+
Category {
13+
Tags { "RenderType"="Opaque" }
14+
LOD 250
15+
SubShader {
16+
UsePass "Reflective/Bumped Unlit/BASE"
17+
Pass {
18+
Tags { "LightMode" = "Vertex" }
19+
Blend One One ZWrite Off Fog { Color (0,0,0,0) }
20+
Material {
21+
Diffuse [_Color]
22+
Ambient [_Color]
23+
Shininess [_Shininess]
24+
Specular [_SpecColor]
25+
Emission [_Emission]
26+
}
27+
Lighting On
28+
SeparateSpecular On
29+
SetTexture [_MainTex] {
30+
constantColor [_Color]
31+
Combine texture * primary DOUBLE, texture * primary
32+
}
33+
}
34+
}
35+
}
36+
37+
FallBack "Reflective/VertexLit"
38+
}

Resources/Reflect-Bumped.shader

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Shader "Reflective/Bumped Diffuse" {
2+
Properties {
3+
_Color ("Main Color", Color) = (1,1,1,1)
4+
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
5+
_MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
6+
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
7+
_BumpMap ("Normalmap", 2D) = "bump" {}
8+
}
9+
10+
SubShader {
11+
Tags { "RenderType"="Opaque" }
12+
LOD 300
13+
14+
CGPROGRAM
15+
#pragma surface surf Lambert
16+
#pragma exclude_renderers d3d11_9x
17+
18+
sampler2D _MainTex;
19+
sampler2D _BumpMap;
20+
samplerCUBE _Cube;
21+
22+
fixed4 _Color;
23+
fixed4 _ReflectColor;
24+
25+
struct Input {
26+
float2 uv_MainTex;
27+
float2 uv_BumpMap;
28+
float3 worldRefl;
29+
INTERNAL_DATA
30+
};
31+
32+
void surf (Input IN, inout SurfaceOutput o) {
33+
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
34+
fixed4 c = tex * _Color;
35+
o.Albedo = c.rgb;
36+
37+
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
38+
39+
float3 worldRefl = WorldReflectionVector (IN, o.Normal);
40+
fixed4 reflcol = texCUBE (_Cube, worldRefl);
41+
reflcol *= tex.a;
42+
o.Emission = reflcol.rgb * _ReflectColor.rgb;
43+
o.Alpha = reflcol.a * _ReflectColor.a;
44+
}
45+
ENDCG
46+
}
47+
48+
FallBack "Reflective/VertexLit"
49+
}

0 commit comments

Comments
 (0)