• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Gruffy · Jan 17 at 09:28 PM · cullingthreadingmultithreadingfrustumfrustrumculling

Frustum Culling using Job system not culling as expected

I have a frustum culling script I've tried to convert over to Jobs, Being new to Jobs and Unity job system as a paradigm, I'm not entirely sure why it isn't working, but it isnt... Could anyone analyze it and give me a heads up as to why it isnt functioning the way i expect it to, which is as follows: calculate the frustum as planes. Use these to determine if an object is inside or not, inclusive of a tolerance to include anything just outside the frustum planes iterate the objects, set each Active according to being in the view or not. The full code is below and Id love some help, so thanks in advance of any attempters

 using Unity.Collections;
 using Unity.Jobs;
 using UnityEngine;
 using UnityEngine.Jobs;
 using System.Linq;
 using UnityEditorInternal;
 using System;
 using Unity.VisualScripting;
 
 public class FrustumCullJobs : MonoBehaviour {
     // The cam whose frustum we will use for culling
     public Camera cam; //assigned in inspector , used to calculate frustum
 
     // The layer mask of the objects we want to cull
     public LayerMask cullingLayerMask;
 
     // The maximum distance from the cam at which objects will be culled
     public float maxDistance = 50f;
 
     public float frustumTolerance;
     // Flag to determine if inactive objects should be checked for occlusion
     public bool checkInactiveObjects = true;
 
     private TransformAccessArray _objects;
     private FrustumCullJob _frustumCullJob;
     private JobHandle _frustumCullJobHandle;
     private void Start() {        
         Transform[] ts = gameObject.GetComponentsInChildren<Transform>(checkInactiveObjects).Where(t => cullingLayerMask == (cullingLayerMask | (1 << t.gameObject.layer))).ToArray();
         _objects= new TransformAccessArray(ts);
     }
     private void OnDestroy() {
         _objects.Dispose();
     }
 
     private void Update() {
         _frustumCullJob = new FrustumCullJob() {
             objects = _objects,
             planes = GeometryUtility.CalculateFrustumPlanes(CameraHelper.GetCamInstance),
             maxDistance = maxDistance,
             tolerance = frustumTolerance
 
         };
 
         _frustumCullJobHandle = _frustumCullJob.Schedule(_objects);
         _frustumCullJobHandle.Complete();
     }
 
     private struct FrustumCullJob : IJobParallelForTransform {
         public TransformAccessArray objects;
         public Plane[] planes;
         public float maxDistance;
         public float tolerance;//set by user in inspector
 
         public void Execute(int index, TransformAccess transform) {
             NativeArray<int> activeObjects = new NativeArray<int>(objects.length, Allocator.Temp);
             int activeCount = 0;
             //_objects.length is zero check
             Debug.Log($"{objects.length > 0 }");
             // Iterate through the frustum planes
             for (int i = 0; i < planes.Length; i++)
             {
                 // Modify the normal of the plane by the frustumTolerance value
                 planes[i].normal += planes[i].normal * tolerance;
             }
             for (int i = 0; i < objects.length; i++) {
                 if (GeometryUtility.TestPlanesAABB(planes, objects[i].GetComponent<Collider>().bounds)
                     && Vector3.Distance(objects[i].position, CameraHelper.CamPosition) > maxDistance) {
 
                     print("inside geom calc");
                         activeObjects[activeCount++] = i;
                 }
             }
             for (int i = 0; i < activeCount; i++) {
                 objects[activeObjects[i]].gameObject.SetActive(true);
             }
 
             for (int i = activeCount; i < objects.length; i++) {
                 objects[i].gameObject.SetActive(false);
             }
             activeObjects.Dispose();
         }
     }
 }
 



The CamerHelper class provides access to the main camera in the scene for any script calling it and allows for thread-safe access in the jobs Execute() function, the class is below for clarity...

 using UnityEngine;
 using UnityEngine.InputSystem.LowLevel;
 
 public class CameraHelper: MonoBehaviour{
 
     private static Vector3 camPos;
     private static Transform t;
     private static Camera cam;
 
     private void Awake() {
         t = transform;
         cam = t.GetComponent<Camera>();
     }
     public static Vector3 CamPosition{
         get {
             camPos = SetPosition();
             return camPos; 
         }
         set{
             if (value == camPos)
                 return;
             camPos = value;
         }
     }
 
     static Vector3 SetPosition() {
         return new Vector3(t.position.x, t.position.y, t.position.z);
     }
 
     public static Camera GetCamInstance {
         get {
             if (cam == null)
                 cam = Camera.main;
 
             return cam; 
         }
     }    
 }
 

As mentioned, any help would be much appreciated as its my first time getting familiar with the Jobs system and practices in Unity that can take advantage of them many thanks :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2.5D billboard shader and frustum culling problem 2 Answers

How to disable frustum culling completely? 0 Answers

How to check if code is running on the unity thread? 1 Answer

Visualize frustrum culling in Editor 1 Answer

MonoBehaviour.Invoke and Threading 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges