fix FPS drop on Android

This commit is contained in:
in0finite 2021-02-13 22:18:27 +01:00
parent 9f1b01296d
commit 2232f4327f
4 changed files with 51 additions and 57 deletions

View file

@ -6,15 +6,8 @@ fixed4 _Color;
#ifdef VEHICLE
int _CarColorIndex;
fixed3 _CarColor1;
fixed3 _CarColor2;
fixed3 _CarColor3;
fixed3 _CarColor4;
fixed3 _HeadLightColor;
fixed3 _TailLightColor;
fixed4 _Lights;
fixed3 _CarColor;
fixed _CarEmission;
float _Metallic;
float _Smoothness;
@ -45,35 +38,11 @@ void surf(Input IN, inout SurfaceOutputStandard o)
fixed3 clr = tex2D(_MainTex, IN.uv_MainTex).rgb;
fixed mask = tex2D(_MaskTex, IN.uv_MainTex).a;
#ifdef VEHICLE
fixed3 carColors[9] = {
fixed3(1, 1, 1),
_CarColor1,
_CarColor2,
_CarColor3,
_CarColor4,
_HeadLightColor,
_HeadLightColor,
_TailLightColor,
_TailLightColor
};
fixed carEmission[9] = {
0,
0,
0,
0,
0,
exp(_Lights.x * 2) - 1,
exp(_Lights.y * 2) - 1,
exp(_Lights.z * 2) - 1,
exp(_Lights.w * 2) - 1
};
#endif
o.Albedo = clr
#ifdef VEHICLE
* carColors[_CarColorIndex]
* _CarColor
#endif
* IN.color.rgb * _Color.rgb;
@ -82,7 +51,7 @@ void surf(Input IN, inout SurfaceOutputStandard o)
#ifdef VEHICLE
o.Metallic = _Metallic * o.Alpha;
o.Smoothness = _Smoothness;
o.Emission = carEmission[_CarColorIndex] * o.Albedo;
o.Emission = _CarEmission * o.Albedo;
#else
o.Metallic = 0;
o.Smoothness = 0;

View file

@ -11,16 +11,9 @@
_Color ("Color", Color) = (1, 1, 1, 1)
_CarColorIndex ("Car Color Index", Range(0, 8)) = 0
_CarColor1 ("Car Color 1", Color) = (1, 1, 1, 1)
_CarColor2 ("Car Color 2", Color) = (1, 1, 1, 1)
_CarColor3 ("Car Color 3", Color) = (1, 1, 1, 1)
_CarColor4 ("Car Color 4", Color) = (1, 1, 1, 1)
_HeadLightColor ("Head Light Color", Color) = (1, 1, 1, 1)
_TailLightColor ("Tail Light Color", Color) = (1, 1, 1, 1)
_Lights ("Light Brightnesses", Vector) = (1, 1, 1, 1)
_CarColor ("Car Color", Color) = (1, 1, 1, 1)
_CarEmission ("Car Emission", Range(0, 10)) = 0
_AlphaCutoff ("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
}

View file

@ -11,16 +11,9 @@
_Color ("Color", Color) = (1, 1, 1, 1)
_CarColorIndex ("Car Color Index", Range(0, 8)) = 0
_CarColor1 ("Car Color 1", Color) = (1, 1, 1, 1)
_CarColor2 ("Car Color 2", Color) = (1, 1, 1, 1)
_CarColor3 ("Car Color 3", Color) = (1, 1, 1, 1)
_CarColor4 ("Car Color 4", Color) = (1, 1, 1, 1)
_HeadLightColor ("Head Light Color", Color) = (1, 1, 1, 1)
_TailLightColor ("Tail Light Color", Color) = (1, 1, 1, 1)
_Lights ("Light Brightnesses", Vector) = (1, 1, 1, 1)
_CarColor ("Car Color", Color) = (1, 1, 1, 1)
_CarEmission ("Car Emission", Range(0, 10)) = 0
}
SubShader

View file

@ -453,16 +453,55 @@ namespace SanAndreasUnity.Behaviours.Vehicles
_colorsChanged = false;
var indices = CarColors.FromIndices(_colors);
for (var i = 0; i < 4; ++i)
_props.SetColor(CarColorIds[i], indices[i]);
_props.SetVector(LightsId, new Vector4(_lights[0], _lights[1], _lights[2], _lights[3]));
Color32 headLightColor = new Color32(255, 255, 255, 255);
Color32 tailLightColor = new Color32(255, 255, 255, 255);
// compute car colors
Color32[] carColors = new []
{
new Color32(255, 255, 255, 255),
indices[0],
indices[1],
indices[2],
indices[3],
headLightColor,
headLightColor,
tailLightColor,
tailLightColor,
};
// compute car emissions
float[] carEmissions = new[]
{
0f,
0f,
0f,
0f,
0f,
Mathf.Exp(_lights[0] * 2) - 1,
Mathf.Exp(_lights[1] * 2) - 1,
Mathf.Exp(_lights[2] * 2) - 1,
Mathf.Exp(_lights[3] * 2) - 1,
};
foreach (var frame in _frames)
{
var mr = frame.GetComponent<MeshRenderer>();
if (mr == null) continue;
mr.SetPropertyBlock(_props);
// get color index from each material, and assign properties accordingly
var materials = mr.sharedMaterials;
for (int i = 0; i < materials.Length; i++)
{
int carColorIndex = materials[i].GetInt("_CarColorIndex");
_props.SetColor("_CarColor", carColors[carColorIndex]);
_props.SetFloat("_CarEmission", carEmissions[carColorIndex]);
mr.SetPropertyBlock(_props, i);
}
}
}