Skip to content

Commit 8aac380

Browse files
committedOct 25, 2024·
LitOpaque shader
1 parent 92dfc69 commit 8aac380

9 files changed

+227
-2
lines changed
 
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace UnityToRebelFork.Editor
5+
{
6+
public class LitOpaqueShaderMapping : ShaderMappingBase, IShaderMapping
7+
{
8+
public int Priority { get; } = 0;
9+
10+
public LitOpaqueShaderMapping(Lazy<ExportOrchestrator> orchestrator, ExportSettings settings) : base(orchestrator, settings)
11+
{
12+
}
13+
14+
public bool CanMap(UnityEngine.Shader shader)
15+
{
16+
if (shader.name == Shaders.RBFX.LitOpaqueShaderAdapter.ShaderName)
17+
return true;
18+
return false;
19+
}
20+
21+
public MaterialModel Map(UnityEngine.Material material)
22+
{
23+
var model = new MaterialModel();
24+
25+
MapCommonParameters(material, model);
26+
MapDefaultTechnique(material, model);
27+
28+
var shaderArgs = new Shaders.RBFX.LitOpaqueShaderAdapter(material);
29+
30+
model.MatDiffColor = shaderArgs._Color;
31+
model.MatEmissiveColor = (Vector4)shaderArgs._EmissionColor;
32+
model.NormalScale = shaderArgs._BumpScale;
33+
model.AlphaCutoff = shaderArgs._Cutoff;
34+
model.Metallic = shaderArgs._Metallic;
35+
model.Roughness = shaderArgs._Roughness;
36+
37+
if (shaderArgs._BumpMap != null)
38+
model.Normal = orchestrator.Value.ScheduleExport(shaderArgs._BumpMap);
39+
40+
if (shaderArgs._EmissionMap != null)
41+
model.Emission = orchestrator.Value.ScheduleExport(shaderArgs._EmissionMap);
42+
43+
if (shaderArgs._MainTex != null)
44+
model.Albedo = orchestrator.Value.ScheduleExport(shaderArgs._MainTex);
45+
46+
if (shaderArgs._PBRMap != null)
47+
model.Properties = orchestrator.Value.ScheduleExport(shaderArgs._PBRMap);
48+
49+
return model;
50+
}
51+
}
52+
}

‎Editor/Material/LitOpaqueShaderMapping.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Editor/Material/Shaders/RBFX.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace UnityToRebelFork.Editor.Shaders.RBFX
2+
{
3+
public class LitOpaqueShaderAdapter
4+
{
5+
public static readonly string ShaderName = "RBFX/LitOpaque";
6+
7+
UnityEngine.Material material;
8+
9+
public LitOpaqueShaderAdapter(UnityEngine.Material material)
10+
{
11+
this.material = material;
12+
}
13+
14+
public UnityEngine.Texture _MainTex
15+
{
16+
get { return material.GetTexture("_MainTex"); }
17+
}
18+
19+
public UnityEngine.Texture _BumpMap
20+
{
21+
get { return material.GetTexture("_BumpMap"); }
22+
}
23+
24+
public UnityEngine.Texture _PBRMap
25+
{
26+
get { return material.GetTexture("_PBRMap"); }
27+
}
28+
29+
public UnityEngine.Texture _EmissionMap
30+
{
31+
get { return material.GetTexture("_EmissionMap"); }
32+
}
33+
34+
public UnityEngine.Color _EmissionColor
35+
{
36+
get { return material.GetColor("_EmissionColor"); }
37+
}
38+
39+
public UnityEngine.Color _Color
40+
{
41+
get { return material.GetColor("_Color"); }
42+
}
43+
44+
public float _BumpScale
45+
{
46+
get { return material.GetFloat("_BumpScale"); }
47+
}
48+
49+
public float _Metallic
50+
{
51+
get { return material.GetFloat("_Metallic"); }
52+
}
53+
54+
public float _Roughness
55+
{
56+
get { return material.GetFloat("_Roughness"); }
57+
}
58+
59+
public float _Cutoff
60+
{
61+
get { return material.GetFloat("_Cutoff"); }
62+
}
63+
}
64+
}

‎Editor/Material/Shaders/RBFX/LitOpaque.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Editor/Material/StandardSpecularShaderMapping.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using UnityEngine;
3+
using UnityToRebelFork.Editor.Shaders;
34

45
namespace UnityToRebelFork.Editor
56
{
@@ -13,7 +14,7 @@ public StandardSpecularShaderMapping(Lazy<ExportOrchestrator> orchestrator, Expo
1314

1415
public bool CanMap(UnityEngine.Shader shader)
1516
{
16-
if (shader.name == "Standard (Specular setup)")
17+
if (shader.name == Standard_Specularsetup_ShaderAdapter.ShaderName)
1718
return true;
1819
return false;
1920
}

‎Editor/RebelForkInstaller.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public static void Install(Rbfx.LightInject.ServiceContainer container, ExportSe
2424
container.RegisterSingleton<IShaderMapping, MobileVertexLitShaderMapping>(nameof(MobileVertexLitShaderMapping));
2525
container.RegisterSingleton<IShaderMapping, StandardShaderMapping>(nameof(StandardShaderMapping));
2626
container.RegisterSingleton<IShaderMapping, StandardSpecularShaderMapping>(nameof(StandardSpecularShaderMapping));
27-
container.RegisterSingleton<IShaderMapping, DefaultShaderMapping>(nameof(DefaultShaderMapping));
2827
container.RegisterSingleton<IShaderMapping, LegacyDiffuseShaderMapping>(nameof(LegacyDiffuseShaderMapping));
28+
container.RegisterSingleton<IShaderMapping, LitOpaqueShaderMapping>(nameof(LitOpaqueShaderMapping));
29+
container.RegisterSingleton<IShaderMapping, DefaultShaderMapping>(nameof(DefaultShaderMapping));
2930

3031
container.RegisterSingleton<NameCollisionResolver>();
3132
container.Register<PrefabVisitor>();

‎Runtime/LitOpaque.shader

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Shader "RBFX/LitOpaque"
2+
{
3+
Properties
4+
{
5+
_MainTex ("Albedo (RGB)", 2D) = "white" {}
6+
_BumpMap ("Normal Map", 2D) = "bump" {}
7+
_PBRMap ("PBR Map (R: Roughness, G: Metallic, A: AO)", 2D) = "white" {}
8+
_EmissionMap ("Emission Map", 2D) = "white" {}
9+
_EmissionColor ("Emission Color", Color) = (0,0,0,1)
10+
_Color ("Albedo", Color) = (1,1,1,1)
11+
_BumpScale ("Normal Map Scale", Range(0, 1)) = 1
12+
_Metallic ("Metallic Multiplier", Range(0, 1)) = 1
13+
_Roughness ("Roughness Multiplier", Range(0, 1)) = 1
14+
_Cutoff ("Alpha Mask Threshold", Range(0, 1)) = 0.5
15+
}
16+
17+
SubShader
18+
{
19+
Tags { "RenderType"="Opaque" }
20+
LOD 200
21+
22+
CGPROGRAM
23+
#pragma surface surf Standard fullforwardshadows
24+
25+
sampler2D _MainTex;
26+
sampler2D _BumpMap;
27+
sampler2D _PBRMap;
28+
sampler2D _EmissionMap;
29+
fixed4 _Color;
30+
fixed4 _EmissionColor;
31+
float _BumpScale;
32+
float _Metallic;
33+
float _Roughness;
34+
float _Cutoff;
35+
36+
struct Input
37+
{
38+
float2 uv_MainTex;
39+
float4 color : COLOR;
40+
};
41+
42+
void surf (Input IN, inout SurfaceOutputStandard o)
43+
{
44+
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
45+
clip(c.a - _Cutoff);
46+
o.Albedo = c.rgb * IN.color.rgb;
47+
48+
// PBR map unpacking
49+
half4 pbr = tex2D(_PBRMap, IN.uv_MainTex);
50+
half roughness = pbr.r * _Roughness;
51+
half metallic = pbr.g * _Metallic;
52+
half ao = pbr.a;
53+
54+
o.Metallic = metallic;
55+
o.Smoothness = 1.0 - roughness;
56+
o.Occlusion = ao;
57+
58+
// Apply the normal map
59+
o.Normal = UnpackNormalWithScale(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
60+
61+
// Apply emission map
62+
fixed4 emission = tex2D(_EmissionMap, IN.uv_MainTex) * _EmissionColor;
63+
o.Emission = emission.rgb;
64+
}
65+
ENDCG
66+
}
67+
FallBack "Diffuse"
68+
}

‎Runtime/LitOpaque.shader.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.