- Home /
RaycastHit.textureCoord returns zero on Android if mesh is not set to readable
Hello, we are using RaycastHit.textureCoord
to find the UV coordinates of a car mesh collider in order to apply a decal on the car body.
The mesh of the mesh collider is non convex and is taken directly from the car model. Everything works correctly in the editor, but its not in Android build.
Specifically, RaycastHit.textureCoord
always returns zero in the Android build unless the mesh of the mesh collider is set to readable.
Is there any way to avoid this? Enabling Read / write means to double memory occupation.
Answer by Bunny83 · Dec 10, 2019 at 11:33 AM
So you want to read mesh information without having the mesh data actually available in memory? How do you think this should work? If you just upload the mesh data to the GPU memory you can not do any raycasting against the collider and query mesh relevant data (like the UV coordinates). If you need to query texture coordinates from the mesh it has to be readable. There's no way around that.
How large is your mesh that you worry about having it in RAM? Mesh data, even for moderate complex meshes, shouldn't be an issue compared to texture or audio data.
This is something that its not mentioned on RaycastHit.textureCoord
documentation. It only says "Note: A textureCoord requires the collider to be a $$anonymous$$eshCollider".
For what concern enabling read write checkbox, even Unity documentation pushes to avoid this whenever is possible, even by specifying when it should be enabled https://docs.unity3d.com/ScriptReference/$$anonymous$$esh-isReadable.html
In most cases, you should disable this option to save runtime memory usage. You should only enable it under the following circumstances:
When you read from or write to the $$anonymous$$esh data in your code.
When you pass the $$anonymous$$esh to StaticBatchingUtility.Combine() to combine the $$anonymous$$esh at run time. -When you pass the mesh to CanvasRenderer.Set$$anonymous$$esh.
When you use the $$anonymous$$esh to bake a Nav$$anonymous$$esh using the Nav$$anonymous$$esh building components at run time.
When the $$anonymous$$esh is convex, you use the $$anonymous$$esh with a $$anonymous$$esh Collider, and the $$anonymous$$esh Collider's Transform has negative scaling (for example, (–1, 1, 1)).
When you use the $$anonymous$$esh with a $$anonymous$$esh Collider, and the $$anonymous$$esh Collider's transform is skewed or sheared (for example, when a rotated Transform has a scaled parent Transform).
When you use the $$anonymous$$esh with a $$anonymous$$esh Collider, and the $$anonymous$$esh Collider's Cooking Options flags are set to any value other than the default. -When using a $$anonymous$$esh with a Particle System's Shape module or Renderer module when using not using GPU instancing. Note that the Particle System will automatically change $$anonymous$$eshes to readable when assigned through the inspector
Damn right. Thank you.
It was very weird how the textureCoord was returning value in the editor and wouldn't in the build. $$anonymous$$y mesh is just a well rounded sphere, and game isn't very demanding, so now it works. But this needs to be addressed in TextureCoords' documentation.
Edit: It is there, but ONLY in the EXA$$anonymous$$PLE's comments. That is a horrible way of giving such a crucial information. The example is not there for giving such info. It is there for giving an EXA$$anonymous$$PL$$anonymous$$
There is a sort of workaround to this, at least for our use case. We need to access the UV coordinates only in the menu scene, so we use
car$$anonymous$$esh = carBody.gameObject.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh;
car$$anonymous$$esh.Upload$$anonymous$$eshData(true);
to free memory before loading the race scene, where we don't need anymore to access mesh data. Can anyone expert can confirm that this is correct?
Your answer
Follow this Question
Related Questions
Does work the raycast on a mesh collider? 3 Answers
Raycasting will not hit some imported meshes with mesh colliders? 2 Answers
Raycast trail renderer? 3 Answers
Getting Ui image name on mouse click over image 0 Answers
Can i use anything other than Graphics raycasting to check what UI element i press? 0 Answers