Reputation: 135
I am currently developing an interior design augmented reality application in Unity right now and I need to detect the color of the wall where the camera is being directed to but I can't seem to find a solution. Is there any function that will help me in my problem? Big thanks!
Upvotes: 2
Views: 6437
Reputation: 959
You can use a raycast from the cameras position to a forward direction. See the code below as an example from Unity.
void FixedUpdate()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 100.0f))
{
Color color = hit.transform.gameobject.GetComponent<Renderer>().material.color;
print("Object colour: " + color);
}
}
The RaycastHit variable holds information about any objects struck by the ray (invisible line between two points). You can refer to the objects colour by getting the material from the renderer component and then looking up its colour property from that. Something like:
Color color = hit.transform.gameobject.GetComponent<Renderer>().material.color;
*Now added to example above. See the full syntax and possible parameters to use in the raycast here in the Unity docs => Here.
Hope that helps.
Upvotes: 2
Reputation: 9821
You'll first need to create a WebCamTexture
object to begin pulling information from the device's camera. These Unity Docs explain how to do so in the Description section of the class's constructor.
Once you've done that, you can use the GetPixel(x,y)
function to get a color of a specific pixel.
Also, since there's likely to be imperfection in the image and the surface being looked at, I'd suggest taking multiple samples from the texture and averaging them together.
Upvotes: 1