Skip to content

Commit

Permalink
Android fixes for VertexColor (#547)
Browse files Browse the repository at this point in the history
Updating VertexColor.cginc to properly support the path when UNITY_STANDARD_SIMPLE is true (e.g. Android builds). This fixes various conversion and re-definition errors (should resolve #125 and #540).
- Tested Android and Standalone builds on 2018.4.14f1. Confirmed errors and warnings on Android without fix and no related errors/warnings in either target after fix.
  • Loading branch information
mhochk authored Jan 14, 2020
1 parent e89e2d1 commit 7bdd9b1
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Shaders/VertexColor.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
#include "UnityStandardInput.cginc"
#include "UnityStandardCoreForward.cginc"

struct WrappedVertexOutputForwardBase {
// Structure for holding the return type of vertBase and the color information. For vertBase definition see
// C:\Program Files\Unity\Hub\Editor\2018.4.14f1\Editor\Data\CGIncludes\UnityStandardCoreForward.cginc
struct WrappedVertexOutput {
#if UNITY_STANDARD_SIMPLE
VertexOutputBaseSimple innerValue;
#else
VertexOutputForwardBase innerValue;
#endif
fixed4 color : COLOR;
};

WrappedVertexOutputForwardBase vert_vcol (appdata_full v)
// Simple replacement for vertBase that captures the additional color information needed later
WrappedVertexOutput vert_vcol(appdata_full v)
{
// Save the color information
WrappedVertexOutputForwardBase o;
WrappedVertexOutput o;
o.color = v.color;

// Forward this call to the default vertBase
Expand All @@ -33,15 +40,65 @@ WrappedVertexOutputForwardBase vert_vcol (appdata_full v)
return o;
}

fixed4 frag_vcol(WrappedVertexOutputForwardBase wvofb) : SV_Target
#if UNITY_STANDARD_SIMPLE
half4 frag_vcol(WrappedVertexOutput wvo) : SV_Target
{
// Put the original output from vertBase in a variable called 'i' so the code can be cleanly
// copy/pasted, as well as for any macros (e.g. FRAGMENT_SETUP) that assume that naming convention.
VertexOutputBaseSimple i = wvo.innerValue;

// The following section is copied from the fragBase implementation, found in
// C:\Program Files\Unity\Hub\Editor\2018.4.14f1\Editor\Data\CGIncludes\UnityStandardCoreForwardSimple.cginc,
// with the name fragForwardBaseSimpleInternal. It has been modified to
// include a section to modify the diffColor after it is calculated but before
// it is used in the remaining calculations.
UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);

FragmentCommonData s = FragmentSetupSimple(i);

UnityLight mainLight = MainLightSimple(i, s);

#if !defined(LIGHTMAP_ON) && defined(_NORMALMAP)
half ndotl = saturate(dot(s.tangentSpaceNormal, i.tangentSpaceLightDir));
#else
half ndotl = saturate(dot(s.normalWorld, mainLight.dir));
#endif

//we can't have worldpos here (not enough interpolator on SM 2.0) so no shadow fade in that case.
half shadowMaskAttenuation = UnitySampleBakedOcclusion(i.ambientOrLightmapUV, 0);
half realtimeShadowAttenuation = SHADOW_ATTENUATION(i);
half atten = UnityMixRealtimeAndBakedShadows(realtimeShadowAttenuation, shadowMaskAttenuation, 0);

// Start: Modified section
s.diffColor.x *= wvo.color.x;
s.diffColor.y *= wvo.color.y;
s.diffColor.z *= wvo.color.z;
// End: Modified section

half occlusion = Occlusion(i.tex.xy);
half rl = dot(REFLECTVEC_FOR_SPECULAR(i, s), LightDirForSpecular(i, mainLight));

UnityGI gi = FragmentGI(s, occlusion, i.ambientOrLightmapUV, atten, mainLight);
half3 attenuatedLightColor = gi.light.color * ndotl;

half3 c = BRDF3_Indirect(s.diffColor, s.specColor, gi.indirect, PerVertexGrazingTerm(i, s), PerVertexFresnelTerm(i));
c += BRDF3DirectSimple(s.diffColor, s.specColor, s.smoothness, rl) * attenuatedLightColor;
c += Emission(i.tex.xy);

UNITY_APPLY_FOG(i.fogCoord, c);

return OutputForward(half4(c, 1), s.alpha);
}
#else
half4 frag_vcol(WrappedVertexOutput wvo) : SV_Target
{
// Put the original output from vertBase in a variable called 'i' for the macros
// (e.g. FRAGMENT_SETUP) that assume that naming convention.
VertexOutputForwardBase i = wvofb.innerValue;
// Put the original output from vertBase in a variable called 'i' so the code can be cleanly
// copy/pasted, as well as for any macros (e.g. FRAGMENT_SETUP) that assume that naming convention.
VertexOutputForwardBase i = wvo.innerValue;

// The following section is copied from the fragBase implementation, found in
// C:\Program Files\Unity\Hub\Editor\2018.4.14f1\Editor\Data\CGIncludes\UnityStandardCore.cginc,
// with the name fragForwardBase/fragForwardBaseInternal. It has been modified to
// with the name fragForwardBaseInternal. It has been modified to
// include a section to modify the diffColor after it is calculated but before
// it is used in the remaining calculations.
UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);
Expand All @@ -55,9 +112,9 @@ fixed4 frag_vcol(WrappedVertexOutputForwardBase wvofb) : SV_Target
UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld);

// Start: Modified section
s.diffColor.x *= wvofb.color.x;
s.diffColor.y *= wvofb.color.y;
s.diffColor.z *= wvofb.color.z;
s.diffColor.x *= wvo.color.x;
s.diffColor.y *= wvo.color.y;
s.diffColor.z *= wvo.color.z;
// End: Modified section

half occlusion = Occlusion(i.tex.xy);
Expand All @@ -71,3 +128,4 @@ fixed4 frag_vcol(WrappedVertexOutputForwardBase wvofb) : SV_Target
return OutputForward(c, s.alpha);
}
#endif
#endif

0 comments on commit 7bdd9b1

Please sign in to comment.