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

📄 macro_baketextures.mcr

📁 3dmax导出3d模型二次开发插件
💻 MCR
📖 第 1 页 / 共 5 页
字号:
		myElement = _obj.GetBakeElement( i )
		if myElement.enabled == false then
		(
			-- format "skipping disabled element: %\n" (myElement as string)
			continue
		)
			
		searchName = _className as name
		-- format "search element: % == % is % \n" (classof myElement as string)(_className)((classof myElement as string)==_className)
		eleName = (classof myElement as string) as name
		if eleName == searchName then
			return ( i )
	)
	return 0
)

fn validateDirectory _dirName  =
(
	-- create the dir if we don't have one
	local dirList =	#()
	dirList = getDirectories _dirName 
	--format "directories = %\n" (dirList as string)
	if dirList.count == 0 then
	(
		--format "make directory = %\n" _dirName
		-- make a new dir
		makeDir( _dirName )
	)
	
	return true
)	

-- utility to check the input filename, if not valid, create one from the element & path
fn validateFileName _element _fileName =
(
	if( _fileName == undefined ) or ( _fileName == "" ) then
	(
		 return ((commonBakeProps.GetFilePath()) + _element.fileName)
	)
	return _fileName
)

-- utility to make a formated number 0000 style
function formatNumber _num =
(
	s = _num as string
	-- format "s = %, s.count = %, " s (s.count)
	-- pad out w/ 0's
	for i = s.count to 3 do
		s = "0" + s
	-- format " final s = %\n" s
	return s
)


------------------------------------------------------------------
--
--	Function to create/update bitmaps on the elements of the object
--
function updateBitmapFilenames	_obj _nFrame =
(
	--format "    update filenames on elements\n"
	path = commonBakeProps.GetFilePath()
	
	-- make the file number
	curFrameString = formatNumber( _nFrame )
		
	for i = 1 to _obj.NumBakeElements() do
	(
		-- get element & filename
		element = _obj.GetBakeElement( i )
		fname = element.filename
		fname = selectedObjectProps.makeFilename (_obj)(element)(fname)

		-- add the path & save it all in fileType
		if (fname[1] == ".") or (fname[1] == "\\" ) or (fname[1] == "/") then
			element.fileType = fname -- has a path
		else
			element.fileType = path + fname
			
	) -- end, for each element

) -- end, update bitmap filenames


------------------------------------------------------------------
--
--	Functions to map bakechannels to material maps of any type
--	based on settings from an .ini file
--

-- look for name in the inList, return index or 0 if not found
function findName _name _inList =
(
	for n = 1 to _inList.count do
	(
		if (_name as name) == (_inList[ n ] as name) then
			return n
	)
	return 0
)

-- read a file section & set up the bakemap to stdmtl channel mapping	
function readBakeMtlMappingsSection	_sectionName =
(
	-- first get the shader
	s = getINISetting( bakeMtlMappingsIniFile)(_sectionName)("shaderName")
	useShader = if s == undefined or s == "" then "blinn" else s
	
	-- for each possible stdmtl texture
	for n = 1 to stdTextures.count do
	(
		-- see if its assigned in this section
		s = getINISetting( bakeMtlMappingsIniFile)(_sectionName)(stdTextures[n]) 
		bakeTextures[ n ] = if( s == undefined ) then "" else s
		
		--if bakeTextures[ n ] != "" then
		--	format "assign map: % <-- % \n" stdTextures[n] bakeTextures[ n ]
	)
)
				
-- this functions gets the list of available mappings
function readBakeMtlMappings =
(
	-- get the sections of the ini file, the distinct mappings
	-- these are listed in the file as DeclareFileMapping1, DeclareFileMapping2, .. since we cant discover 
	-- section names
	fileMappings = #()
	for i = 1 to numMaxFileMappings do
	(
		key = "DeclareFileMapping" + (i as string)
		s = getINISetting( bakeMtlMappingsIniFile )("DeclareFileMappings")( key )
		if s == undefined or s == "" then continue
		-- format "add mapping: % \n" s
		append (fileMappings) (s)
	)
	-- format "file mappings = % \n" (fileMappings as string)
)		

-- this sets up the given mapping from elements to map channels
function selectBakeMtlMapping _mappingName =
(
	-- first check for valid mapping name
	if findName(_mappingName)(fileMappings) == 0 then
		return 0
		
	-- this creates the mapping between stdMtl channels & bakeElements 	
	readBakeMtlMappingsSection( _mappingName )
)

-- this function clear all the textures on a material
fn clearTextures _toMtl =
(
	local mapName = ""
	theShader = _toMtl.shaderByName
	
	format "clear textures, shader = %\n" theShader
	
	if theShader == "Anisotropic" then
	(
		for i = 1 to anisoTextures.count do
		(
			mapName = anisoTextures[ i ]
			SetProperty( _toMtl )( mapName as name )( noTexture() )
		)
	)
	else if theShader == "Multi-Layer" then
	(
		for i = 1 to stdTextures.count do
		(
			mapName = stdTextures[ i ]
			SetProperty( _toMtl )( mapName as name )( noTexture() )
		)
	)
	else if theShader == "Strauss" then
	(
		for i = 1 to straussTextures.count do
		(
			mapName = straussTextures[ i ]
			SetProperty( _toMtl )( mapName as name )( noTexture() )
		)
	)
	else if theShader == "Oren-Nayar-Blinn" then
	(
		--format "clear onb textures\n"
		for i = 1 to onbTextures.count do
		(
			mapName = onbTextures[ i ]
			format "clear map = % \n" mapName
			SetProperty( _toMtl )( mapName as name )( noTexture() )
		)
	)

	else
	(
		-- just do the standard phong/blinn/metal maps
		-- format "clear p,b,m textures\n"
		for i = 1 to pbmTextures.count do
		(
			mapName = pbmTextures[ i ]
			SetProperty( _toMtl )( mapName as name )( noTexture() )
		)
	)
	format "end clearTextures\n"
)

-- this applies maps, via the current element-to-mapChannel mapping, to the given material,
-- assigning the available elements to the material channels given by the file mapping
function applyIniFileMtlMapping _obj _toMtl =
(	
	format "apply ini mapping\n"
	-- first set the shader
	_toMtl.shaderByName = useShader
	_toMtl.adLock = false
	_toMtl.adTextureLock = false
	
	clearTextures( _toMtl )
	
	-- for each possible bake element
	for nEle = 1 to _obj.NumBakeElements() do
	(
		theElement = _obj.GetBakeElement( nEle )
		
		if theElement.enabled == false then
			continue -- skip disabled elements
		
		-- look for the element name in the map
		n = findName( classof theElement as string )( bakeTextures )
		if n == 0 then 
		(
			format "no match on % \n" ( classof theElement as string )
			continue	-- no match
		)
		
		-- got one, get the file name 
		fname = theElement.fileType
		if fname == undefined or fname == "" then
		(
			path = commonBakeProps.GetFilePath()
			fname = path + theElement.fileName
		)
		
		-- & get the channel name from stdTextures...
		mapName = stdTextures[ n ]
		format "   update map  % w/ texture file: % \n" mapName fname
		
		-- NB: assign indirectly to the just found channel....does this work?
		theMap = bitmapTexture filename:fname
		theMap.coords.mapChannel = _obj.bakeChannel 
		SetProperty( _toMtl )( mapName as name )( theMap )
		
	) -- end, for each element
	format "end apply maps\n"
) -- end, apply maps function


------------------------------------------------------------------
--
--	Function looks for complete, blend map, diffuse or the first map
--	 and applies the found map to the materials diffuse map channel.
--
function applyDefaultFileMapping _obj  _bakeMtl =
(
	-- get diffuse or complete bake element
	nEle = findByClass (_obj)("CompleteMap")
	if nEle <= 0 then
	(	-- not found, look for blend
		nEle = findByClass (_obj)("BlendMap")
	)
	if nEle <= 0 then
	(	-- not found, look for blend
		nEle = findByClass (_obj)("DiffuseMap")
	)

	if nEle <= 0 then
	(	-- nothing found, just get the first enabled element, whatever it is
		for n = 1 to _obj.NumBakeElements() do
		( 
			local elem = _obj.GetBakeElement( n )
			if elem.enabled then
			(
				nEle = n
				exit	-- break the loop
			)
		)
	)

	-- assign to material
	element = _obj.GetBakeElement( nEle )
	--format "        update map using element: % \n" (element as string)
	
	fname = validateFileName( element )( element.fileType )
	--format "        update diffuse: % \n" fname
	_bakeMtl.diffuseMap = bitmaptexture filename:fname
	_bakeMtl.selfIllumAmount = 100
	_bakeMtl.diffuseMap.coords.mapChannel = _obj.bakeChannel 
	
	-- now do the optional maps
--	nEle = findByClass (_obj)("SpecularMap")
--	if nEle > 0 then
--	(	-- found, apply to specular level
--		element = _obj.GetBakeElement( nEle )
--		fname = validateFileName( element )( element.fileType )
--		format "        update specular: % \n" fname
--		_bakeMtl.specularLevelMap = bitmaptexture filename:fname
--	)
--	
--	nEle = findByClass (_obj)("AlphaMap")
--	if nEle > 0 then
--	(	-- found, apply to opacity
--		element = _obj.GetBakeElement( nEle )
--		fname = validateFileName( element )( element.fileType )
--		format "        update opacity: % \n" fname
--		_bakeMtl.opacityMap = bitmaptexture filename:fname
--	)
	
	-- cd also apply the lighting map to the diffuseLevelMap & specularLevel too if 
	-- not used by the specular map above.

)


-----------------------------------------------------------------
--
--	utility fns for handling hw shaders
--

-- get the viewport manager for the material
fn getVPManager _mtl =
(
	manager = DXShaderManager.getViewportManager( _mtl )
	if( manager == undefined ) then
		manager = DXShaderManager.addViewportManager( _mtl )
	return manager
)

-- activate/deactivate the shader
fn activateHWShader	_mtl _isOn	=
(
	manager = getVPManager( _mtl )
		
	-- d'activate the hardware material, anything else to do ????
	if manager != undefined then
		manager.activateEffect( _mtl )( _isOn )
)


-- look for the requested shader in the effect list, set it to the manager if found
fn setHWShader	_manager	_shaderName =
(
	nShaders = _manager.getNumViewportEffects()
	effect = undefined
	-- Search for the effect based on name	
	for i = 1 to nShaders do
	(
		-- match shader by name
		if( _manager.getViewportEffectName( i ) == _shaderName ) then
		(
			--format "set viewport shader =  %  @ index % \n" _shaderName i
			effect = _manager.setViewportEffect( i )
			exit	-- break the loop
		)
	)
	--format "effect = % \n" (effect as string)
	
	return effect
)

-- get the list of hwShaders into the hwShaders array
fn getHWShaderList =
(
	i = 1
	while( i < 16 and (superClassOf meditMaterials[i] as string) != "material" ) do
		i += 1
	if i == 16 then return -1
	mgr = getVPManager( meditMaterials[i] )
	if mgr == undefined then return -1
	nShaders = mgr.getNumViewportEffects() 
	hwShaders = #()
	for i = 1 to nShaders do
		append( hwShaders )( mgr.getViewportEffectName( i ) )
)


------------------------------------------------------------------
--
--	Function to set up the light map dx shader
--
function SetupLightMapShader	_obj	_bakeMtl =
(
	-- look for light & diffuse maps
	nEleLt = findByClass (_obj)("LightingMap")
	nEleDiff = findByClass (_obj)("DiffuseMap")
	--format "lightMap index = %, diffMap index = %\n" nEleLt nEle
	if nEleDiff > 0 and nEleLt > 0 then
	(
		-- install the dx shader for light maps on the bake material
		manager = getVPManager( _bakeMtl )

		-- find the shader & make it active
		effect = setHWShader( manager )( lightMapHWShaderName  )
		-- check it
--		if (effect == undefined) or (effect == null) then
		if (effect == undefined) then
		(
			format "effect undefined\n"
			return false
		)

		-- diffuse map first		
		effect.diffuse_filename = _obj.material.originalMaterial.diffuseMap.filename  
		
		--element = _obj.GetBakeElement( nEleDiff )
		--effect.diffuse_filename = validateFileName( element )( element.fileType )
		--format "set diffuse filename = % \n" effect.diffuse_filename

		-- then the light map
		element = _obj.GetBakeElement( nEleLt )
		effect.lightmap_filename = validateFileName( element )( element.fileType )
		--format "set lightMap filename = % \n" effect.lightmap_filename

		-- set the mapping channel to use

⌨️ 快捷键说明

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