So what I want to do is to highlight all objects surfaces within a radius around a central object.
I considered using
Collider hitColliders = Physics.OverlapSphere(center, radius); to access all objets within a certain range.
The problem is, that I don’t want to change the whole texture but only its pixels within the given range, so that just some parts of the objects texture is changed.
I am allready familiar with the
SetPixels() function, but I don’t know how to access only the pixels in the given distance.
Take a look at the following picture:
The cube on the left is not within the radius and should therefore not be effected. The cube in the front is inside the sphere and its whole surface should be highlighted in some way. The third cube is only partly inside the sphere, so some of its surface should be highlighted and the outside should remain as it is.
Please note, that I allready considered using spot lights, but it does not give me the results I am looking for.
I found a solution to my problem. A very cost efficient way is to use decals for this.
There is no build in solution to do this, but you can choose between some solutions in the asset store.
This one is free and you can set an angle, so the decal is wrapped all around the object. The downside is, that it can not be applied dynamically at runtime.
I ended up using this approach from the Unity documentation. It is not perfect, but a good and free start point.
here is what you have to do in order to see a boundary painted on the surrounding objects.:
- Download the RenderingCommandBuffers.zip file and copy the deferredDecals folder in your project.
- create a new material and select an appropriate Shader (Decal/DecalShader)
- Set the rendering path of your main camera to Deferred
- assign the DeferredDecalRenderer.cs script to any visible object in the scene and apply the standart cube mesh
- create an empty gameobject and assign the decal.cs script to it, then choose your previously created material
I created a red circle as a texture for my material and get this result:
You have to experiment with adding more decals and rotating them so cover all edges in this solution. But it works great so far.