# How do I place a texture on an irregular 3D model?

I'm trying to reproduce a textured head in Mathematica, and I'm using a kind of 'net' of a head which contains all the features on the face. The texture looks like this:

which is supposed to look something like this:

Here's the code I'm using.

ListSurfacePlot3D[Import["inputImage.obj", "VertexData"],
PlotStyle -> Texture[texture], Mesh -> None, MaxPlotPoints -> 80,
TextureCoordinateFunction -> ({#1, -#2, #3} &)]


which gives me

Another problem is that the texture is reflected about the origin (ie the face is plastered on the back of the head as well). It should instead be black on the back of the head mesh. I suppose this is suppose to do with the head being centered at the origin. I also need to have the texture to 'follow' the head when I change the coordinates of the mesh later on.

I have attempted using

Graphics3D[
{EdgeForm[],
Texture[texture],
GraphicsComplex[mesh,
Polygon[polygon, VertexTextureCoordinates -> mesh]]}]


but it gives me this instead

I need to be able to reproduce this in Mathematica as I have some coordinate transformations which I need to apply, and thereafter, 'wrap' the mesh with the texture. Are these possible?

EDIT: The link to the model is located https://www.dropbox.com/s/3elv96e8mfodzou/fit_9D0F8093-5566-4EE4-88A7-00070C1D6305.zip

EDIT 2: I've tried converting the OBJ to PLY and I did this

Import["inputImage.ply", "UserExtensions"][[2]]
Import["inputImage.ply", "UserExtensions"][[3]]


which gave an output of

"texture_u" -> {..}
"texture_v" -> {..}


where .. was a bunch of numbers which had a Length of 3066 for both. My guess was that these are texture coordinates (where u is horizontal and v is vertical) as per the links provided by Rahul Narain.

I attempted to create a list of coordinates from these two list in the following manner

texturecoord = {Import["inputImage.ply", "UserExtensions"][[2]][[2]],
Import["inputImage.ply", "UserExtensions"][[3]][[2]]} // Transpose


Then, I tried doing

Graphics3D[
{EdgeForm[],
Texture[texture],
GraphicsComplex[mesh,
Polygon[polygon, VertexTextureCoordinates -> mesh]]}]


But that gives me this

-
Could you link to the texture and the face model as well? –  The Toad Oct 16 '13 at 18:14
ListSurfacePlot3D is quite appropriate for Halloween. That face is.... scary. –  István Zachar Oct 16 '13 at 22:09
I've added a link at the bottom. –  user10035 Oct 17 '13 at 1:52
The problem is that the mesh in the OBJ file comes equipped with texture coordinates, but Mathematica throws them away when importing it. Thus it is not possible to correctly map the texture on the imported mesh, unless you write your own OBJ parser that retains the texture coordinates. Fortunately the OBJ file format is quite easy to parse, but someone with more free time than me should do it. –  Rahul Oct 17 '13 at 23:55
@RahulNarain Have you seen this? You suggestion has been implemented. –  Szabolcs Jun 26 '14 at 0:09

Not sure if this helps, but if you play with the texture function coordinates and change the lighting you can get something that looks reasonable:

ListSurfacePlot3D[
Import["inputImage.obj", "VertexData"],
PlotStyle -> Texture[texture], Mesh -> None, MaxPlotPoints -> 80,
TextureCoordinateFunction -> (If[#3 > 0.3, {0.7 #1 + 0.15, -#2 - 0.06}, {0.5, 0.9}] &),
Lighting -> "Neutral"]


Change the 0.3 if you need more or less of the back colored black.

-
I have decided to make a switch to using Graphics3D, because it is computationally more efficient and I do not have to worry about rough edges. VertexTextureCoordinate is to GraphicsComplex as what TextureCoordinateFunction is to ListSurfacePlot3D. The main difference is that VertexTextureCoordinate uses a series of vertices as I understand it, to match the texture coordinate to the surface. Do you have any suggestions as to how I could tweak this code? `Graphics3D[ {EdgeForm[], Texture[texture], GraphicsComplex[mesh, Polygon[polygon, VertexTextureCoordinates -> mesh]]}] –  user10035 Oct 18 '13 at 8:24
I require an exact mapping. Would that be possible? –  user10035 Oct 21 '13 at 10:04
@user10035 See the comment of Rahul above. –  Sjoerd C. de Vries Oct 21 '13 at 10:31
I've converted the format to .PLY and managed to extract the texture coordinates and replotted the mesh as seen in the second edit in my original post. However, it still doesn't seem to work. Any insights on this? –  user10035 Oct 22 '13 at 10:53
@user10035 I would love to dive into this, but unfortunately don't have time to do that at this moment. –  Sjoerd C. de Vries Oct 22 '13 at 11:18