suite à une question voici un script écrit par le fabuleux James Newton et modifié par Alexx
Ce script permet, à partir d'un modelsUnderLoc ou d'un modelsUnderRay en mode #detailed sur un modèle texturé, de connaître le point2D correspondant dans l'image de la texture
exemple d'utilisation :
Lingo
data = sprite(1).camera.modelsunderloc(the mouseloc, 1, #detailed)
if data.count then
tLoc = mMapPointToTexture(data[1], 512,512) -- en imaginant que la texture qui nous intéresse fasse 512*512
end if
Lingo
-- MAP POINT TO TEXTURE --
--
-- © October 2001, James Newton <newton@planetb.fr>
-- Bidouillage d'Alexx : remplacement de l'objet anImage par ses dimensions tWidth,tHeight
on mMapPointToTexture(iSectList, tWidth,tHeight) ----------------------------
-- RETURNS the point in the Bitmap member used by the texture
-- defined implicitly in <iSectList>
--
-- INPUT:
-- <iSectList> should be a property list of the format:
-- [#model: model("model name"),
-- #distance: <float>,
-- #isectPosition: <vector>,
-- #isectNormal: <vector>,
-- #meshID: <integer>,
-- #faceID: <integer>,
-- #vertices: [<vector>, <vector>, <vector>],
-- #uvCoord: [#u: <float>, #v: <float>]]
-- <anImage> can be VOID, an image object, or any type of object
-- with width and height properties (eg: propList)
--
-- When you use aCamera.modelsUnderLoc(aLoc, #detailed) or
-- aMember.modelsUnderRay(aPoint, aVector, #detailed), the result is
-- a linear list of lists with this format.
---------------------------------------------------------------------
tModel = iSectList.model
tResource = tModel.resource
isMesh = (tResource.type = #mesh)
if not isMesh then
-- We need to use the mesh deform modifier to get texture data
if not((tModel.modifier).getpos(#meshdeform))then
-- Add mesh deform modifier... but remember to remove it later
tModel.addmodifier(#meshdeform)
isModified = true
end if
end if
tFace = iSectList.faceID
tUVCoord = iSectList.uvCoord
-- Determine how the iSect data maps to this particular face
if isMesh then
tCoordList = tResource.textureCoordinateList
tFaceList = tResource.face[tFace].texturecoordinates
else
tCoordList = tModel.meshdeform.mesh[iSectList.meshID].textureCoordinateList
tFaceList = tModel.meshdeform.mesh[iSectList.meshID].face[tFace]
if isModified then
-- Remove the modifier now that it has done its job
tModel.removeModifier(#meshdeform)
end if
end if
-- tCoordList will be a list of lists, each containing two floating-
-- point numbers between 0.0 and 1.0. The first number defines the
-- relative horizontal position of a point in the texture, the
-- second number defines the relative vertical point. The origin
-- point [0.0, 0.0] is in the bottom left hand corner.
--
-- tFaceList will be a list with three integer values [a, b, c]
-- These values determine which entry in tCoordList is used by the
-- chosen face. The first entry <a> defines the origin. The u
-- value increases from <a> to <b>. Any point on the line between
-- <a> and <b> will have a v value of zero. The v value increases
-- from <a> to <c>. Any point on the line between <a> and <b> will
-- have a v value of zero.
-- Calculate the position of the points <a>, <b> and <c> within the
-- Bitmap member.
tLocA = tCoordList[tFaceList[1]] -- [<float>, <float>]
tLocA = point(tLocA[1] * tWidth, (1 - tLocA[2]) * tHeight)
tLocB = tCoordList[tFaceList[2]] -- [<float>, <float>]
tLocB = point(tLocB[1] * tWidth, (1 - tLocB[2]) * tHeight)
tLocC = tCoordList[tFaceList[3]] -- [<float>, <float>]
tLocC = point(tLocC[1] * tWidth, (1 - tLocC[2]) * tHeight)
tUVector = (tLocB - tLocA) * tUVCoord.u -- actually a 2D point...
tVVector = (tLocC - tLocA) * tUVCoord.v -- ... rather than a vector
-- Start from the origin <a>, move first in the u direction then in
-- the v direction to end up at the point in the texture
return tLocA + tUVector + tVVector
end mMapPointToTexture









