gpt4 book ai didi

c# - 另一个游戏对象背后的粒子系统渲染

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

首先,我希望你能听懂我的英语。

我使用源代码手动将相机的投影更改为正交投影。请引用下面的代码。

using UnityEngine;
using System.Collections;

public class CameraOrthoController : MonoBehaviour
{
private Matrix4x4 ortho;

private Matrix4x4 perspective;

public float near = 0.001f;

public float far = 1000f;

private float aspect;

public static CameraOrthoController Instance
{
get
{
return instance;
}
set { }
}

//-----------------------------------------------------
private static CameraOrthoController instance = null;
//---------------------------------------------------
// Use this for initialization
void Awake()
{
if (instance)
{
DestroyImmediate(gameObject);
return;
}

// 이 인스턴스를 유효한 유일 오브젝트로 만든다
instance = this;
}

private void Start()
{
perspective = Camera.main.projectionMatrix;
}

public void StartMatrixBlender(float OrthoSize)
{
aspect = (Screen.width + 0.0f) / (Screen.height + 0.0f);

if (OrthoSize != 0f)
{
float vertical = OrthoSize;
float horizontal = (vertical * 16f) / 9f;

ortho = Matrix4x4.Ortho(-horizontal, horizontal, -vertical, vertical, near, far);

BlendToMatrix(ortho, 1f);
}
else
{
BlendToMatrix(perspective, 1f);
}
}

//---------------------------------------
private Matrix4x4 MatrixLerp(Matrix4x4 from, Matrix4x4 to, float time)
{
Matrix4x4 ret = new Matrix4x4();
int i;
for (i = 0; i < 16; i++)
ret[i] = Mathf.Lerp(from[i], to[i], time);
return ret;
}

IEnumerator LerpFromTo(Matrix4x4 src, Matrix4x4 dest, float duration)
{
float startTime = Time.time;
while (Time.time - startTime < duration)
{
Camera.main.projectionMatrix = MatrixLerp(src, dest, (Time.time - startTime) / duration);
yield return new WaitForSeconds(0f);
}
Camera.main.projectionMatrix = dest;
}

//-------------------------------------------------
private Coroutine BlendToMatrix(Matrix4x4 targetMatrix, float duration)
{
StopAllCoroutines();
return StartCoroutine(LerpFromTo(Camera.main.projectionMatrix, targetMatrix, duration));
}

//-------------------------------------------------
public void OnEvent(EVENT_TYPE Event_Type, Component Sender, object Param = null, object Param2 = null)
{
switch (Event_Type)
{

}
}
}

我就是这样使用代码的。

CameraOrthoController.Instance.StartMatrixBlender(OrthographicSize);

目前为止效果很好。但是,当我为效果添加粒子系统时出现了问题。

The screen where the problem is occurring

在正常状态下,效果出现在游戏对象的前面,如上图底部的场景屏幕所示。

但是如果我用我上面写的代码来操作camera,效果总是会被所有gameobject遮挡,就好像在游戏画面的最上方一样。尽管效果位于游戏对象的前面。

起初,我认为可以通过图层排序来解决它,但我认为这不是图层问题,因为它在正常相机条件下是可见的。

我想知道上面代码的问题在哪里,因为我必须使用它们。

如果您知道如何解决,请告诉我。谢谢。

最佳答案

当您修改 Camera.projectionMatrix 时,相机将不再根据视野更新其渲染。粒子将保留在 GameObject 后面,直到您调用 Camera.ResetProjectionMatrix()这结束了设置 Camera.projectionMatrix 属性的效果。


如果这不起作用,请使用多个摄像机使粒子系统始终出现在 3D 对象的顶部。基本上,您使用主相机渲染 3D 对象和其他对象,然后使用另一个相机渲染粒子系统。

图层:

1.新建图层并命名为“Particle”

2.将Particle System层更改为Particle

enter image description here

主摄像头:

1.确保主相机的Clear Flags设置为Skybox

2。将Culling Mask 更改为“Everything”。单击 Everything,这是 Culling Mask 的设置,然后取消选择/取消选中 Particle

enter image description here

3。确保其深度设置为0

相机此时不应渲染粒子系统。

新相机:

1.创建新相机。确保它与主摄像头处于相同的位置/旋转。同时删除附加到它的 AudioListener

2。将 Clear Flags 更改为 Depth only

3.将相机的Culling Mask更改为Particle,并确保“Cu​​lling Mask”中没有选择任何其他内容

enter image description here

4。将深度更改为 1

这将使粒子系统始终显示在使用第一个或主相机渲染的每个对象的顶部。


如果您希望粒子系统出现在 Sprite/2d 对象而不是 Mesh/3D 对象之上,请将粒子的 RenderersortingOrder 更改为更大比 SpriteRenderer 的 sortingOrder。默认值为 0,因此将 Particle 的 sortingOrder 更改为 1 或 2 应该没问题。

particle.GetComponent<Renderer>().sortingOrder = 2;

关于c# - 另一个游戏对象背后的粒子系统渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50254797/

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