Unity Ply網格讀取
Ply格式說明
The version 1.0 PLY format, 詳細參見:https://ww2.mathworks.cn/help/vision/ug/the-ply-format.html
File Header
文件頭示例’{}‘為說明:
ply {文件頭}
format binary_big_endian 1.0 {數據格式以及版本}
element vertex 9200 {定義vertex元素}
property float x {定義屬性}
property float y
property float z
element face 18000 {定義面元素}
property list uchar int vertex_indices
end_header {這行后面就是數據了}
其中,format <data format> <PLY version>
中的<data format>
支持(little/big endian refers to the byte ordering of multi-byte data):
- ascii
- binary_little_endian
- binary_big_endian
元素定義方式如下:
element <element name> <number in file>
property <data type> <property name 1>
property <data type> <property name 2>
property <data type> <property name 3>
...
其中對應的<data type>
有:
Name | Type |
---|---|
char | (8-bit) character |
uchar | (8-bit) unsigned character |
short | (16-bit) short integer |
ushort | (16-bit) unsigned short integer |
int | (32-bit) integer |
uint | (32-bit) unsigned integer |
float | (32-bit) single-precision float |
double | (64-bit) double-precision float |
對於list的屬性為:
property list <count data type> <data type> <property name>
在文件頭里面還可以添加注釋,具體格式如下:
comment <comment text>
Data
header之后存數據。按照之前約定的元素順序和屬性類型,依次存儲。
Common Elements and Properties
常見元素和屬性為:
Required Core Property | Element | Property | Data Type | Property Description |
---|---|---|---|---|
✓ | vertex | x | float |
x,y,z coordinates |
✓ | y | float |
||
✓ | z | float |
||
nx | float |
x,y,z of normal | ||
ny | float |
|||
nz | float |
|||
red | uchar |
vertex color | ||
green | uchar |
|||
blue | uchar |
|||
alpha | uchar |
amount of transparency | ||
material_index | int |
index to list of materials | ||
face | vertex_indices | list of int | indices to vertices | |
back_red | uchar |
backside color | ||
back_green | uchar |
|||
back_blue | uchar |
|||
edge | vertex1 | int |
index to vertex | |
vertex2 | int |
index to other vertex | ||
crease_tag | uchar |
crease in subdivision surface | ||
material | red | uchar |
material color | |
green | uchar |
|||
blue | uchar |
|||
alpha | uchar |
amount of transparency | ||
reflect_coeff | float |
amount of light reflected | ||
refract_coeff | float |
amount of light refracted | ||
refract_index | float |
index of refraction | ||
extinct_coeff | float |
extinction coefficent |
Unity Ply Reader
附上項目路徑:https://github.com/grassofsky/PlyImporter
關於unity導入網格主要需要考慮的問題有:
- 怎么支持顏色信息;
- 坐標系轉換;
- 三角形頂點組成順序轉換;
- 距離單位;
因為通常的軟件,在網格處理導出的時候,都是基於右手坐標系,頂點逆時針順序表明網格的正面,距離單位不固定;而在unity中,坐標系是基於左手坐標系,同時頂點的順時針順序表明是網格的正面,網格的背面通常不參與繪制,unity自帶的shader通常不支持基於頂點的顏色設置。為此,該項目在原有項目的基礎上做了一些改進以及擴展。
PlyImporter
原始項目的說明見:https://github.com/3DBear/PlyImporter
PLY (Polygon File Format) importer for Unity.
在原來基礎上的改動主要有
- 增加了PlyElement基類,專門用來處理不同的element,方便后期進行擴展,目前支持PlyFaceElement,PlyVertexElement;
- 增加了PlyProperty基類,處理不同的屬性,按照屬性的功能又分為PlyMultiProperty,用來支持單個property定義;PlyListProperty支持property list定義。
支持的功能以及需要滿足的要求
1. element vertex
元素頂點支持的格式類似如下:
element vertex 6462
property float x
property float y
property float z
property float nx
property float ny
property float nz
property uchar red
property uchar green
property uchar blue
property uchar alpha
其中
x,y,z,nx,ny,nz,red,green,blue,alpha
的名字是固定的;x,y,z
必須同時出現;類型必須是float
nx,ny,nz
必須同時出現;類型必須是float
red,green,blue,alpha
屬性必須同時出現;類型必須是uchar
property
定義的順序可以是亂序的;vertex
的名字是固定的;
2. element face
類似如下:
element face 12920
property list uchar int vertex_indices
其中
vertex_indices
的名字是固定的;face
的名字是固定的;- list中的具體index類型支持int和uint兩種格式;
- list中的count類型為uchar;
3. element g_material
考慮到unity中提供的shader一般不支持頂點顏色設置,此處對常規的ply進行了一定的擴展;在comment區域給出全局元素和屬性的設置;
comment element g_material
comment property uchar red 255
comment property uchar green 0
comment property uchar blue 0
comment property uchar alpha 255
其中:
g_material
名字是固定的;red,green,blue,alpha
名字是固定的,必須是uchar
4. element g_meshinfo
考慮到需要識別網格名稱,引入該元素,具體如下:
comment element g_meshinfo
comment property string name what_ever_you_want
comment property string unit mm { one of cm/m/mm }
comment property string x_inner x { one of x/y/z/-x/-y/-z}
comment property string y_inner y { one of x/y/z/-x/-y/-z}
comment property string z_inner z { one of x/y/z/-x/-y/-z}
comment property string coordinate right { one of right/left }
其中:
g_meshinfo
名字是不定的;name,unit,x_inner,y_inner,z_inner,coordinate
名字是固定的;name,unit,x_inner,y_inner,z_inner,coordinate
的類型必須是string
;- 具體的名字
what_ever_you_want
中不能帶空格; unit
的值為mm/cm/m
中的一個;coordinate
的值為right/left
中的一個,right表示右手坐標系,left表示左手坐標系;comment property string x_inner x
前一個x_inner
指的是unity坐標系中的x軸,后一個x對應網格數據中的x軸;用於定義坐標變換;- property的順序也可變化;
5. data store order
必須先存儲頂點element,然后再存儲face element;
TODO
- 支持PlyEdgeElement;
- Support for Binary Big Endian
- PLY exporting