How to assign a texture and shader

I am trying to procedurally draw a triangle given 3 coordinates. I have the triangle drawn ,but I don’t know how to assign a texture and shader to the triangle. Here is my attempt:

var shader : Shader;
var texture : Texture

I don’t know how to assign my texture to the variable “texture”. I tried to do this:

var texture : Texture = ("RockDark")

Also, when It comes to shaders, I have no idea how to assign one.

Can someone show me the coorect way to assign a texture and shader to the created variables?

3 Answers

3

The easiest way to assign things is to drag and drop them.

  • Attach your script to an empty game object
  • Select the script in the Hierearchy view
  • In the Inspector view, find the component with your script name and see the variables
  • From your project view, drag your texture onto the variable in the Inspector view…where it says None (Texture).

What does generating the mesh procedurally have to do with dragging and dropping textures and shaders in the inspector? You can dynamically load a texture using Resources.Load() instead as the comment out code does if you need to dynamically load something. But nothing in this code prevents the use of resources dragged and dropped in the Inspector.

A material is a shader and the settings for that shader. You can build a procedural mesh with a standard material by:

  • Create a game object
  • Add a MeshFilter
  • Add a MeshRenderer
  • Create a material
  • Choose the shader for the material
  • Apply the texture to the material
  • Assign the material to the MeshRenderer
  • Build your mesh
  • Assign it to the mesh filter

Repeat the last 2 steps if the mesh is changing

You can change the texture then by doing:

    renderer.material.mainTexture = Resources.Load("someTexture", Texture2D) as Texture2D;

Or you could use a RenderTexture or any other texture that you made up.

If you really want to create the material at runtime

   var m = new Material(Shader.Find("TheNameOfTheShader"));
   m.mainTexture = someTexture2D;
   m.SetTexture("SomeOtherTextureName", someDifferentTexture);
   renderer.material = m;

Ok, here is what I did, I placed this into the code: var m = new Material(Shader.Find("Diffuse")); m.mainTexture = Resources.Load("RockDark", Texture2D) as Texture2D; m.SetTexture("RockDark", m.mainTexture); block.renderer.material = m; Unfortunatly, it gives this error: Shader wants normals, but the mesh doesn't have them Does anyone understand this? However, there is good news. The generated shape is now a gray instead of a strange pink, but unfortunatly, it still isn't showing the texture "RockDark".

You need to assign the mesh's normals just like you did the vertices and uvs.

Ok, in not sure how to use Recalculate normals. I tried doing this: meshFilter.Mesh.RecalculateNormals(); I also tried this mesh.Mesh.RecalculateNormals(); It doesn't work either way ,and I don't know why. Do you guys understand?

Can you work out the normals of your mesh and just allocate them? There's one for each vertex, it's a Vector3 and it goes in an array set into the normals property.

An unlit shader won't need normals, anything lit will.

Ok, I finally solved it! I took your advice whydoidoit and decided to involve the inspecter. I created a brand new material called “TestMaterial” in the inspecter (rather than in the code). Then I used this:

block.renderer.material = Resources.Load("TestMaterial");

No shader problems, and the texture actually loaded! I was able to load the texture procedurally, which is what I needed. Thank you very much.