From 2232f4327f84f3fba18c97fa28f399af784a7016 Mon Sep 17 00:00:00 2001 From: in0finite Date: Sat, 13 Feb 2021 22:18:27 +0100 Subject: [PATCH] fix FPS drop on Android --- Assets/Resources/Shaders/Shared.cginc | 39 ++------------- Assets/Resources/Shaders/Vehicle.shader | 11 +---- .../Shaders/VehicleTransparent.shader | 11 +---- Assets/Scripts/Behaviours/Vehicles/Vehicle.cs | 47 +++++++++++++++++-- 4 files changed, 51 insertions(+), 57 deletions(-) diff --git a/Assets/Resources/Shaders/Shared.cginc b/Assets/Resources/Shaders/Shared.cginc index c6791b26..06c03094 100644 --- a/Assets/Resources/Shaders/Shared.cginc +++ b/Assets/Resources/Shaders/Shared.cginc @@ -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; diff --git a/Assets/Resources/Shaders/Vehicle.shader b/Assets/Resources/Shaders/Vehicle.shader index 5c69be14..7424414c 100644 --- a/Assets/Resources/Shaders/Vehicle.shader +++ b/Assets/Resources/Shaders/Vehicle.shader @@ -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 } diff --git a/Assets/Resources/Shaders/VehicleTransparent.shader b/Assets/Resources/Shaders/VehicleTransparent.shader index 49f8e6ff..2d2ba5c5 100644 --- a/Assets/Resources/Shaders/VehicleTransparent.shader +++ b/Assets/Resources/Shaders/VehicleTransparent.shader @@ -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 diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs index b56c53f7..ef4d7d16 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs @@ -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(); 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); + } + } }