Render a RenderTexture to a mesh - unity3d

In Unity, I'm updating a Render Texture procedurally (via writing data to it) via a DirectX plugin. I do something like the following to initially create my RenderTexture:
RenderTexture myTexture = new RenderTexture (100, 100, 0);
myTexture.Create ();
transform.GetComponent<Renderer> ().material.mainTexture = myTexture;
transform.GetComponent<Renderer> ().enabled = true;
Then later on I modify the texture as needed. Yet this object's material (what it looks like in the real world) doesn't change. If I click on that object, then click on it's material, and click on this RenderTexture attached to it, I can see it updating, just for some reason it doesn't update on the actual mesh. Why is this? I've tried using different built-in shaders, but that hasn't seemed to help. Is there a way to write a shader to render a RenderTexture to a mesh, as one idea?

I found the best option is simply to use a RawImage instead of a Material, and apply the render texture to that RawImage's texture (not mainTexture, just texture). A material can then even be applied to that raw image if you want to use a shader.

Related

How to apply textures to 3d models

I downloaded the 3d character model. But, the model is not applied textures. Is there a way to apply textures to models in Unity?
Or do I have to create a material myself and attach a texture to it?
I tried this method, but it was difficult when there were multiple textures. Any help would be appreciated.
When you import a 3D model into Unity, it will automatically create a material for the model and apply the textures to it.
If you are having trouble with the textures not being applied to the model, there are a few things you can try to fix the issue:
First make sure that the textures are in the same folder as the 3D model. Unity will automatically look for textures in the same folder as the model, so if the textures are in a different one, they will not be applied.
Also check the texture import settings. When you import a texture into Unity, it will automatically assign certain import settings to the texture. These settings can affect how the texture is applied to the model, so it is important to make sure they are set correctly. To do this, select the texture in the Assets window and then go to the Inspector window. In the Inspector window, you will see the texture import settings, which you can adjust as needed.
Yes, most definitely can you apply textures to models in Unity.
As you have guessed correctly, textures are applied via a Material. If you import a model, by default there will be a standard Material assigned to it. However, you can also create one yourself and assign it to the Material list of the MeshRenderer component.
Depending on the shader you choose for your Material, there will be different shader properties, including the ones for textures. Usually, there is a Albedo property which can be used to set a texture. In the editor, you can simply drag and drop your texture on to it. If you want to apply a texture at runtime, you can set the texture with yourMaterial.SetTexture(shaderTexturePropertyName e.g. _MainTex).
does the model come with the textures? If you are trying to use your own textures, it might now work because it's not properly "UV Mapped". Or it might work but it will look messy.
If the textures came with the model, then you can just drag and drop the textures into a new material then assign it to the proper mesh.
I can help you better if you can elaborate more on how you are importing your models.
You can apply mew material with variable color,
if the downloaded model is already mapped you can make your own texture map with any image editing software, though it is hard to tell whats going where, you can start with using a checkerboard to test it out.
if its not uv mapped, you need softwares that can do it, unity does not have the ability to create uvmap. 3d software like maya,blender,c4d,3dmax has the ability to create uv map, there are also specific softwares that are just for uv ,here or texturing like substance painter

How does one Render a texture directly to the Camera in Unity

In Unity, I have 2 images in the form of a texture that I am merging together (one overlaying the other). I do this in a Compute Shader and put the results on a RenderTexture. I want this RenderTexture to be everything that the output camera sees.
I have found articles saying to use a the ReplacementShader property of the camera, but I couldn't get that to properly work.
Currently I have simply put the RenderTexture onto a UIRawImage that covers the whole UI Canvas so that the entire camera is filled. This however has a lot of lag and is obviously a suboptimal solution.
So how does one output the Rendertexture or the compute Shader result directly onto the camera. Thanks.
You could probably use OnRenderImage
Event function that Unity calls after a Camera has finished rendering, that allows you to modify the Camera's final image.
and use Graphics.Blit
Copies source texture into destination render texture with a shader.
and do something like e.g.
// This script goes onto your according Camera
public class RenderReplacement : MonoBehaviour
{
public RenderTexture replacement;
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
// To overwrite the entire screen
Graphics.Blit(replacement, null);
// Or to overwrite only what this specific Camera renders
//Graphics.Blit(replacement, dest);
}
}
Where
dest
The destination RenderTexture. Set this to null to blit directly to screen. See description for more information.
Note as mentioned in the API the OnRenderImage is called after this Camera already finished rendering. So in order to make this more efficient - since we basically throw away that render - simply make sure that camera basically isn't rendering anything by disabling all Layers and e.g. let it only render a single color background

Use the whole screen to display a shader / material

I created a hlsl shader which is rendering a sierpinski fractal using Raymarching. Currently I have assigned the shader to a material, this material is assigned to a cube which I placed in the scene. So the sierpinski fractal is displayed / rendered on the cube geometry.
How can I use the whole screen / camera view to display my shader? I don’t want to add my shader to a material which I assign to a geometry.
In case someone is coming to this question and have the same problem as I had, you can do the following:
use a Graphics.Blit to execute a certain shader
store this information in a RenderTexture
assign this RenderTexture to e.g. a Canvas RawImage.texture

Parts of mesh become invisible at certian angles Unity3D

I am importing an fbx model but parts of it are invisible in the scene And game at certain angles.
Attached image SS-1 = the full model - You can see that some parts of the mesh are invisible Attached image SS-2 = closeup of an invisible mesh selected
Attached image SS-3 = Main Camera Settings
Attached image SS-4 = Model import settings
Any ideas what's going on?
The normals of your mesh are not set properly, so the culling algorithm treats it as a back-face that should not be rendered.
If you can edit the model so that you can inverse the normals that would work. Most modeling tools have convenient tools or direct routines for "flipping normals". However, if that is not possible, a trick is to change the culling settings from your material: When the culling mode is set to Cull Back (which is the default setting), the polygons that are not facing the camera are not rendered. Then, for the mesh that is not visible, you can change the culling property from Cull Back to Cull Front. This way it will be visible.
The caveat is that most of the time material properties might be overlooked as Cull Front and No Cull settings are not as common as Cull Back. Also, performance-wise, you will have a different shader running because of that mesh.
Try two-sided shader, as suggested here
The easiest solution I found for this problem is:
1.Select the object that's going invisible on certain angles.
2.Navigate to Mesh Renderer > Render Face > Both
The problem should be solved now
My Unity version is 2019.3

How to use free particles available in the unity store after importing?

I wanted some particle textures for my particle system. I looked up in the unity store and there were some free ones, so I imported them. But when I go inside their textures folder, the textures are full block like images and not like the single transparent texture.
I researched before asking this question but couldn't find anything related to it. My scene is bright so I can't use black background block like textures.
Do I need to edit them further like cropping and removing background? Or am I doing something wrong?
Try this two ways.
(1) Can you try it by creating new material. Just assign a material to the image you want to use.
To create a material==> Assets -> Create -> Material
give name it, adjust it.
"Create a new material and the apply the texture by clicking the blank box next to albido, then selecting the desired texture."from here
done! Assign it to particle system.
(2) Just assign your texture to particle system. In particle import setting try Alpha Is Transparent (In Inspector) checked/unchecked.