Get the object or renderer displayed (rendered on top) - unity3d

Is it possible to know I a mesh renderer is not visible due to the fact its being occluded by another gameObject? Up to what I researched, Rederer.isVisible responds to "isRendered" and scene params needs to be baked. So I wonder if there could be a simple way to know if an scene element is rendered on top of other in a simple manner. Larger explanation below.
Consider a simplified use case, with a camera (with occlusion culling activated), an occluder and an occludee such as this:
With the shadows options disabled (for both occluder and occludee. I would think disabling this is only needed for the ocludee but disabled for both just in case):
And a simple script to check visibility options:
using UnityEngine;
public class RenderCheck : MonoBehaviour
{
MeshRenderer mr;
private void Start()
{
mr = GetComponent<MeshRenderer>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) {
bool isVisible = mr.isVisible;
Debug.Log($"cube visible: {isVisible}");
}
}
void OnBecameVisible() {
Debug.LogError($"{this.GetHashCode()} VISIBLE!!");
}
void OnWillRenderObject() {
Debug.LogError($"{this.GetHashCode()} VISIBLE!!");
}
}
The result I expected is that the ocludee .isVisible would return false, and that OnWillRenderObject() would be executed constantly after OnBecameVisible() is executed on the event of the occludee been moved out the occluders back, so into the sight of the camera. However I get .isVisible = true and the log thrown by the OnWillRenderObject().
I am aware of the fact that the Scene view cameras will also cause this value to be true. The explained outcome considers no scene camera and only the gamePlay camera (with occlusion culling activated).
I got this approach to work baking the scene occlusion options following the documentation steps. But my question here more if its rendered or not regarding the oclussion settings, is to know if a gameobject or a mesh is rendered "on top" of other (in the case of culling not being applied). Unity needs to handle that as it is how the render takes place in the scene.
There seem to be a bunch of useful options here for what I am after, however, for .isVisible to work I need to set and bake the occusion scene options (afaik). So .isVisible responds more to "isRendered"
fact. According to the documentation shouldn't Renderer.isVisible be giving the result I expected in a direct/simple manner and not be influenced by the fact of being rendered or not by dynamic occlusion?
Edit: As far as I checked, seems that only a raycast can address blocking by geometry. All these other things mean "visible" in the sense that the camera's rendering pipeline has to consider the object in some way.
With a raycast I can get the "physical" block, determined by a collision. Isn't there any way to determine the "graphical" block? As I said, that is figured out somewhere soas to be renedered accordingly in the display screen, so is that info available somehow?

Related

How to properly use prefabs from AsssetBundles?

I've recently encountered AssetBundles and tried to implement it in my project. I have a pretty simple game where controlling a character you should collect coins. I created the AssetBundle where I made prefabs and put everything from the game scene (background, player, terrain, etc...) into the AssetBundle. However, when loading objects from the bundle to the game scene, despite having the same size and transform parameters in the inspector, they are bigger than their original prefabs when starting a game. When it comes to the loaded character, not only is it ten times the size of the original but it also needs to be readjusted in script dependencies during the game to control it with a joystick. In terms of prefab size discrepancy, I think it has something to do with the loading screen as everything that comes out of the bundle is scaled to its size (see Fig.1) but I don't know why it happens nor how to fix it.
The script which loads prefabs:
public class LoadAssetBundles : MonoBehaviour
{
AssetBundle loadedAssetBundle;
public string path;
public string assetName;
void Start()
{
LoadAssetBundle(path);
InstantiateObjectFromBundle(assetName);
}
void LoadAssetBundle(string bundleUrl)
{
loadedAssetBundle = AssetBundle.LoadFromFile(bundleUrl);
}
void InstantiateObjectFromBundle(string assetName)
{
var prefab = loadedAssetBundle.LoadAsset(assetName);
Instantiate(prefab);
}
}
Try setting your prefab root (the most outer gameObject) scale to 1, them rescale your sprites inside it.
If you have a CanvasScaler on your canvas, the canvas scale will probably be lower than 1, something small like: 0.02321f, your object seems to have a high scale like 107.f or something, this may be the cause of the discrepancies.
When instantiating, try passing a parent transform (where you want to place your prefab) and change the bool instantiateInWorldSpace (on/off) and see how it affects your prefab.
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
Check docs: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Unity Hand tracking issue on the quest with UI raycasting

I'm working on a Unity project, trying to test the UI interaction on the Quest II with hand tracking and ray casting.
I've set up a very basic scene with all the default assets (OVRCameraRig, UIHelper) and added just a button to test the UI interaction.
This is what my scene looks like:
The issue I have is, when I run the scene, the Ray is rotated 90 degrees, and it's attached to the wrist for some reason. I made a video here to show what I mean :
https://www.youtube.com/watch?v=5f12yfpugB8
It's still interacting with the UI though.
Then after watching some online tutorials, I commented out these lines in the HandInputSelector.cs, which was attached to the UIHelper:
void SetActiveController(OVRInput.Controller c)
{
/*
Transform t;
if(c == OVRInput.Controller.LTouch)
{
t = m_CameraRig.leftHandAnchor;
}
else
{
t = m_CameraRig.rightHandAnchor;
}
m_InputModule.rayTransform = t;
*/
}
and instead added a 2nd script to the UI helper, with these lines only:
public OVRHand hand;
public OVRInputModule inputModule;
private void Start()
{
inputModule.rayTransform = hand.PointerPose;
}
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
https://youtu.be/q3d0eG2LwY0
My Unity version is 2021.3.1f1
Can someone please tell me what I'm doing wrong?

Check if a gameobject is visble on the current camera

I've been searching stack overflow and the internet for a bit trying to search for a solution to this issue, everytime i try to use OnBecameVisible() or OnBecameInvisible() or TestPlanesAABB to check if the object is not visible through the wall, the camera can still see the object through a solid wall.
Video of the problem here:
https://youtu.be/3HiEugm6On8
As you can see, if i'm looking at him he stops moving, if i turn around and he "unloads" or "becomes invisible" he moves closer, but if i go around a corner still looking in his direction he stops moving as if i can see him and there is no wall there, this is what i'm looking to solve
it's an enemy that roams around, and i want him to only move if i cannot see him, which i thought would be fairly simple, but alas it seems not to be as simple as i thought
my current code is basic:
public bool IsSeen = false;
public void OnBecameVisible()
{
Debug.Log("I can now see you");
IsSeen = true;
}
public void OnBecameInvisible()
{
Debug.Log("I can't see you");
IsSeen = false;
}
this is attatched to the object that i wish to detect / not detect through walls, which i do believe checks if the object is viewable by the camera that i choose.
does anyone have any ideas as to how i can fix / achieve this?
As already mentioned the general "problem" with Renderer.OnBecameVisible is
Note that object is considered visible when it needs to be rendered in the Scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the Scene view cameras will also cause this function to be called.
So its not really usable for you.
is there a simple way to raycast the whole screen?
Unfortunately not really :/
You can a bit avoid this using GeometryUtility.CalculateFrustumPlanes in order to get the four planes of the camera frustrum. Then you can check whether the object is actually inside the frustrum using GeometryUtility.TestPlanesAABB
var cam = Camera.main;
var planes = GeometryUtility.CalculateFrustumPlanes(cam);
var objCollider = GetComponent<Collider>();
if (GeometryUtility.TestPlanesAABB(planes, objCollider.bounds))
{
Debug.Log("I am inside the camera frustrum!");
}
else
{
Debug.Log("I am out of sight...");
}
However, this still does not cover any other object being in front of the target object and therefore actually covering it.
You would need to define exactly what visible means (e.g. any portion of the mesh? Is the center of the object enough to test? etc).
For e.g. testing only the center of the object you could use a Physics.Linecast like e.g.
if (GeometryUtility.TestPlanesAABB(planes, objCollider.bounds))
{
Debug.Log("I am inside the camera frustrum!");
if(Physics.LineCast(cam.transform.position, objCollider.GetComponentInChildren<Renderer>().bounds.center, out var hit)
{
if(hit.gameObject != objCollider.gameObject)
{
Debug.Log("..but something else is in the way..");
}
else
{
Debug.Log("Now you see me, muhaha!");
}
}
}
If you want it to be more precise and track of any part of the mesh is visible then it actually gets tricky. You would need to raycast multiple key points of the bounding box (e.g. each corner, centers of the edges etc) depending on your needs.

Why 'Is Kinematics' works different on HoloLens?

Overview
I wanted to have a cube, that I can drag around the scene with the components Collider, Rigidbody and ObjectManipulator. In play mode everything works fine, but running it on the hololens, the cube starts flying around after dragging it a few time.
Steps to reproduce (All components are set via editor, not via code)
Create new project and set it up for AR/HoloLens
Import MRTK 2.4
Create cube with a Box Collider. Set Is Trigger = true
Add Rigidbody to cube. Set Use Gravity = false and Is Kinematic = true
Add Object Manipulator to cube. I have a method getting called after On Manipulation Ended, but don't know if thats important.
Expected behavior
The rigidbody is set to Is Kinematic = true and Use Gravity = false, so that the cube stays still/stops moving after releasing dragging it. This works while trying it inside the unity play mode. But running it on the hololens, the cube behaves like Is Kinematic = false and starts flying around after interacting with it. Sometimes after the second drag and sometimes after the third time drag.
Error
Before updating this post, I didnt noticed the development console in left corner of my hololens. At the beginng of the scene I get the message [Physics.PhysX] BV4 midphase only supported on intel platforms but at that moment everything is fine. As the cube begins to fly around I get the a NullReferenceExeption: Object reference not set to an instance of an object.
I fixed my issue. I know the approximate cause, but I do not fully understand it. The method, getting called after OnManipulationEnded caused that.
I have a list, getting filled and drained by OnTriggerEnter/-Exit (exit looks the same, except add→remove):
private void OnTriggerEnter(Collider other){
if (other.gameObject.layer != 31) return;
_objectsCollidingWith.Add(other.gameObject);}
OnManipulationEnded triggered this method:
private int GetMeshes(List<KeyValuePair<Transform, Mesh>> transMeshes){
foreach (GameObject go in _objectsCollidingWith)
{
transMeshes.Add(new KeyValuePair<Transform, Mesh>(go.transform , go.GetComponent<MeshFilter>().mesh));
}
return transMeshes.Count;}
So I got alot of nullreferences from GetMeshes, because some gameobject in the list _objectsCollidingWith were null. Thats because the mesh is getting updated every once in a while. That caused a lot of nullreferences until the cube just flew away.
I used the whole time the logging provider via the device portal and couldnt see what is causing this errors. But after running the project via holographic emulation I could see in the console where they were coming from.
How did I fixed my problem?
I found this post because I realized that my OnTriggerExit didn't get called and cased having null objects and some spatial meshes with the same name triggered OnTriggerEnter very often. Also I added this line in the foreach loop in GetMeshes because once in a while there is still a null object:
if (go == null)
continue;
PS: Please forgive the strange code formatting, somehow the editor here on so does not allow me to place the brackets somewhere else

How to disable a collider when the trigger of another collider has been entered?

I am building a game where the player runs on a path. When the player triggers a collider, 2 enemy objects will spawn.
What I want is when the first collider trigger has been entered, I want the second collider, which is at a certain distance from the first collider, to get disabled for a certain time. How to achieve this?
If you'd like to disable the colliders so they won't hit or rebound off the wall, for example, then you can change your collider's "isTrigger" variable to true, to change it into a trigger volume instead of a solid collider. This has the effect of disabling it - in that it won't cause other objects to stop or rebound if they hit it.
For example:
function Update() {
if (Input.GetKeyDown(KeyCode.X)) {
collider.isTrigger = true;
}
}
Note that things like MouseOver still work.
If you want to disable that completely, you can try collider.enabled = false. I'm not sure if that works or not. If it doesn't, you can always scale down your collider:
var myOldSize:Vector3;
function DisableBoxCollider(myCollider:BoxCollider)
{
//actually just resizes it
myOldSize=myCollider.size;
myCollider.size=Vector3(0,0,0);
}
function EnableBoxCollider(myCollider:BoxCollider)
{
if(myOldSize!=Vector3(0,0,0))
myCollider.size=myOldSize;
}
You can use the above code to integrate it in your own project. I'm not going to spill out all of the code for you because else we'd miss the point of learning to program and post on Stackoverflow in general. But it should help you to get on your way. Try and play some with the code, and if you have questions, get back here and ask them, providing the question with some code to show what you have tried.