2013年3月21日 星期四

3dsMax - 請小心FBX Export Preset的設定

今天遇到了一個超奇妙的問題,當我輸出FBX要Bake Animation的時候,我的時間區段一直只能設定0到100之間,但昨天明明還是正常的,查了將近一整天一直無法解決,後來請高手支援一下才發現原來是個愚蠢的問題!

那就是FBX Export Preset中可以設定Start跟End的最大最小值,而這該死的被設定成了0到100,至於為何突然會被設定成0至100之間,這還是一個謎!不過我猜是因為我用了MAXScript的FbxExporterSetParam的關係‧‧‧

設定FBX Export Preset的步驟

  1. 在FBX Exporter dialog中按下Edit按鈕並選擇Edit preset
  2. 會跳出上圖左方的Tree List找到Bake Animation的Start/End項目並右鍵點擊選擇Edit min max
  3. 輸入Minimum及Maximum數值
但目前還不知道要怎麼將他回復成預設狀態‧‧‧

另外要用MAXScript設定Start/End的話可以使用

FBXExporterSetParam "BakeFrameStart" [Integer value]

FBXExporterSetParam "BakeFrameEnd" [Integer value]

FBXExporterSetParam "BakeFrameStep" [Integer value]

2013年3月19日 星期二

[AE] Camera Mapping

想用一張2D照片做出3D空間的效果,所使用的技術為Camera Mapping或Camera Projection。


教學:
  1. 較陽春的作法,適合構圖簡單和沒這麼多時間生小孩的時候用XD,將元素切分成幾個圖層,拉出前後距離即可。
    How To Make Virtual 3D Photos In After Effects by AndrewKramer
  2. 使用AE的Light shadow做出投影的效果,只需用幾片板子即可做出簡單的3D空間效果。
    而在實際用過之後發現有幾點需要注意。在結構較複雜的場景中,板子的對位非常重要,否則會有很大的破綻。另一點則是,投影出來的圖像解析度跟原圖差距相當大,因為通常原圖是被投影到更大的面積上,且有角度造成的失真,儘管把Shadow Map Resolution調到最大也還是達不到可用的水準,目前還不知道較佳解決的方法,若解析度不佳,那這種方法也就沒路用了。
    教學:
    Camera Mapping in After Effects by Creative Cow
    2D footage to stereo 3D using camera projection mapping
    Camera Projection technique Tutorial
    其他範例:峽谷
  3. 利用建模做出更真實的3D效果。
    Discover projection mapping to animate photos in 3D
  4. 另外我有試出一種偷吃步的方式:傾斜大法!
    在AE中利用Effect/Corner pin做出Skew,再把與後方景物位差明顯的物件拆出來,不做傾斜並跟著會傾斜的底圖移動,就能有空間感的錯覺。但目前只試過水平運鏡的狀況,也許垂直運鏡跟深度運鏡會較難表現。
延伸閱讀:
[Wiki] 3D Projection 

[MAXScript] Path處理

以前在處理檔案路徑時,有些功能都自己寫Function來用,例如將兩段路徑結合成一個完整路徑等等。今天看MAXScript Reference時才發現,MAXScript自己就有許多Methods可以對路徑做各種的處理,非常方便。

MAXScript Reference - PathConfig Struct
以下從中節錄出較常用的項目

pathConfig.appendPath 
將路徑片段合併
pathConfig.appendPath "c:\\temp" "test.txt"
"c:\temp\test.txt"

pathConfig.convertPathToLowerCase
- 將路徑轉為小寫並將反斜槓統一

pathConfig.convertPathToLowerCase "C:\\Temp\Test/pATH"
"c:\temp\test\path"


pathConfig.pathsResolveEquivalent
- 比對兩個路徑是否相同

pathConfig.pathsResolveEquivalent "scenes\\test.txt" "./scenes/test.txt"
true


pathConfig.removePathLeaf
- 取得上一層的目錄路徑

pathConfig.removePathLeaf "c:\\temp\\test"
"c:\temp"



pathConfig.stripPathToTopParent 
- 取得最上層的路徑

pathConfig.stripPathToTopParent "C:\\temp\\test\\test.tga"
"C:\"
pathConfig.stripPathToTopParent "\\\\SomeServer\\Projects\\Test"
"\\SomeServer"



pathConfig.removePathTopParent
- 去除最上層的路徑

pathConfig.removePathLeaf "c:\\temp\\test"
"temp\test"


pathConfig.stripPathToLeaf
- 取得最深一層的路徑,與filenameFromPath作用相同

pathConfig.stripPathToLeaf "C:\\temp\\test"
"test"
pathConfig.stripPathToLeaf "C:\\temp\\test\\"
""
pathConfig.stripPathToLeaf "C:\\temp\\test\\somefile.tga"
"somefile.tga"


pathConfig.isAbsolutePath
- 檢驗是否是絕對路徑(但不檢驗該路徑是否存在)

pathConfig.isLegalPath
- 檢驗是否是合法的路徑(但不檢驗該路徑是否存在)

pathConfig.GetDir <#directory_type>
- 傳回系統預設目錄路徑

#font | #Scene | #export | #import | #help | #expression | #preview | #image | #Sound | #plugcfg | #maxstart | #vpost | #drivers | #autoback | #matlib | #scripts | #startupScripts | #defaults | #renderPresets | #ui | #maxroot | #renderoutput | #animations | #archives | #Photometric | #renderassets | #userScripts | #userMacros | #userStartupScripts | #temp | #userIcons | #maxData | #downloads | #proxies


pathConfig.isPathRootedAtBackslash
- 檢驗路徑是否是以斜槓為開頭

要取得Max的一些預設路徑則從此查找
MAXScript Reference - 3ds Max System Directories

2013年3月12日 星期二

如何不影響Scale的數值對Bone做到縮放尺寸的效果

如何不影響Scale的數值對Bone做到縮放尺寸的效果

例,在對原本Scale值為[1,1,1]的Bone,進行Scale 2倍的動作後,Scale值變為[2,2,2],若想在保持現有尺寸的狀態下把Bone的Scale值歸回[1,1,1],可利用Animation>Bone Tools,方法如下:
  1. 開啟Bone Tools視窗。
  2. 選擇要修改的Bone,執行Reset Scale,此時Scale值已被修正為1,但Bone的顯示有問題,用以下步驟進行修正。
  3. 將Bone On關掉再打開,做個Refresh動作。
  4. 執行Reset Stretch。
  5. 執行Realign。

另外可用Max Script進行這個手續:
for o in selection do
(
    resetScale o
    o.setBoneEnable false currenttime
    o.setBoneEnable true currenttime
    o.resetBoneStretch()
    o.realignBoneToChild()
)

2013年3月7日 星期四

Unity3D輸出之圖檔壓縮格式(行動平台)

要對圖檔壓縮進行優化時,須先了解各圖檔之規格,Unity Manual中有提到各平台可支援的各種圖檔格式,請參考在本文結尾的參考文章中的[1]以及Wiki中的資訊。

iOS:
PVRCT
  貼圖尺寸須為二的冪次方之正方貼圖
Android:
RGB Compressed ETC 4 bits (ETC1)
 (Ericsson Texture Compression, supported by all OpenGL ES 2.0 GPUs)
 4bits / pixel,無Alpha,Android 2.2(Froyo)以上支援
RGBA 16bit
 有Alpha,容量是ETC的四倍(請看參考[1])。

若要減少Alpha對資源的佔用,有一些技巧可以解決(請看參考[3][4])。
 方法1,將Alpha Channel抽出,與Color Map連接拼成一張貼圖。
  優點:一張貼圖、Shader改動較小。
  缺點:貼圖只能二方連續(one direction wrap),提高shader效能消耗
 方法2,將Alpha Channel抽出,單獨存成一個檔案,再從shader進行結合。
  優點:貼圖可四方連續,運用上靈活性較高。
  缺點:shader需要額外多出一個sampler。
當硬體不支援輸出之壓縮格式時,Unity會將資源以RGBA的方式做Decompress,如此會提高渲染時間、記憶體消耗和Decompress時間(增加讀取階段的時間)。

下表是檔案容量對照(含MipMaps)

尺寸 ETC 4bits RGBA 16bit RGBA 32bit
2048
2.7 mb
10.7 mb
21.3mb
1024 682.8
2.7 mb
5.3mb
512 170.8 682.7
1.3mb
256 42.8 170.7 341.3
128 10.7 42.8 85.3
64 2.7 10.7 21.3


參考文章:
[1]http://docs.unity3d.com/Documentation/Manual/Textures.html
[2]http://docs.unity3d.com/Documentation/Manual/android-GettingStarted.html
[3]http://forum.unity3d.com/threads/73998-DXT-or-PVRTC
[4]http://malideveloper.arm.com/develop-for-mali/sample-code/etcv1-texture-compression-and-alpha-channels/
http://gamedev.stackexchange.com/questions/44134/unity-android-truecolor-texture-performance-hit-and-alternatives-for-truecolor
http://stackoverflow.com/questions/9148795/android-opengl-texture-compression

http://zh.wikipedia.org/wiki/RGB
http://en.wikipedia.org/wiki/Ericsson_Texture_Compression
http://en.wikipedia.org/wiki/PVRTC

2012年5月25日 星期五

利用nvidia DDS Utilities - nvDXT進行DDS轉檔



最近嘗試了幾種dds轉檔的方式,用maxScript的缺陷是在於無法產生alpha,用python透過com操作photoshop則是運行耗時、且我找不到dds的save option,利用nvidia DDS Utilities來進行dds轉檔看來是目前最好的方法。

nvidia DDS Utilities是什麼?nvidia的說明是:
included are a set of utilities for manipulating DDS image files, including:
nvDXT, a command-line binary version of the nvDXT library, 
detach, a tool that extracts MIP levels from a DDS file,
stitch, a tool that recombines MIP levels into a single DDS file and
readDXT, which reads compressed images and writes TGA files.
Download DDS Utilities >> http://developer.nvidia.com/legacy-texture-tools

利用nvDXT即可用command-line的方式來輸出.dds檔案,最簡單的命令如下
nvdxt -profile profile -file file -outdir outdir
nvdxt -profile profile -file file -outsamedir
很方便,可以讀取.psd,-file可運用萬用字元,還可指定profile(由photoshop產生)

----------以下是nvDXT本身的說明內容----------
This program
   compresses images
   creates normal maps from color or alpha
   creates DuDv map
   creates cube maps
   writes out .dds file
   does batch processing
   reads .tga, .bmp, .gif, .ppm, .jpg, .tif, .cel, .dds, .png, .psd, .rgb, *.bw and .rgba
   filters MIP maps


Options:
  -profile : Read a profile created from the Photoshop plugin
  -quick : use fast compression method
  -quality_normal : normal quality compression
  -quality_production : production quality compression
  -quality_highest : highest quality compression (this can be very slow)
  -rms_threshold : quality RMS error. Above this, an extensive search is performed.
  -prescale : rescale image to this size first
  -rescale : rescale image to nearest, next highest or next lowest power of two
  -rel_scale : relative scale of original image. 0.5 is half size Default 1.0, 1.0


Optional Filtering for rescaling. Default cube filter:
  -RescalePoint
  -RescaleBox
  -RescaleTriangle
  -RescaleQuadratic
  -RescaleCubic
  -RescaleCatrom
  -RescaleMitchell
  -RescaleGaussian
  -RescaleSinc
  -RescaleBessel
  -RescaleHanning
  -RescaleHamming
  -RescaleBlackman
  -RescaleKaiser

  -clamp : maximum image size. image width and height are clamped
  -clampScale : maximum image size. image width and height are scaled
  -window : window of original window to compress
  -nomipmap : don't generate MIP maps
  -nmips : specify the number of MIP maps to generate
  -rgbe : Image is RGBE format
  -dither : add dithering
  -sharpenMethod : sharpen method MIP maps
    is
        None
        Negative
        Lighter
        Darker
        ContrastMore
        ContrastLess
        Smoothen
        SharpenSoft
        SharpenMedium
        SharpenStrong
        FindEdges
        Contour
        EdgeDetect
        EdgeDetectSoft
        Emboss
        MeanRemoval
        UnSharp
        XSharpen
        Custom
  -pause : wait for keyboard on error
  -flip : flip top to bottom
  -timestamp : Update only changed files
  -list : list of files to convert
  -cubeMap : create cube map .
            Cube faces specified with individual files with -list option
            positive x, negative x, positive y, negative y, positive z, negative z
            Use -output option to specify filename
            Cube faces specified in one file.  Use -file to specify input filename
  -volumeMap : create volume texture.
            Volume slices specified with individual files with -list option
                  Use -output option to specify filename
            Volume specified in one file.  Use -file to specify input filename

  -all : all image files in current directory
  -outdir : output directory
  -deep [directory]: include all subdirectories
  -outsamedir : output directory same as input
  -overwrite : if input is .dds file, overwrite old file
  -forcewrite : write over readonly files
  -file : input file to process. Accepts wild cards
  -output : filename to write to [-outfile can also be specified]
  -append : append this string to output filename


  -8    : compress 8 bit images with this format
  -16 : compress 16 bit images with this format
  -24  : compress 24 bit images with this format
  -32  : compress 32 bit images with this format

  -swapRB : swap rb
  -swapRG : swap rg
  -gamma : gamma correcting during filtering
  -outputScale : scale the output by this (r,g,b,a)
  -outputBias : bias the output by this amount (r,g,b,a)
  -outputWrap : wraps overflow values modulo the output format
  -inputScale : scale the inpput by this (r,g,b,a)
  -inputBias : bias the input by this amount (r,g,b,a)
  -binaryalpha : treat alpha as 0 or 1
  -alpha_threshold : [0-255] alpha reference value
  -alphaborder : border images with alpha = 0
  -alphaborderLeft : border images with alpha (left) = 0
  -alphaborderRight : border images with alpha (right)= 0
  -alphaborderTop : border images with alpha (top) = 0
  -alphaborderBottom : border images with alpha (bottom)= 0
  -fadeamount : percentage to fade each MIP level. Default 15
  -fadecolor : fade map (color, normal or DuDv) over MIP levels
  -fadetocolor : color to fade to
  -custom_fade : set custom fade amount.  n is number number of fade amounts. fadeamount are [0,1]
  -fadealpha : fade alpha over MIP levels
  -fadetoalpha : [0-255] alpha to fade to
  -border : border images with color
  -bordercolor : color for border
  -force4 : force DXT1c to use always four colors
  -weight : Compression weightings for R G and B
  -luminance :  convert color values to luminance for L8 formats
  -greyScale : Convert to grey scale
  -greyScaleWeights : override greyscale conversion weights of (0.3086, 0.6094, 0.0820, 0)
  -brightness : per channel brightness. Default 0.0 usual range [0,1]
  -contrast : per channel contrast. Default 1.0  usual range [0.5, 1.5]

Texture Format  Default DXT3:
  -dxt1c   : DXT1 (color only)
  -dxt1a   : DXT1 (one bit alpha)
  -dxt3    : DXT3
  -dxt5    : DXT5n
  -u1555   : uncompressed 1:5:5:5
  -u4444   : uncompressed 4:4:4:4
  -u565    : uncompressed 5:6:5
  -u8888   : uncompressed 8:8:8:8
  -u888    : uncompressed 0:8:8:8
  -u555    : uncompressed 0:5:5:5
  -p8c     : paletted 8 bit (256 colors)
  -p8a     : paletted 8 bit (256 colors with alpha)
  -p4c     : paletted 4 bit (16 colors)
  -p4a     : paletted 4 bit (16 colors with alpha)
  -a8      : 8 bit alpha channel
  -cxv8u8  : normal map format
  -v8u8    : EMBM format (8, bit two component signed)
  -v16u16  : EMBM format (16 bit, two component signed)
  -A8L8    : 8 bit alpha channel, 8 bit luminance
  -fp32x4  : fp32 four channels (A32B32G32R32F)
  -fp32    : fp32 one channel (R32F)
  -fp16x4  : fp16 four channels (A16B16G16R16F)
  -dxt5nm  : dxt5 style normal map
  -3Dc     : 3DC
  -g16r16  : 16 bit in, two component
  -g16r16f : 16 bit float, two components

Mip Map Filtering Options. Default box filter:
  -Point
  -Box
  -Triangle
  -Quadratic
  -Cubic
  -Catrom
  -Mitchell
  -Gaussian
  -Sinc
  -Bessel
  -Hanning
  -Hamming
  -Blackman
  -Kaiser

***************************
To make a normal or dudv map, specify one of
  -n4 : normal map 4 sample
  -n3x3 : normal map 3x3 filter
  -n5x5 : normal map 5x5 filter
  -n7x7 : normal map 7x7 filter
  -n9x9 : normal map 9x9 filter
  -dudv : DuDv

and source of height info:
  -alpha : alpha channel
  -rgb : average rgb
  -biased : average rgb biased
  -red : red channel
  -green : green channel
  -blue : blue channel
  -max : max of (r,g,b)
  -colorspace : mix of r,g,b
  -norm : normalize mip maps (source is a normal map)
  -toHeight : create a height map (source is a normal map)


Normal/DuDv Map dxt:
  -aheight : store calculated height in alpha field
  -aclear : clear alpha channel
  -awhite : set alpha channel = 1.0
  -scale : scale of height map. Default 1.0
  -wrap : wrap texture around. Default off
  -minz : minimum value for up vector [0-255]. Default 0

***************************
To make a depth sprite, specify:
  -depth

and source of depth info:
  -alpha  : alpha channel
  -rgb    : average rgb (default)
  -red    : red channel
  -green  : green channel
  -blue   : blue channel
  -max    : max of (r,g,b)
  -colorspace : mix of r,g,b

Depth Sprite dxt:
  -aheight : store calculated depth in alpha channel
  -aclear : store 0.0 in alpha channel
  -awhite : store 1.0 in alpha channel
  -scale : scale of depth sprite (default 1.0)
  -alpha_modulate : multiplies color by alpha during filtering
  -pre_modulate : multiplies color by alpha before processing


Examples
  nvdxt -cubeMap -list cubemapfile.lst -output cubemap.dds
  nvdxt -cubeMap -file cubemapfile.tga
  nvdxt -file test.tga -dxt1c
  nvdxt -file *.tga
  nvdxt -file c:\temp\*.tga
  nvdxt -file temp\*.tga
  nvdxt -file height_field_in_alpha.tga -n3x3 -alpha -scale 10 -wrap
  nvdxt -file grey_scale_height_field.tga -n5x5 -rgb -scale 1.3
  nvdxt -file normal_map.tga -norm
  nvdxt -file image.tga -dudv -fade -fadeamount 10
  nvdxt -all -dxt3 -gamma -outdir .\dds_dir -time
  nvdxt -file *.tga -depth -max -scale 0.5

Send comments, bug fixes and feature requests to texturetools@nvidia.com

2012年5月9日 星期三

Regular Expressions

Regular Expressions 可簡稱 re / regex / regexp
中文則為:正則表示式、正規表示式...
下面的連結對RE有詳細的解說
http://www.regular-expressions.info/reference.html
Regular-Expressions.info
石頭閒語 : RE in JavaScript

RE JavaScript Tester

MAXscript不支援RE(至少Max9是),但是可以藉由dotNet來實行,請見下面連結
For obj in $ do : RE with MAXscript
rx = dotNetClass "System.Text.RegularExpressions.RegEx"
pattern = "^([A-Za-z0-9]+_){2}\d{3}"
s = "robot_leftArm_001"
if (((rx.match s pattern).success) == true) then (print "success") else (print "fail!")

2012年5月3日 星期四

MAXscript : Find biped

想要在max檔案中找出所有的biped有什麼方法比較好?
主要是透過取得Biped的根骨架就能夠判別出有幾組biped骨架,而方法有好幾種...
  1. 利用RootNode這個Property
     bipedNode.controller.rootNode
    這個方式是從網上看到的(影片來源),透過能從所有biped骨頭中獲取的rootNode這個property,可取得biped根骨架 。另外也可使用rootName這個property直接獲取rootNode的name。
    //
    for o in geometry where classof o == Biped_Object and o.controller.rootNode == o do print o
  2. 透過Controller的類型
     classof bipedNode.controller
    利用判別controller的類型來尋找,root的controller類型為Vertical_Horizontal_Turn,root外的biped parts都會是BipSlave_Control,Footsteps即為Footsteps。
    //
    for o in geometry where classof o.controller == Vertical_Horizontal_Turn do print o
  3. 透過biped.getNode這個指令
     biped.getNode [bipedNode | bipedCtrl] [limbName | index]
    只要知道固定部位的limb name或者index即可用biped.getNode傳回該node,而根骨架的limb name是#vertical、#horizontal、#turn,index為13~15。詳見MAXScript Reference:Biped Node Hierarchy

2012年4月27日 星期五

MAXscript : constraints appendTarget

近日用MAXscript來做批次處理,其中有個步驟是先切biped的FigureMode為true,然後Merge物件並appendTarget $Bip01,但是發現取消FigureMode之後物件的座標點仍是merge時的座標,並未依據$Bip01的位移改變。後來發現在appendTarget後必需要取用物件的position(或者直接取用transform)資訊才會正常。不過若是一步一步手動執行,就沒有這個問題,也不是因為使用disableSceneRedraw、undo off、用fn進行addConstraint所導致。

2012年4月18日 星期三

MAXscript : Skin Copy/Paste

--
--選擇已Skin物件執行Copy, 自動記錄bones和存出env, 再選擇要套用Skin的物件執行Paste套回
--需要設定tempDir供存出env檔案用
--by JanusHaung
skinBonesArray = #()
tempDir = "D:/MaxProject/export/"
envFilename = ""

rollout skinCopyPaste "Skin Copy Paste" width:185 height:50
(
 button btn_copy "Copy" across:2
 button btn_paste "Paste"
 label lbl1 ""

 on btn_copy pressed do (
  -- 選擇已skin好的物件並執行
  if $.modifiers[#Skin] != undefined do (
   skinBonesArray = for i in 1 to skinops.getnumberbones $.skin collect skinops.getbonename $.skin i 0
   envFilename = tempDir + $.name + ".env"
   skinOps.saveEnvelope $.skin envFilename
   lbl1.caption = "Skin copied."
  )
 )
 
 on btn_paste pressed do (
  max modify mode
  if skinBonesArray.count > 0 do (
   -- 選擇尚未skin好的模型執行
   if $.modifiers[#Skin] == undefined do addmodifier $ (Skin())
   for i in 1 to skinBonesArray.count do skinOps.addbone $.skin ( getNodeByName skinBonesArray[i] ) 0
   --先執行completeRedraw()才可成功LoadEnvlope
   completeRedraw()
   skinOps.LoadEnvelope $.skin envFilename
   lbl1.caption = "Skin pasted."
  )
 )
)

createDialog skinCopyPaste

[MAXscript] 將所選擇物件轉為物件陣列形式

可將選擇中的物件名稱以#( $node1, $node2, ... )物件陣列的方式印出來

-- 將所選擇的物件名稱輸出成物件陣列形式 by Janus Huang

-- 紀錄下所有物件的名稱
tmp = for o in selection collect o.name

out = "#( "

--把物件名稱去除掉空白格
for i in 1 to tmp.count where matchpattern tmp[i] pattern:"* *" do (

    strNew = ""
    
    --留下space外的字串成array
    strTmp = filterString tmp[i] " "
    
    --將array結合成string後重新丟回
    for s in strTmp do strNew += s
    tmp[i] = strNew
)

for i in 1 to tmp.count do (
    --把物件名稱去除掉()
    if matchpattern tmp[i] pattern:"*(*)*" then
    (
        strNew = "getNodeByName \""+tmp[i]+"\""
        tmp[i] = strNew
    ) else (
        tmp[i]="$"+tmp[i]
    )

    if i != tmp.count then out += tmp[i]+", " else out += tmp[i]+" )"
)

print out