perspective texture mapping

I need to project on a rectangle from a projector and my problem is exactly this (Mika’s prob). For which I need to change a rectangle to a quad. I have succeeded in doing so by building the mesh everytime it’s corners change (The code is seen below). I need a quad based on the four corners (TL, TR, BL and BR) but my current method is based on bilinear interpolation and I am getting this (the right side one)

35379-pvmre.png

But I need the one like the the left side (but it’s a quad). I want something exactly like the ones mentioned in the sulutions of Mika’s prob [35380-changecamera.txt|35380])…

I have the tried the same input as the ones in those solutions to check errors in the code…I get the same projection matrix…

 // Create a quad mesh
     public Mesh CreateMesh() {
      
            mesh = new Mesh();
    		
    		int segments=15;
    		int numofVertices=segments+1;
    		float uvFactor = 1.0f/segments;
    		 Vector3[] vertices = new Vector3[numofVertices*numofVertices];
              Vector2[] uvs = new Vector2[numofVertices*numofVertices];
    		int numTriangles = segments*segments * 6;
    		int[] triangles = new int[numTriangles];
    		int index = 0;
         /*  vertices = new Vector3[]
            {
                new Vector3( tr[0], tr[1],  0),
                new Vector3( br[0], -br[1], 0),
                new Vector3(-tl[0], tl[1], 0),
                new Vector3(-bl[0], -bl[1], 0),
                        };*/
    		//y=y0+(y1-y0)*((x-x0)/(x1-x0))
    		float lDist=Vector2.Distance(new Vector2(tl[0],tl[1]),new Vector2(bl[0],bl[1]));
    		float rDist=Vector2.Distance(new Vector2(tr[0],tr[1]),new Vector2(br[0],br[1]));
    		float tDist=Vector2.Distance(new Vector2(tl[0],tl[1]),new Vector2(tr[0],tr[1]));
    		float bDist=Vector2.Distance(new Vector2(bl[0],bl[1]),new Vector2(br[0],br[1]));
    		for (float y = 0.0f; y < numofVertices; y++)
    
                {	
    			float xl=-((bl[0]-tl[0])/segments*y+tl[0]);
    			float xr=((br[0]-tr[0])/segments*y+tr[0]);
    				//xl=tl[0]+(bl[0]-tl[0])*y-0/segments-0
                    for (float x = 0.0f; x < numofVertices; x++)
    
                    {
    					float yb=-((br[1]-bl[1])/segments*x+bl[1]);
    					float yt=((tr[1]-tl[1])/segments*x+tl[1]);
    					float xcod=((xr-xl)/segments)*x+xl;
    					float ycod=((yb-yt)/segments)*y+yt;
    					//Debug.Log ("Y: "+y+" :"+yuse+" lengths :"+y0+" "+y1+" X: "+x+" :"+xuse+" lengths :"+x0+" "+x1);
                        //vertices[index] = new Vector3(x*(16f/segments) - (tl[0]+tr[0])/2f , y*(10f/segments) - (tl[1]+bl[1])/2f, 0.0f );
    					//vertices[index] = new Vector3(x*(16f/segments) - (xuse)/2f , y*(10f/segments) - (yuse)/2f, 0.0f );
    					//vertices[index] = new Vector3((-bl[0]+ (yuse/segments)*x), (-bl[1]+ (yuse/segments)*y), 0.0f );
    					vertices[index] = new Vector3(xcod, ycod, 0.0f );
                        uvs[index++] = new Vector2(x*uvFactor, y*uvFactor);
    					//Debug.Log(" xl :"+xl+" xr :"+xr+" yb :"+yb+" yt :"+yt);
    
                    }
    
                }
    		//vertices[0]=new Vector3(99,99,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,
    			1,3,2,
            };*/
    		index = 0;
    		for (int y = 0; y < segments; y++)
    
                {
    
                    for (int x = 0; x < segments; x++)
    
                    {
    
                        triangles[index]   = (y     *numofVertices) + x;
    
                        triangles[index+1] = ((y+1) * numofVertices) + x;
    
                        triangles[index+2] = (y     * numofVertices) + x + 1;
    
            
    
                        triangles[index+3] = ((y+1) * numofVertices) + x;
    
                        triangles[index+4] = ((y+1) * numofVertices) + x + 1;
    
                        triangles[index+5] = (y     * numofVertices) + x + 1;
    
                        index += 6;
    
                    }
    
                }
    
            mesh.vertices = vertices;
            mesh.uv = uvs;
            mesh.triangles = triangles;
            mesh.RecalculateNormals();
    
            return mesh;
        }

I have removed the ‘-’ from xl and yb and it seems to work fine

91094-sin-titulo.png

    public static Mesh CreateMesh(Vector2 tl, Vector2 bl, Vector2 tr, Vector2 br)
    {
        var mesh = new Mesh();
        var segments = 16;
        var numofVertices = segments + 1;
        var uvFactor = 1.0f / segments;
        var vertices = new Vector3[numofVertices * numofVertices];
        var uvs = new Vector2[numofVertices * numofVertices];
        var numTriangles = segments * segments * 6;
        var triangles = new int[numTriangles];
        var index = 0;

        for (float y = 0.0f; y < numofVertices; y++)
        {
            var xl = ((bl[0] - tl[0]) / segments * y + tl[0]);
            var xr = ((br[0] - tr[0]) / segments * y + tr[0]);

            for (float x = 0.0f; x < numofVertices; x++)
            {
                var yb = (br[1] - bl[1]) / segments * x + bl[1];
                var yt = ((tr[1] - tl[1]) / segments * x + tl[1]);
                var xcod = ((xr - xl) / segments) * x + xl;
                var ycod = ((yb - yt) / segments) * y + yt;

                vertices[index] = new Vector3(xcod, ycod, 0.0f);
                uvs[index++] = new Vector2(x * uvFactor, y * uvFactor);
            }
        }

        index = 0;
        for (int y = 0; y < segments; y++)
        {
            for (int x = 0; x < segments; x++)
            {
                triangles[index] = (y * numofVertices) + x;
                triangles[index + 1] = ((y + 1) * numofVertices) + x;
                triangles[index + 2] = (y * numofVertices) + x + 1;

                triangles[index + 3] = ((y + 1) * numofVertices) + x;
                triangles[index + 4] = ((y + 1) * numofVertices) + x + 1;
                triangles[index + 5] = (y * numofVertices) + x + 1;

                index += 6;
            }
        }

        mesh.vertices = vertices;
        mesh.uv = uvs;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        return mesh;
    }