#my shawty code

1 messages · Page 1 of 1 (latest)

magic hound
#
using System.Collections.Generic;
using UnityEngine;

public class WallTransparencyManager : MonoBehaviour
{
    public GridManager gridManager;
    public float transparentAlpha = 0.3f;
    public float opaqueAlpha = 1f;
    public LayerMask wallLayer;

    private HashSet<GameObject> wallsFadedThisFrame = new HashSet<GameObject>();

    void Update()
    {
        if (gridManager == null) return;

        Tile[,] grid = gridManager.GetGrid();
        if (grid == null) return;

        Vector3 cameraPos = transform.position;
        wallsFadedThisFrame.Clear();

        foreach (Tile tile in grid)
        {
            if (tile == null) continue;

            Vector3 tilePos = tile.transform.position;
            Vector3 direction = (tilePos - cameraPos).normalized;
            float distance = Vector3.Distance(cameraPos, tilePos);

            Debug.DrawRay(cameraPos, direction * distance, Color.red, 0.1f);

            if (Physics.Raycast(cameraPos, direction, out RaycastHit hit, distance, wallLayer))
            {
                GameObject wall = hit.collider.gameObject;
                FadeWall(wall);
            }
        }

        ResetAllWalls(grid);
    }

    private void FadeWall(GameObject wall)
    {
        if (wall == null || wallsFadedThisFrame.Contains(wall)) return;

        Debug.Log($"Fading wall '{wall.name}' to transparent.");
        SetWallAlpha(wall, transparentAlpha);
        wallsFadedThisFrame.Add(wall);
    }

    private void ResetAllWalls(Tile[,] grid)
    {
        foreach (Tile tile in grid)
        {
            if (tile == null) continue;

            ResetWallIfNotFaded(tile.wallNorth);
            ResetWallIfNotFaded(tile.wallSouth);
            ResetWallIfNotFaded(tile.wallEast);
            ResetWallIfNotFaded(tile.wallWest);
        }
    }

    private void ResetWallIfNotFaded(GameObject wall)
    {
        if (wall == null) return;

        if (!wallsFadedThisFrame.Contains(wall))
        {
            Debug.Log($"Resetting wall '{wall.name}' to opaque.");
            SetWallAlpha(wall, opaqueAlpha);
        }
    }

    private void SetWallAlpha(GameObject wall, float alpha)
    {
        Renderer rend = wall.GetComponent<Renderer>();
        if (rend == null) return;

        Material mat = rend.material;
        Color color = mat.color;
        color.a = alpha;
        mat.color = color;

        if (alpha >= 1f)
        {
            SetMaterialToOpaque(mat);
        }
        else
        {
            SetMaterialToTransparent(mat);
        }
    }

    private void SetMaterialToOpaque(Material mat)
    {
        mat.SetFloat("_Surface", 0); // 0 = Opaque
        mat.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
        mat.EnableKeyword("_SURFACE_TYPE_OPAQUE");
        mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;

        Debug.Log($"Set material '{mat.name}' to OPAQUE.");
    }

    private void SetMaterialToTransparent(Material mat)
    {
        mat.SetFloat("_Surface", 1); // 1 = Transparent
        mat.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
        mat.DisableKeyword("_SURFACE_TYPE_OPAQUE");
        mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;

        Debug.Log($"Set material '{mat.name}' to TRANSPARENT.");
    }
}
#

its super unoptimized i think and tbh idek how its working

#

i frankenstined it hard

humble phoenix
#

eeeh. I wonder if the keyword/"_Surface" prop/render queue shenanigans that you're doing might be related...

magic hound
#

wdym...

#

did i do a dumb

humble phoenix
#

Well, I was wondering if all that is required at all.

magic hound
#

it may not be. should i jsut cut it out entirely?

humble phoenix
#

Ah, I see.
The way I usually do it, is by having a separate transparent material prepared, and then I just swap between the materials.

magic hound
#

dude i feel so stupid

#

THAT MAKES SINCE

#

and its prolly way easier