视野中各物体所占的可视面积为整数

问题描述 投票:0回答:1

奇怪的用例。我希望能够得到在代理的视野内的每一个物体 该物体所占的视野比例。 基于对 这个 问题,我设法获得了一个代理的视野内的所有对象。我已经提供了下面的代码。现在,我需要找出这些对象在可视区域内各占多少位置,我对unity和C#相当陌生,完全不知道从何下手。 我对unity和C#相当陌生,完全不知道从哪里开始。任何帮助将是非常感激的 我还提供了下面的图片来帮助解释。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
using MLAgents.Sensors;
using UnityEngineExtensions;

public class FovObjects : MonoBehaviour
{
    public Camera cam;

// Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        string allVisibleObjects = "";
        foreach (GameObject go in GameObject.FindGameObjectsWithTag("arena"))
        {
            Transform[] allChildren = go.GetComponentsInChildren<Transform>();
            foreach (Transform child in allChildren) {
                Vector3 screenPoint = cam.WorldToViewportPoint(child.position);
                bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;

                if(onScreen) {
                    allObjects += child.name + "-";
                }
            }
            Debug.Log(allVisibleObjects);
        }
    }
}

这只是为了演示的目的。%并不准确。

enter image description here

unity3d rendering agent fieldofview
1个回答
1
投票

这个问题在这里已经有了答案。https:/answers.unity.comquestions691066how-to-calculate-screen-area-coverage-of-a-3d-obje.html。

基本上,你只需将这个对象渲染到一个叫做 渲染纹理 用一个已知的背景(例如只有黑色像素),然后你看看有多少像素不是你的背景。不过我还没有在最新的Unity版本中测试过。

需要注意的是,这个功能现在也可以在Unity免费版中使用,因为现在所有的功能都可以在免费版中使用(除了闪屏移除)。

© www.soinside.com 2019 - 2024. All rights reserved.