[英]How do I link a stored variable to an equation that affects multiple lights individually? (In Unity, using C#)
对于我的游戏,我正在模拟眩光,并具有一个用于计算观察者眼睛上的亮度的方程式。 由于眩光是基于视角的,因此该方程式需要考虑到,当用户四处移动头部时,太阳撞击用户眼睛的角度(在这种情况下,将是摄影机)会发生变化。 对于每个单独的光源,这又需要改变我有光源的每个点的强度。 有没有人有任何策略可以对此进行编码?
我假设我需要分别调用每个光线作为函数,然后将强度链接到眩光方程式,该方程式将光源入射角存储为变量,并链接到头戴式耳机。
统一调用光源的方式是:
private Light[] lights;
// Use this for initialization
void Start () {
lights = FindObjectsOfType(typeof(Light)) as Light[];
foreach(Light light in lights)
{
light.intensity = 0;
Debug.Log(light);
}
}
但是强度需要以(10 *(来自太阳的照度值)/(根据耳机位置变化的角度)^ 2)的比率变化。
我将如何在此代码中存储耳机角度,以使强度不是静态数字,而是基于变量?
提供的任何帮助将不胜感激!
编辑:
这是我现在所拥有的,无需输入更改:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GlareChange : MonoBehaviour {
private Light[] lights;
// Use this for initialization
void Start()
{
// SDK Object Alias|Utilities|90140
namespace VRTK
{
using UnityEngine;
/// <summary>
/// The GameObject that the SDK Object Alias script is applied to will become a child of the selected SDK Object.
/// </summary>
[AddComponentMenu("VRTK/Scripts/Utilities/VRTK_SDKObjectAlias")]
public class VRTK_SDKObjectAlias : MonoBehaviour
{
/// <summary>
/// Valid SDK Objects
/// </summary>
public enum SDKObject
{
/// <summary>
/// The main camera rig/play area object that defines the player boundary.
/// </summary>
Boundary,
/// <summary>
/// The main headset camera defines the player head.
/// </summary>
Headset
}
[Tooltip("The specific SDK Object to child this GameObject to.")]
public SDKObject sdkObject = SDKObject.Boundary;
protected virtual void OnEnable()
{
VRTK_SDKManager.SubscribeLoadedSetupChanged(LoadedSetupChanged);
ChildToSDKObject();
}
protected virtual void OnDisable()
{
if (!gameObject.activeSelf)
{
VRTK_SDKManager.UnsubscribeLoadedSetupChanged(LoadedSetupChanged);
}
}
protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
{
if (VRTK_SDKManager.ValidInstance() && gameObject.activeInHierarchy)
{
ChildToSDKObject();
}
}
protected virtual void ChildToSDKObject()
{
Vector3 currentPosition = transform.localPosition;
Quaternion currentRotation = transform.localRotation;
Vector3 currentScale = transform.localScale;
Transform newParent = null;
switch (sdkObject)
{
case SDKObject.Boundary:
newParent = VRTK_DeviceFinder.PlayAreaTransform();
break;
case SDKObject.Headset:
newParent = VRTK_DeviceFinder.HeadsetTransform();
break;
}
transform.SetParent(newParent);
transform.localPosition = currentPosition;
transform.localRotation = currentRotation;
transform.localScale = currentScale;
lights = Find(typeof(Light)) as Light[];
foreach (Light light in lights)
{
//EYE = illuminance from source, in this case the sun
//q = angle from viewer
light.intensity = (10*EYE/(Mathf.Atan2(currentPosition/SUNPOSITION));
Debug.Log(light);
}
}
}
// Update is called once per frame
void Update () {
}
}
这是来自统一站点的示例,该示例通过变量来更改光强度:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float duration = 1.0F;
public Light lt;
void Start() {
lt = GetComponent<Light>();
}
void Update() {
float phi = Time.time / duration * 2 * Mathf.PI;
float amplitude = Mathf.Cos(phi) * 0.5F + 0.5F;
lt.intensity = amplitude;
}
}
在每个更新周期中,您需要遍历光源,并获得一个世界空间向量,该向量表示光源与头戴式耳机/用户之间相对于世界法线的距离。 这可以用于计算耳机(A),光源(C)和世界法线(B)之间的角度。
注意:根据您的坐标空间,您可能需要交换符号或操作数)
d(light) = light.Position - user.Position
可以通过使用该距离构造直角三角形ABC,然后使用三角函数求解角度ABC的切线来计算角度θ(同样,可能需要根据使用的坐标空间来调整符号和操作数):
tan(θ) = (user.Position.Y - light.Position.Y)/(user.Position.X - light.Position.X)
大概,您的每个光源都有一个基本强度或绝对强度。 使用此基本(绝对)亮度值来计算用户所看到的实际(视在)亮度,如方程式所示:
L(act) = 10 * L(abs)/θ^2
Unity框架可能具有内置的功能,可以为您执行这些任务,但是这些组件或类似组件将在后台执行这些操作。 如果您正在寻找Unity特定的指导,我建议您发布到Game Development SE网站 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.