Using texturetool to Compress Textures - 使用紋理工具壓縮紋理
iOS SDK包括一個將紋理壓縮為PVRTC或ASTC格式的工具凤粗,恰當(dāng)?shù)孛麨閠exturetool。 如果您安裝了iOS 7.0 SDK或更高版本的Xcode槐瑞,那么texturetool位于:/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/texturetool犹芹。
紋理工具提供了各種壓縮選項崎页,在圖像質(zhì)量和尺寸之間進(jìn)行權(quán)衡。 您需要對每個紋理進(jìn)行實驗腰埂,以確定哪個設(shè)置提供最佳折中飒焦。
注意:紋理工具可用的編碼器,格式和選項可能會更改屿笼。 本文檔介紹了從iOS 7開始可用的選項牺荠。
texturetool Parameters - texturetool參數(shù)
可以傳遞給紋理工具的參數(shù)在本節(jié)的其余部分進(jìn)行了描述。
user$ texturetool -h Usage: texturetool [-hl]? ? ? texturetool -ctexturetool [-ms] [-e] [-p] -o[-f]first form:? ? -h? ? ? Display this help menu.? ? -l? ? ? List available encoders, individual encoder options, and file formats. second form:? ? -c? ? ? Comparetoand report differences. third form:? ? -m? ? ? ? ? ? ? ? Generate a complete mipmap chain from the input image.? ? -s? ? ? ? ? ? ? ? Report numerical differences betweenand the encoded image.? ? -eEncode texture levels with.? ? -pOutput a PNG preview of the encoded output to. Requires -e option? ? -oWrite processed image to.? ? -fSet fileforimage.
注意:-p選項表示需要-e選項驴一。 它還需要-o選項休雌。
// Listing C-1Encoding optionsuser$ texturetool -lEncoders: PVRTC --channel-weighting-linear --channel-weighting-perceptual --bits-per-pixel-2--bits-per-pixel-4--alpha-is-independent --alpha-is-opacity --punchthrough-unused --punchthrough-allowed --punchthrough-forcedASTC --block-width-4--block-width-5--block-width-6--block-width-8--block-width-10--block-width-12--compression-mode-veryfast --compression-mode-fast --compression-mode-medium --compression-mode-thorough --compression-mode-exhaustive --srgb-yes --srgb-no --block-height-4--block-height-5--block-height-6--block-height-8--block-height-10--block-height-12Formats: RawPVRKTX
texturetool如果不提供其他選項的話,默認(rèn)為--bits-per-pixel-4, --channel-weighting-linear和-f Raw肝断。
--bits-per-pixel-2和--bits-per-pixel-4選項創(chuàng)建PVRTC數(shù)據(jù)杈曲,將源像素編碼為每像素2或4位驰凛。 與未壓縮的32位RGBA圖像數(shù)據(jù)相比,這些選項代表固定的16:1和8:1壓縮率担扑。 最小數(shù)據(jù)大小為32字節(jié); 壓縮器從不產(chǎn)生比此更小的文件恰响,并且至少在上傳壓縮紋理數(shù)據(jù)時預(yù)計會有許多字節(jié)。
當(dāng)壓縮時涌献,指定--channel-weighting-linear所有通道的平均壓縮誤差胚宦。 相反,與線性選項相比燕垃,指定--channel-weighting-perceptual嘗試減少綠色通道中的錯誤间唉。 一般來說,PVRTC壓縮比攝影圖像比線條藝術(shù)更好利术。
-m選項會自動為源圖像生成mipmap級別呈野。 這些級別作為創(chuàng)建的存檔中的附加映像數(shù)據(jù)提供。 如果您使用原始圖像格式印叁,則每個級別的圖像數(shù)據(jù)將一個接一個地附加到存檔被冒。 如果使用PVR歸檔格式,則每個mipmap圖像都將作為存檔中的單獨圖像提供轮蜕。
(-f)參數(shù)控制其輸出文件的格式昨悼。 默認(rèn)格式為Raw。 此格式是原始壓縮紋理數(shù)據(jù)跃洛,無論是單個紋理級別(不使用-m選項)還是連接在一起的每個紋理級別(使用-m選項)率触。 存儲在文件中的每個紋理級別的大小至少為32字節(jié),并且必須將其全部上傳到GPU汇竭。 PVR格式與Imagination Technologies的PowerVR SDK中找到的PVRTexTool所使用的格式相匹配葱蝗。 要加載PVR壓縮紋理,請使用GLKTextureLoader類细燎。
-s和-c選項在編碼期間打印錯誤指標(biāo)两曼。 -s選項將輸入(未壓縮)圖像與輸出(編碼)圖像進(jìn)行比較,-c選項可以比較任何兩個圖像玻驻。 比較結(jié)果包括均方根誤差(rms)悼凑,感知加權(quán)pRms,最壞情況紋理錯誤(max)和每個統(tǒng)計量(rmsC璧瞬,pRmsC和maxC)的基于合成的版本户辫。 基于合成的錯誤假定圖像的Alpha通道用于不透明度,并且每個紋素的顏色與最壞情況的目標(biāo)顏色混合嗤锉。
在優(yōu)化壓縮圖像時渔欢,與-s和-c選項以及編碼器一起使用的錯誤指標(biāo)默認(rèn)情況下將圖像的Alpha通道視為獨立通道(或使用--alpha-is-independent選項時)。--alpha-is-opacity選項根據(jù)標(biāo)準(zhǔn)混合操作將錯誤度量值更改為一個档冬,如通過調(diào)用glBlendFunc(GL_SRC_ALPHA膘茎,GL_ONE_MINUS_SRC_ALPHA)來實現(xiàn)的。
PVR紋理壓縮支持特殊的穿透模式酷誓,可以在每個4x4塊的基礎(chǔ)上啟用披坏。 該模式限制了塊內(nèi)可以使用的顏色漸變,但是引入了將像素的Alpha值強制為0的選項盐数。它可以打敗PVRTC平滑色彩插值棒拂,引入塊邊界偽像,因此應(yīng)謹(jǐn)慎使用玫氢。 三個突破性選項是:
--punchthrough-unused帚屉。沒有穿透(默認(rèn)選項)。
--punchthrough-allowed漾峡。當(dāng)優(yōu)化圖像質(zhì)量時攻旦,編碼器可以逐塊地實現(xiàn)穿透。 該選項通常改進(jìn)了壓縮算法使用的目標(biāo)(每像素)誤差度量生逸,但可能會引入主觀的偽像牢屋。
--punchthrough-forced。 每個塊都啟用Punchthrough槽袄,限制顏色漸變烙无,但是可以讓塊中的任何像素的alpha為0。此選項主要用于完整性遍尺,但是當(dāng)結(jié)果在視覺上與其他選項進(jìn)行比較時還是有用的截酷。
重要信息:編碼器的源圖像必須滿足以下要求:
高度和寬度必須至少為8。
高度和寬度必須是2的冪乾戏。
必須是正方形(height == width)迂苛。
源圖像必須采用Image IO在OS X中接受的格式。為獲得最佳效果鼓择,原始紋理應(yīng)以未壓縮的數(shù)據(jù)格式開始灾部。
重要提示:如果您使用PVRTexTool來壓縮紋理,那么您必須創(chuàng)建張方形的并且是2的次冪長度的紋理惯退。 如果您的應(yīng)用程序嘗試在iOS中加載非平方或非2的次冪長度的紋理赌髓,則會返回錯誤。
// Listing C-2? Encoding images into the PVRTC compression formatEncode Image.png into PVRTCusinglinear weightsand4bpp,andsaving as ImageL4.pvrtcuser$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-4-o ImageL4.pvrtc Image.pngEncode Image.png into PVRTCusingperceptual weightsand4bpp,andsaving as ImageP4.pvrtcuser$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-4-o ImageP4.pvrtc Image.pngEncode Image.png into PVRTCusinglinear weightsand2bpp,andsaving as ImageL2.pvrtcuser$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-2-o ImageL2.pvrtc Image.pngEncode Image.png into PVRTCusingperceptual weightsand2bpp,andsaving as ImageP2.pvrtcuser$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-2-o ImageP2.pvrtc Image.png
// Listing C-3? Encoding images into the PVRTC compression format while creating a previewEncode Image.png into PVRTC using linear weightsand4bpp,andsaving the outputasImageL4.pvrtcanda PNG previewasImageL4.pnguser$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-4-o ImageL4.pvrtc -p ImageL4.png Image.pngEncode Image.png into PVRTC using perceptual weightsand4bpp,andsaving the outputasImageP4.pvrtcanda PNG previewasImageP4.pnguser$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-4-o ImageP4.pvrtc -p ImageP4.png Image.pngEncode Image.png into PVRTC using linear weightsand2bpp,andsaving the outputasImageL2.pvrtcanda PNG previewasImageL2.pnguser$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-2-o ImageL2.pvrtc -p ImageL2.png Image.pngEncode Image.png into PVRTC using perceptual weightsand2bpp,andsaving the outputasImageP2.pvrtcanda PNG previewasImageP2.pnguser$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-2-o ImageP2.pvrtc -p ImageP2.png Image.png
注意:無需創(chuàng)建預(yù)覽也不能指定-o參數(shù)和有效的輸出文件催跪。 預(yù)覽圖像始終為PNG格式锁蠕。
要加載PVR壓縮紋理,請使用GLKTextureLoader類懊蒸。
有關(guān)直接使用PVR壓縮數(shù)據(jù)的示例荣倾,請參閱PVRTextureLoader示例。