mesh and renderer texture

Hi,

I have create a basic square mseh via C# code like:

public Mesh CreatePlaneMesh() 
	{
		Mesh mesh = new Mesh();
		Vector3[] vertices = new Vector3[]
		{
			new Vector3( 1, 1, 0),
			new Vector3( 1, -1, 0),
			new Vector3(-1, 1, 0),
			new Vector3(-1, -1, 0),
		};
		Vector2[] uv = new Vector2[]
		{
			new Vector2(1, 1),
			new Vector2(1, 0),
			new Vector2(0, 1),
			new Vector2(0, 0),
		};
		int[] triangles = new int[]
		{
			0, 1, 2,
			2, 1, 3,
		};
		mesh.vertices = vertices;
		mesh.uv = uv;
		mesh.triangles = triangles;
		mesh.RecalculateNormals();
		return mesh;
	}

And a second mesh from an obj file.

When I display a texture2D on each of them everything work fine.

But when I assign a render texture. the render texture is display correctly on the square not on my own mesh. I can see only the last lign of my render texture on my mesh.

		// create render texture
		RenderTexture _RenderTexture = new RenderTexture(512, 512, 24);
		_RenderTexture.Create();
		_RenderTexture.name = "Camera render texture";
		// render texture to Main camera
		var original = GameObject.FindWithTag("MainCamera");
		original.camera.targetTexture = _RenderTexture;
		original.camera.Render();
		// Render texture to plane texture
		Material mat = new Material(Shader.Find("Diffuse"));
		mat.name = "Camera Texture";
		mat.SetTexture("_MainTex", _RenderTexture);
		var plane = GameObject.Find("mesh");
		plane.renderer.material = mat;

I do not understand why is wrong in my code because, the mmesh is working fine

I think the bigger question is, why are you creating a quad via code? Unity has a built in Quad object.

I just use a quad for this example. I want to load at runtime a mesh for obj file and place the render texture on it.

I try first with a quad and after with my own mesh. Both meshs are create by the same function and same method. With texture2D it is working fine but not with the render texture.

I need to create all my meshes by code, because, I need to encapsulate all my code into a plugin to share with somes partners developpers to create my game.

Not sure, will test a few things and come back

Edit: you need to set the active rendertexture.

RenderTexture current = RenderTexture.active;

RenderTexture.active = _RenderTexture;

camera.Render();

RenderTexture.active = current;

The Texture2D fit very well both meshes (obj file or quad).

On the obj mesh I have only the last line of the render Texture

Did you make sure to use the same wrap mode for both the Texture2D and the RenderTexture?

Yes, I have check that. But this not solve my problem.

Did you check the render texture? You can assign it to a variable in the inspector. If you double click it there, it will become visible and you’ll see whether it is correct.

Yes, I have check it and it is correct.

I do not understand why a texture2D should fit well a mesh and a render texture not for the same mesh.

I add my create mesh function from obj file:

	// Create a quad mesh
	public Mesh CreateMesh() 
	{
		Mesh mesh = new Mesh();
		
		ArrayList verticesList = new ArrayList();
		ArrayList uvList = new ArrayList();
		ArrayList trianglesList = new ArrayList();
		StreamReader objFile = new StreamReader("mymesh.obj");
		//replace double spaces and dot-notations
		string s = objFile.ReadToEnd();
		s = s.Replace("  ", " ");
		//s = s.Replace(",", ",");
		string[] lines = s.Split("\n"[0]);
		
		uvList.Clear();
		verticesList.Clear();
		trianglesList.Clear();
		
		for(int i=0; i<lines.Length-1;++i)
		{
			string buffer = lines[i];
			if(buffer[0]=='v')
			{
				if(buffer[1]=='t')
				{
					// UV point
					string[] elements = buffer.Split(" "[0]);
					float _x = System.Convert.ToSingle(elements[1]);
					float _y = System.Convert.ToSingle(elements[2]);
					uvList.Add(new Vector2(_x,-_y));
				}
				else
				{
					// vertices point
					string[] elements = buffer.Split(" "[0]);
					float _x = System.Convert.ToSingle(elements[1]);
					float _y = System.Convert.ToSingle(elements[2]);
					float _z = System.Convert.ToSingle(elements[3]);
					verticesList.Add(new Vector3(_x, -_y, _z));
				}
			}
			else
			{
				if(buffer[0]=='f')
				{
					//faces
					string[] elements = buffer.Split(" "[0]);
					if(elements.Length==5)
					{
						string[] strBrk1 = elements[1].Split("/"[0]);
						string[] strBrk2 = elements[2].Split("/"[0]);
						string[] strBrk3 = elements[3].Split("/"[0]);
						string[] strBrk4 = elements[4].Split("/"[0]);
						trianglesList.Add(System.Convert.ToInt32(strBrk1[1])-1);
						trianglesList.Add(System.Convert.ToInt32(strBrk2[1])-1); 
						trianglesList.Add(System.Convert.ToInt32(strBrk3[1])-1); 
						trianglesList.Add(System.Convert.ToInt32(strBrk1[1])-1); 
						trianglesList.Add(System.Convert.ToInt32(strBrk3[1])-1); 
						trianglesList.Add(System.Convert.ToInt32(strBrk4[1])-1); 
					}					
				}
			}
		}
		
		Vector3[] vertices = new Vector3[verticesList.Count];
		verticesList.CopyTo(vertices);
		Vector2[] uv = new Vector2[uvList.Count];
		uvList.CopyTo(uv);
		int[] trianglestab = new int[trianglesList.Count];
		trianglesList.CopyTo(trianglestab);
		mesh.vertices = vertices;
		mesh.uv = uv;
		mesh.triangles = trianglestab;
		mesh.RecalculateNormals();
		return mesh;
	}

Could you show the code where you create and assign the material and assign the texture to the material?

Edit: There must be a difference between assigning a Texture2D and the RenderTexture and there is most likely the issue.

Here is my code:

RenderTexture _RenderTexture = new RenderTexture(512, 512, 24);
		_RenderTexture.Create();
		_RenderTexture.name = "Camera render texture";
		// render texture to Main camera
		var original = GameObject.FindWithTag("MainCamera");
		original.camera.targetTexture = _RenderTexture;
		RenderTexture current = RenderTexture.active;
		RenderTexture.active = _RenderTexture;
		original.camera.Render();
		// Render texture to plane texture
		Material mat = new Material(Shader.Find("Diffuse"));
		mat.name = "Camera Texture";
		mat.SetTexture("_MainTex", _RenderTexture);
		var plane = GameObject.Find("mymesh");
		plane.renderer.material.mainTexture.wrapMode = TextureWrapMode.Clamp;
		plane.renderer.material = mat;

Line 15 should be

_RenderTexture.wrapMode = TextureWrapMode.Clamp;

Are you using the same tiling and offset as in the original texture?
Did you try to set another texture at line 13, just to try out whether the general setup is correct?

I do your correction.

I use the same tiling and offset. The standard one. I want display the full texture.

I see something strange with:

mat.SetTexture("_MainTex", _RenderTexture);

I have different texture in place of first parameters and obtain differnt result.

Sorry, no idea what you mean. Could you share a screenshot?

I attached here my current situation.

The render texture is well filled into the editor.

Sorry, I don’t understand what those pictures mean. Does the bottom one shot the rendered texture of the top?

No, the two left images are with Texture2D. The two right one are with render texture.

In the scene capetured by the first camera and display on the reneder texture I have only a cube.

The upper images are with the square mesh, the bottom one are shot with the obj mesh.

My problem is why with the renderTexture my obj mesh display only the last line of the renderTexture.

How did you apply the Texture2D to the object? Did you apply it in the same way as the RenderTexture? If not, try to add it exactly like that.

My code with render texture:

RenderTexture _RenderTexture = new RenderTexture(512, 512, 24);
		_RenderTexture.Create();
		_RenderTexture.name = "Camera render texture";
		// render texture to Main camera
		var original = GameObject.FindWithTag("MainCamera");
		original.camera.targetTexture = _RenderTexture;
		RenderTexture current = RenderTexture.active;
		RenderTexture.active = _RenderTexture;
		original.camera.Render();
		// Render texture to plane texture
		Material mat = new Material(Shader.Find("Diffuse"));
		mat.name = "Camera Texture";
		mat.SetTexture("_MainTex", _RenderTexture);
		var plane = GameObject.Find("HelloWorld");
		_RenderTexture.wrapMode = TextureWrapMode.Clamp;
		plane.renderer.material = mat;
		RenderTexture.active = _RenderTexture;
		original.camera.Render();
		RenderTexture.active = current;

My code with Texture2D:

		var plane = GameObject.Find("HelloWorld");
		Texture2D tex = new Texture2D(4, 4);
		byte[] textureFile = System.IO.File.ReadAllBytes("C:/Users/Public/Pictures/Sample Pictures/tulips.png");
        tex.LoadImage(textureFile);
		plane.renderer.material.mainTexture = tex;

If you want further help, post the full code and clean it up. You are showing something different each time you post code!