I was looking for this answer on the web for some time. But apparently my search phrases were wrong or it is really that hard. But I doubt it.
Imagine, I have any 3d-body. A sphere, cylinder or cube.
I want to put a text onto that object. As if it was a sticker or painted onto that object. Meaning, it would follow the objects curves or edges, if wrapping around them. I managed to create some texts which are ALWAYS in front or behind my object. But that is not what I want.
The closest to what I want looks like this:
I cannot believe, that I need to code for this. It must be achievable via child/parents, canvas and text, JUST using the inspector and components.
Is this so ?
Not that simple but here is how you can achieve something similar. This requires your 3D models to be correctly UV mapped so you can simply apply a flat texture to it. The setup may sound complex but it is really cool ;)
Create a
RenderTexture
: In theAsset
do right mouse click →Create
→RenderTexture
and call it however you like e.g.TextTexture
size
to e.g.2048*2048
depedning on your needs of course.Create a new Material
RenderTexture
asAlbedo
RenderingMode
toFade
(in order to later have it's background transparent)Create a new Layer and call it e.g.
TEXT
In you normal main
Camera
underCulling Mask
exclude the just createdTEXT
layer. So it will not render our Text contentAdd a new
Camera
to your scene (it will only render the text) and call it e.g.TextCamera
and make the following settings:
AudioListener
componentClear Flags
→Solid Color
Background
→ Color actually doesn't matter but make sure to set the Alpha level to0
!Culling Mask
→ Nothing except the createdTEXT
layerTarget Texture
→ The createdRenderTexture
Now you already have a Material with a dynamically changeable Texture with transparent background and whatever content you like. So lets make it e.g. a UI.Text
To your scene (I did it simply as child of the
TextCamera
so I can simply move it out of sight in the SceneView while working on other stuff) add aText
(including the Canvas etc - Unity usually adds it automatically)TEXT
so they will not be rendered by the normalCamera
but only by theTextCamera
.Canvas
usesRenderMode = WorldSpace
(it won't work with overlay canvas)!Canvas
about e.g.3
units in front of theTextCamera
(or wherever you like so the Text is visible in the texture later)Text
RectTransform
setwidth = 1000
,height = 1000
,Scale = 0.001, 0.001, 0.001
Text
component setFont Size = 300
Raycast Target
optionAnd now you can simply apply the created material to your 3D Object and hit play and should see that it gets completely transparent except having the text on it.
So in order to use it as overlay for a 3D object you could e.g. simply duplicate the original object, call one e.g.
Inner
the other oneOuter
and makeInner
a child ofOuter
. Now on theOuter
you set our Text material. This works since a material usingFade
as render mode is rendered on a different render chain which is rendered on top of the default one.→ Tadaaa 3D object with Text applied to its surface and can even dynamically change the text and its properties like color etc
The whole thing dynamic (Except creating Layers)
Since you asked: Yes you can make this all in a script ... except creating new Layers! This is not possible on runtime!
So you have to know all the Layers you will use beforehand then you can do something like
Basically reproducing all the steps from before. Since we can't create or edit Layers on runtime you have to know them beforehand and enter it as
LayerToUse
.I created every thing as child of the original object so it is easy to control and change it also later on runtime.