I am trying to set the texture of a shader graph material and am having no luck. After following some tutorials and some googling I can't seem to get it working.
So far I have set my scene up with a plane i generated via code and a pre-built one and have applied the material to both of these. I then created a script to set the texture of the MeshRenderer to "_MainTex" as I saw mention several times while googling. I also set the material texture property to "_MainTex".
I also tried the Reference name as well but that also didn't work.
So how do you set the texture of a shader graph material with Universal Render Pipeline via code?
The two planes ingame:
Prebuilt plane's inspector:
Shader Graph:
The script:
void Start()
{
GetComponent<MeshRenderer>().material.SetTexture("_MainTex", new TextureGenerator().GenerateNoiseTexture(256, 256));
}
You need to fill out the "Reference" section for this property.
It is found in Graph Inspector -> Node Settings.
(usually in the top right of the ShaderGraph)
Be sure to change "Texture2D_234E865" to "_MainTex" there. Verify the asset saved by clicking on the shader and looking in the inspector. Under Properties it should list _MainTex. Then you can use your SetTexture code or simply call material.mainTexture, which does the same thing.
Related
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
I am using HDRP.
I have this PBR Shader Graph:
But the result I get is this:
The Shader is not showing up on the sphere at all.
It seems that the material of the sphere does have a shader, but it's probably one of the build-in ones. You need to assign your shader to the material the sphere is using. There are two ways that you can do that. One is to grab the shader and drop it on the material from the project tab. The second way is to select the material and on the top of the properties tab, there is a dropdown next to some Text saying "Shader". Click it and select your shader. If you want a visual representation of how to do that, you can check this video out at 6:40 (by Brackeys). If you have further question, feel free to ask.
I'm using Unity 2020.1.3f1's URP, with the new 2D renderer system.
As of right now, I have objects that change between the built in "Sprite-Lit-Default" material, and a material with the custom built pixel outline shader detailed here: https://danielilett.com/2020-04-27-tut5-6-urp-2d-outlines/
This worked well and good, but I recently added lights, and a normal map to my sprites as a secondary texture in the import settings. The default lit texture has no problems displaying the normal map, but when I attempted to modify my shader graph to include the normal map, it doesn't import like the sprite texture does when _MainTex is set as the reference.
I've tried _NormalMap (which is the name of the secondary texture in the importer!) as well as _NormalTex, but it always ends up not importing the normal map. I even attempted changing _MainTex to a Texture2D, but given that kept sparking an error, I didn't think it was the right way to go about it. (This one to be specific.)
Error assigning 2D texture to 2DArray texture property '_MainTex': Dimensions must match
UnityEditor.EditorApplication:Internal_CallUpdateFunctions()
Am I missing something here? All the tutorials I can find online only show people dragging the normal map in through the inspector, but this material is going to be used by many different sprites, so that seems...counterintuitive.
On top of this, the default material/shader has no issues with this, so I feel like I'm either missing something, or I'm going to end up having to code my sprites to change material through code instead of the animator, just for this small, annoying quirk..
Blackboard Properties, and nodes. This just goes into the normals input.
Inspector panel showing the missing normal map slot.
And the Secondary Textures in case I somehow misnamed it, why not?
(EDIT)
So, an update on this, for anyone else who runs into this same issue.
I managed to find a section of the shadergraph documentation that seems to be the only part talking about this:
It is required to name the reference for MainTex as _MainTex to render Sprites. It is also recommended to name the references for Mask as _MaskTex and Normal as _NormalMap to match the Shader inputs used in this package.
So from what I gather from that, _MainTex is the only one that's automatic in ShaderGraph.
After a full day of looking up tutorials, I've noticed that every single one of them simply set the normal map and extra textures as the default textures so they'll show up without being assigned manually.
I think this is possible with hand-written shaders, but I've decided to just go with a simple unlit shadergraph on a hand-drawn sprite outline, displayed on a separate gameobject parented to the main object.
I'm not posting this as an answer in case someone else finds a solution to it in the future, and since this isn't...really a solution in my eyes.
I don't know if you have figured this out yet but I'll try to answer since I had the same problem. Create a new Texture2D node, convert it to a property, and have the reference as _NormalMap, connect that to the sample texture 2D node as normal, and plug that into the sprite lit master. Now go into the sprite editor, assign the normal maps as a secondary texture, and make sure the name is the same as in your shader, _NormalMap (or something else, as long as it's the same). This currently has worked for me, shader graph detects the normal map texture by reference automatically. Attached below are some images to help and the finished result on the character sprite, which uses a custom shader I picked up from a tutorial and added the normal map to.
Shader Graph Sprite EditorSprite Editor 2 Normal Map result
I've recently imported a Unity project that references .fbx file.
When I imported the project (which was created by another user) all surfaces are pink.
Having looked at all the materials the following is observed
All the materials in the top level materials are present but are shades of grey not as the material should appear
At specific assest level (assets->material_name->material) there are no materials
All textures are present
All objects have a material associated with them which are all from the grey materials.
I can force change a material by selecting the surface and manually changing the material from its default to the one material which isn't grey "teleport" for all three surface materials.
There is a seemingly related error at the bottom "speedtree materials need to be regenerated". I've googled this however, the only solution was to select the prefab option in the assets list which is not an existing option in my project.
Any thoughts would be awesome.
CBusBus
This is a shader problem. Please note that shader is not the-same as material. You plug shader into a material. The circled image below shows a "Standard" Shader that is attached to a material named "CubeMat2".
The imported Object can be pink because:
1.The shader is missing in the Project.
Manually copy the shader into your project and re-attach it to your material.
2.The shader is obsolete or uses a keyword that's not available your current version of Unity.
Make sure you are using the-same version of Unity "another user" used to create the original project.
3.A simple import bugs. This happens somethings.
All you have to do is select the material, change the shader to another Shader then change it back to what it was before. For some reason, this seems to fix the issue.
4.There is an error in the shader. If there an error in the shader, Unity can't compile it and it will be pink when attached to a material. Also, if there is a shader error, you will see that in the Console tab. You will have to fix that error.
I have a material called "RockyMountain" to which I have assigned a specific texture through the inspector, (Main Maps > Albedo property). The shader is set to "Standard" and the rendering mode is set to "Opaque". The other properties have default values. In the preview panel under the properties, the material is displayed appropriately with the texture being applied and all.
The problem is: when I apply this material to any game object, the object does not display the texture of its material. It merely shows a solid color that seems to be a shade of a color in that same texture.
Does anyone have any idea what I'm doing wrong?
You have to UV unwrap your object first, basically tell the object what it looks like and how textures should be applied. I doesn't know this on its own!
Here's a tutorial to get you started: https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/UV_Map_Basics