⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogreskeletonlib_meshfns.ms

📁 赫赫大名的 OGRE 游戏引擎
💻 MS
📖 第 1 页 / 共 2 页
字号:

----------------------------------------------------------------------------------------
-- ----------------------------------- WRITE MESH ----------------------------------- --
----------------------------------------------------------------------------------------
-- NOTA
-- 3dsmax starts ID from 1 although OGRE starts from 0.
-- -> a vertex 0 and a bone 0 will be added

-- flipNormal, scale, flipYZ, exportColours,exportUV : mesh parameters
-- firstFrmae, lastFrame,length,animName : animation parameters.
struct exportOptions (firstFrame, lastFrame, length, animName, scale, flipyz, flipNormal, exportColours, exportUV) ;



-----------------------------------------------------------------
-- Global 'variables'
-- There is an array with vertices datas for each submesh
-- There is also an array made up of faces.
-----------------------------------------------------------------

verticesArrays=#() ;
facesArrays=#() ;
boneAssignments=#() ;
submeshesId=#() ;
ogreVertIndex=#() ;
materialName = "" ;

-- verticesArrays[i] will describe the submesh number i and its elements will be ogreVertices where
--	ogreVert = #(x,y,z,nx,ny,nz,u,v,r,g,b,a) 
--   		     ----- -------- --- -------
--          	 Pos     Nmal   U,V   RGBA

-- facesArrays[i] consists in arrays like #(v1,v2,v3) which discribe submesh number i.

-- boneAssignments is made up of arrays like #(vertex,bone,weight)

-- In submeshesId, there are material Ids

-- in ogreVertIndex, there are the current index for submeshes

-- materialName is the name that Ogre will use


-----------------------------------------------------------------
-- There is no function to have the number of a bone with the 
-- Physique modifier, so before trying to export, the user
-- properties "number" is added to bones.
-----------------------------------------------------------------

function setBonesNumber phy =
(
	local i ;

	for i=1 to (physiqueOps.GetBoneCount $) do
	(
		b = (physiqueOps.getBones $)[i] ;
		setUserProp b "number" i ;
	)
)


	
-----------------------------------------------------------------
-- exploreMesh returns a string which is "OK" or an error message
-- if there are warnings, it displays message boxes.
-----------------------------------------------------------------

function exploreMesh pmesh exportUV exportColours=
(
	local material, answer, m ;
	
	answer = "" ;
	m = snapShotAsMesh pmesh ;
	
	-- first, is a material defined	?
	material = pmesh.material ;
	if (material == undefined) then
		answer = "This mesh doesn't have any material, please apply one\n" ;
	else
		-- then, are UVW coords set up ?
		if (exportUV and getnumtverts m == 0) then
			answer = "This mesh must have UVW coords in order to be exported" ;
		
	if (answer == "") then 
		answer = "OK" ;
	
	delete m ;
	answer ;
)


----------------------------------------------------------------------------
-- Retrieves al the datas which wil be used
-- flipyz = true if you want coords to fit in OGRE.
-- flipNormal = true if you want to flip normals
-- scale : the scale factor
-- exportUV = true if you want to export texture
-- exportColor = true if you want to export vertices colors
-- sk : the skin modifier (may be undefined)
--
-- stores datas in verticesArrays and facesArrays.
----------------------------------------------------------------------------

function getDatas tmesh flipyz scale flipNormal exportUV exportColours sk phy =
(
    local num_tverts, num_faces,v,f,cf,face,nmal,vert,vertTmp,tvertTmp,faceTmp ;
	local couple,searchTable,ogreface ;
	local i,j,k,vwcount,bid,w,matId
	local rootId ;
	
	searchTable=#() ;
	-- In order to know if a #(vertex,tvertex) has already be found,
	-- just read in the searchtable[vertex] in order to find (or not) an element such as #(tvertex,cvertex,ogreVertNum)
	-- This algorithm was used because I think there is not lots of (tvertex,cvertex) linked to one vertex...
	-- Maybe I'm wrong, but I don't find anything else to perform a quick search and to save memory...
	searchTables=#() ;
	-- It is a searchTable array. (one searchTable for each submesh)
	
	num_tverts = tmesh.numtverts ;
	num_faces = tmesh.numfaces ;

	ogreFace=Point3 12 12 12 ;
	cmpt = 1 ; -- vertex index.

	-- Data arrays init.	
	verticesArrays=#() ;
	facesArrays=#() ;
	boneAssignments = #() ;
	submeshesId = #() ;
	ogreVertIndex=#() ;
	

	-- For each face
	for i = 1 to (getnumfaces tmesh) do -- For each face
	(		
		
		f = getface tmesh i ;
		matId = getFaceMatId tmesh i ;		
		
		-- we retrieve datas. it depends on options.
		-- UV face
		if (exportUV) then
			tvf = gettvface tmesh i ;
		else
			tvf = point3 1 1 1 ;
		
		-- Color Face
		if (exportColours) then
			cf = getVCFace tmesh i ;
		else
			cf = point3 1 1 1 ;
		
		-- For each vertex whose face is made up of.
		for j=1 to 3 do (			
		    a = (int) (f[j]) ;
			b = (int) (tvf[j]) ;
			c = (int) (cf[j]) ;

			-- we try to find the corresponding searchtable.											
			if (searchTables[matId] == undefined ) then
				searchTables[matId]=#() ;	
			
			searchTable = searchTables[matId] ;
			
			-- Maybe this triplet (vertex,tvertex,cvertex) has already been found.
			-- So we use searchTable.
			ogreVertNum = -1 ;
			
		    if (searchTable[a] == undefined ) then
				searchTable[a]=#() ;
			
			tab = searchTable[a] ;
			
			-- Now we can search in the array searchTable[a]
			for triplet in tab do
			(
				if (triplet[1] == b) then
					if (triplet[2] == c) then
						ogreVertNum = triplet[3] ;					
			)

			if (ogreVertNum == -1) then -- That is to say it has not been found. 
			(
				-- the vertex number for the current submesh must be updated
				
				-- if undefined, the array is created
				if (ogreVertIndex[matId] == undefined ) then
					ogreVertIndex[matId]=1 ;
				
				-- vertex number is increased.								
				ogreVertNum = ogreVertIndex[matId] ;
				ogreVertIndex[matId] = ogreVertIndex[matId] + 1 ;
				
				-- it is added to the search table.
				append searchTable[a] #(b,c,ogreVertNum) ;      
				
				-- we retrieve data
				vert = getVert tmesh f[j] ;
				nmal = getNormal tmesh f[j] ;
				
				tvert = point3 0 0 0 ;
				if (exportUV) then
					tvert = getTvert tmesh tvf[j] ;   
				cvert = color 0 0 0 1;
				if (exportColours) then
					cvert = getVertColor tmesh cf[j] ;
				
				-- change scale
				vert = vert * scale ;
		
				-- flip axes		
				if (flipyz) then
				(
					vertTmp = copy vert ;
					vert[2] = vertTmp[3] ;
					vert[3] = -vertTmp[2] ;
					
					nmalTmp = copy nmal ;
					nmal[2] = nmalTmp[3] ;
					nmal[3] = -nmalTmp[2] ;
				)
				
				-- Maybe the material found defines a new submesh...
				if (verticesArrays[matId] == undefined) then
				(
					format "- Submesh detected: material ID = %\n" matId
					verticesArrays[matId] = #() ;
					boneAssignments[matId] = #() ;
					append submeshesId matId ;
					-- A new vertex is created because OGRE starts with 0 although 3dsmax starts with 1
					append verticesArrays[matId] #(vert[1],vert[2],vert[3],nmal[1],nmal[2],nmal[3],tvert[1],tvert[2],cvert.r,cvert.g,cvert.b,cvert.a) ;   -- Vertex is added to datas
				)
				append verticesArrays[matId] #(vert[1],vert[2],vert[3],nmal[1],nmal[2],nmal[3],tvert[1],tvert[2],cvert.r,cvert.g,cvert.b,cvert.a) ;   -- Vertex is added to datas

				
				-- And now, bone assignments. (if a skin modifier is present)
				-- It seems that there are issues when just few vertices have bone assignments.
				-- So there is at least the root assignment.
				if (sk != undefined) then (
					vwcount = skinOps.getVertexWeightCount sk a ; -- a is the vertex number: "a = (int) (f[j]) ;"

					for k=1 to vwcount do
					(
						bid = skinOps.getVertexWeightBoneID sk a k ;
						w = skinOps.getVertexWeight sk a k ;
						
						append boneAssignments[matId] #(ogreVertNum,bid,w) ;
					)
					-- assignment to the root bone.
					if (vwcount==0) then (
						-- gets the root Id:
						rootId=getRootsId sk
						append boneAssignments[matId] #(ogreVertNum,rootId[1],1) ;
					)
				)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -