Сборка IOS — проблема с шейдером meshrenderer?IOS

Программируем под IOS
Ответить
Anonymous
 Сборка IOS — проблема с шейдером meshrenderer?

Сообщение Anonymous »

Unity 2022.3.33f1
По какой-то причине изменение материала шейдера meshrenderer не работает на iOS, но работает на Android и Windows!!< /p>
Я работал над Fog Of War, где использовал SimpleFOW от Revision3, это очень простой FOW.

Код: Выделить всё

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SimpleFOW
{
[RequireComponent(typeof(MeshRenderer))]
public class FogOfWarShaderControl : MonoBehaviour
{
public static FogOfWarShaderControl Instance { get; private set; }

[Header("Maximum amount of revealing points")]
[SerializeField] private uint maximumPoints = 512;

[Header("Game Camera")]
[SerializeField] private Camera mainCamera;

private List points = new List();

private Vector2 meshSize, meshExtents;

private Vector4[] sendBuffer;

private MeshRenderer meshRenderer;

private void Awake()
{
Instance = this;

Init();
}

// Initialize required variables
public void Init()
{
meshRenderer = GetComponent();
meshExtents = meshRenderer.bounds.extents;
meshSize = meshRenderer.bounds.size;

points = new List();
sendBuffer = new Vector4[maximumPoints];
}

// Transform world point to UV coordinate of FOW mesh
public Vector2 WorldPointToMeshUV(Vector2 wp)
{
Vector2 toRet = Vector2.zero;

toRet.x = (transform.position.x - wp.x + meshExtents.x) / meshSize.x;
toRet.y = (transform.position.y - wp.y + meshExtents.y) / meshSize.y;

return toRet;
}

// Show or hide FOW
public void SetEnabled(bool on)
{
meshRenderer.enabled = on;
}

// Add revealing point to FOW renderer if amount of points is lower than MAX_POINTS
public void AddPoint(Vector2 worldPoint)
{
if (points.Count < maximumPoints)
{
points.Add(WorldPointToMeshUV(worldPoint));
}
}

// Remove FOW revealing point
public void RemovePoint(Vector2 worldPoint)
{
if (worldPoint == new Vector2(0, 0))
{
return;
}

if (points.Contains(WorldPointToMeshUV(worldPoint)))
{
points.Remove(WorldPointToMeshUV(worldPoint));
}
}

// Send any change to revealing point list to shader for rendering
public void SendPoints()
{
points.ToArray().CopyTo(sendBuffer, 0);

meshRenderer.material.SetVectorArray("_PointArray", sendBuffer);
meshRenderer.material.SetInt("_PointCount", points.Count);
}

// Send new range value to shader
public void SendRange(float range)
{
meshRenderer.material.SetFloat("_RadarRange", range);
}

// Send new scale value to shader
public void SendScale(float scale)
{
meshRenderer.material.SetFloat("_Scale", scale);
}
}
}
И шейдер

Код: Выделить всё

Shader "Revision3/FogOfWar"
{
Properties
{
_MainTex ("Texture", 2D) = "black" {}
_PointCount("Point count", Range(0,512)) = 0
_Scale("Scale", Float) = 1.0
_RadarRange("Range", Float) = .5
_MaxAlpha("Maximum Alpha", Float) = 1.0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent"  }
LOD 100

ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

Pass
{
CGPROGRAM

#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;

float _RadarRange;
uint _PointCount;
float _Scale;
float _MaxAlpha;

float2 _PointArray[512];

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}

float getDistance(float2 pa[512], float2 uv) {

float cdist = 99999.0;

for (uint i = 0; i < _PointCount; i++) {
cdist = min(cdist, distance(pa[i]*_Scale, uv));
}

return cdist;
}

fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);

i.uv *= _Scale;

if (_PointCount > 0)
col.w = min(_MaxAlpha, max(0.0f, getDistance(_PointArray, i.uv) - _RadarRange));
else
col.w = _MaxAlpha;

return col;
}
ENDCG
}
}
}
Теперь я создаю игровой объект под названием FogOfWar следующим образом:
[img]https: //i.sstatic.net/eA6uRF1v.png[/img]

А затем в сценарии Unit.cs и Building.cs я добавляю следующую логику
частный Vector3 LastPos;

Код: Выделить всё

private void Update()
{
    if (lastPos != transform.position)
    {
        FogOfWarShaderControl.Instance.RemovePoint(lastPos);
        FogOfWarShaderControl.Instance.AddPoint(transform.position);

        lastPos = transform.position;

        FogOfWarShaderControl.Instance.SendPoints();
    }
}
Теперь это дает мне следующий эффект FOW на IOS
https://imgur.com/ao6v6SX
где на других устройствах результат должен быть следующим
https://imgur.com/2x4VLMf
Я не знаю, почему это происходит только на устройствах iOS.
Логика работает нормально на Android/Windows/Linux/editor, но не на iOS устройств.
Есть ли какая-то опция в сборке iOS, где отключается изменение шейдера материала в Meshrenderer?!

Подробнее здесь: https://stackoverflow.com/questions/787 ... ader-issue
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»