gpt4 book ai didi

c# - 检查玩家在潜行游戏中的亮度

转载 作者:行者123 更新时间:2023-11-30 18:14:16 26 4
gpt4 key购买 nike

所以,我正在构建一个潜行游戏,虽然使用带相机的阴影缓冲区并获取像素已经奏效,但它在光线处于特定角度时不是很有效,而且它在 Unity 中非常昂贵。我正在寻找一种方法来确定玩家通过着色器的亮度,但是,我不是程序员,而且我从未接触过着色器。我一直在寻找几个月,但我没有找到一个值,每个人都只是在使用触发器,这对这个游戏没有影响。我正在尝试做一些类似于 Thief2 或 Splinter Cell 游戏的事情。我知道光线转换,但是,它们也不够准确。

那么,我怎样才能确定播放器的亮度,以获得合适的渐变?

这是我目前用来收集光线的代码。

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

public class ShadowBufferScript : MonoBehaviour {

public RenderTexture shadowBuffer;
Texture2D tex2d;
public float percentageHidden;
public float relativeLuminance;

void Start()
{
tex2d = new Texture2D (shadowBuffer.width, shadowBuffer.height, TextureFormat.RGB24, false);
}


void FixedUpdate ()
{
tex2d = new Texture2D (shadowBuffer.width, shadowBuffer.height, TextureFormat.RGB24, false);
RenderTexture.active = shadowBuffer;
tex2d.ReadPixels (new Rect (0, 0, shadowBuffer.width, shadowBuffer.height), 0, 0);
tex2d.Apply ();

Color shadowBufferColor = tex2d.GetPixel (0, 0);
relativeLuminance = shadowBufferColor.r * 0.2126f + shadowBufferColor.g * 0.7152f + shadowBufferColor.b * 0.0722f;

percentageHidden = relativeLuminance * 100;

}
}

最佳答案

我将您的游戏想象成一个有很多黑暗区域的游戏,光源是额外的障碍。我不知道你在谈论触发器时的确切意思,但你可以用碰撞区覆盖你的照明区域,这可以计算玩家与光源的距离,并将其转换为可以发送给玩家的照明值。

// Collision functions based on correct layering (Player/LightingZones)
[RequireComponent(typeof(Collider))]
public class LightingCalculator : MonoBehaviour
{
private bool _entered = false;

private void Update()
{
if (_entered) FindObjectOfType<PlayerController>().LightingValue = CalculateLighting();
}

private void OnCollisionEnter(Collision col)
{
_entered = true;
}

private void OnCollisionExit(Collision col)
{
_entered = false;
}

private float CalculateLighting()
{
float lighting = 0;
// Your calculation
return lighting;
}
}

关于c# - 检查玩家在潜行游戏中的亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com