;PickedU,PickedV by Fredborg;PickedTX,PickedTY addon by RemiD;calculates picked U,V with some math wizardry (by Fredborg);once the U,V ( PickedU PickedV ) of the picked point is determined, we can calculate the position X,Y of the picked texel ( PickedTX, PickedTY )Graphics3D(800,600,32,2)SeedRnd(MilliSecs())Global PickedU#, PickedV# ;picked U,VGlobal PickedTX%, PickedTY% ;picked texel X,YLocal Cam = CreateCamera()CameraRange(Cam,0.1,100)CameraClsColor(Cam,025,025,025)Local Texture = CreateTexture(64,64,1)SetBuffer(TextureBuffer(Texture))ClsColor(250,250,250) : Cls();outline of the textureColor(125,125,125) : Line(0,0,64-1,0)Color(125,125,125) : Line(0,64-1,64-1,64-1)Color(125,125,125) : Line(0,0,0,64-1)Color(125,125,125) : Line(64-1,0,64-1,64-1);corner top left (red texel)Color(255,000,000) : Plot(0,0);corner top right (green texel)Color(000,255,000) : Plot(64-1,0);corner bottom left (blue texel)Color(000,000,255) : Plot(0,64-1);corner bottom right (white texel)Color(255,255,255) : Plot(64-1,64-1);some textColor(025,025,025) : TStr$ = "Fredborg" : TX% = TextureWidth(Texture)/2-StringWidth(TStr)/2 : TY% = TextureHeight(Texture)/2-StringHeight(TStr)/2 : Text(TX,TY,TStr)Local Mesh = CreateCube()EntityTexture(Mesh,Texture)RotateEntity(Mesh,0,Rand(-180,180),0,True)EntityPickMode(Mesh,2)PositionEntity(Cam,0,1.65,-3.0)RotateEntity(Cam,22.5,0,0)While Not KeyDown(1) If( KeyDown(200)=1 ) TurnEntity(Mesh,+1,0,0) Else If( KeyDown(208)=1 ) TurnEntity(Mesh,-1,0,0) EndIf If( KeyDown(203)=1 ) TurnEntity(Mesh,0,-1,0) Else If( KeyDown(205)=1 ) TurnEntity(Mesh,0,+1,0) EndIf If( MouseDown(1)=1 ) CameraPick(Cam, MouseX(), MouseY()) If( PickedEntity() <> 0 And PickedSurface() <> 0 ) CalculatePickedUVvFredborg() ;by Fredborg PickedTX% = TextureWidth(Texture)*PickedU : PickedTY% = TextureHeight(Texture)*PickedV DebugLog(PickedU+","+PickedV+"->"+PickedTX+","+PickedTY) SetBuffer(TextureBuffer(Texture)) Color(255,000,255) : Plot(PickedTX,PickedTY) EndIf EndIf SetBuffer(BackBuffer()) RenderWorld() Color(255,255,255) Text(0,0,"Mouse Left to pick / color texels on the texture") Text(0,16,"Arrows to turn the shape up, down, left, right") Flip(True)WendEnd()Function CalculatePickedUVvFredborg() If( PickedSurface() ) picent = PickedEntity() picsurf = PickedSurface() pictri = PickedTriangle() picpx# = PickedX() picpy# = PickedY() picpz# = PickedZ() Local picvx#[3], picvy#[3], picvz#[3] ;Local picvnx#[3], picvny#[3], picvnz#[3] Local picvu#[3], picvv#[3] ; picvw#[3] Local picpu#[1], picpv#[1] ; picpw#[1] For i = 0 To 2 TFormPoint(VertexX(picsurf,TriangleVertex(picsurf,pictri,i)),VertexY(picsurf,TriangleVertex(picsurf,pictri,i)),VertexZ(picsurf,TriangleVertex(picsurf,pictri,i)),picent,0) picvx[i] = TFormedX() picvy[i] = TFormedY() picvz[i] = TFormedZ() ;picvnx[i] = VertexNX(picsurf,TriangleVertex(picsurf,pictri,i)) ;picvny[i] = VertexNY(picsurf,TriangleVertex(picsurf,pictri,i)) ;picvnz[i] = VertexNZ(picsurf,TriangleVertex(picsurf,pictri,i)) picvu[i+0] = VertexU(picsurf,TriangleVertex(picsurf,pictri,i),0) picvv[i+0] = VertexV(picsurf,TriangleVertex(picsurf,pictri,i),0) ;picvw[i+0] = VertexW(picsurf,TriangleVertex(picsurf,pictri,i),0) Next ; Select which component of xyz coordinates to ignore Local coords = 3 If Abs(PickedNX()) > Abs(PickedNY()) If Abs(PickedNX())>Abs(PickedNZ()) Then coords = 1 Else If Abs(PickedNY())>Abs(PickedNZ()) Then coords = 2 EndIf Local a0#,a1#,b0#,b1#,c0#,c1# ; xy components If (coords = 3) ; edge 0 a0# = picvx[1] - picvx[0] a1# = picvy[1] - picvy[0] ; edge 1 b0# = picvx[2] - picvx[0] b1# = picvy[2] - picvy[0] ; picked offset from triangle vertex 0 c0# = PickedX() - picvx[0] c1# = PickedY() - picvy[0] Else ; xz components If (coords = 2) ; edge 0 a0# = picvx[1] - picvx[0] a1# = picvz[1] - picvz[0] ; edge 1 b0# = picvx[2] - picvx[0] b1# = picvz[2] - picvz[0] ; picked offset from triangle vertex 0 c0# = PickedX() - picvx[0] c1# = PickedZ() - picvz[0] Else ; yz components ; edge 0 a0# = picvy[1] - picvy[0] a1# = picvz[1] - picvz[0] ; edge 1 b0# = picvy[2] - picvy[0] b1# = picvz[2] - picvz[0] ; picked offset from triangle vertex 0 c0# = PickedY() - picvy[0] c1# = PickedZ() - picvz[0] End If End If ; ; u and v are offsets from vertex 0 along edge 0 and edge 1 ; using these it is possible to calculate the Texture UVW coordinates ; of the picked XYZ location ; ; a0*u + b0*v = c0 ; a1*u + b1*v = c1 ; ; solve equation (standard equation with 2 unknown quantities) ; check a math book to see why the following is true ; Local u# = (c0*b1 - b0*c1) / (a0*b1 - b0*a1) Local v# = (a0*c1 - c0*a1) / (a0*b1 - b0*a1) ; If either u or v is out of range then the ; picked entity was not a mesh, and therefore ; the uvw coordinates cannot be calculated If ( u < 0.0 Or u > 1.0 ) Or ( v < 0.0 Or v > 1.0 ) Return End If ; Calculate picked uvw's for coordset 0 (and modulate them to be in the range of 0-1 nescessary) picpu[0] = (picvu[0] + ((picvu[1] - picvu[0]) * u) + ((picvu[2] - picvu[0]) * v)) Mod 1 picpv[0] = (picvv[0] + ((picvv[1] - picvv[0]) * u) + ((picvv[2] - picvv[0]) * v)) Mod 1 ;picpw[0] = (picvw[0] + ((picvw[1] - picvw[0]) * u) + ((picvw[2] - picvw[0]) * v)) Mod 1 ; If any of the coords are negative If( picpu[0] < 0.0 ) Then picpu[0] = 1.0 + picpu[0] If( picpv[0] < 0.0 ) Then picpv[0] = 1.0 + picpv[0] ;If( picpw[0] < 0.0 ) Then picpw[0] = 1.0 + picpw[0] PickedU = picpu[0] : PickedV = picpv[0] End IfEnd Function