Samples/Geometry/VertexArrayManipulation.py

Samples/Geometry/VertexArrayManipulation.py
1 # Copyright 2012 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # This script is to demonstrate the fast mesh creation via python by directly manipulating of vertex array data.
7 #
8 # Topic: FBGeometry, FBMesh, FBGeometryArrayID, FBModelTransformationType, FBModelShadingMode, FBShaderManager
9 #
10 
11 from pyfbsdk import *
12 import os
13 
14 def CreateMultiPatchMesh(pArrayIds):
15  # Create mesh object
16  lMesh = FBMesh("myGeom")
17 
18  # Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
19  lMesh.GeometryBegin()
20 
21  # Call VertexArrayInit() to init vertex/normal array, with multi patch support.
22  lMesh.VertexArrayInit(5, False, pArrayIds)
23 
24  # pack position vector with 3 float component into list
25  lPosList = [0, 100, 0, 100,100, 0, 100, 0, 0, 0, 0, 0, 50, 150, 0]
26  lMesh.SetPositionsArray(lPosList)
27 
28  # pack UV vector 2 float component into list, range [0, 1]
29  lUVList = [0, 1, 1, 1, 1, 0, 0, 0, 0.5, 0]
30  lMesh.SetUVSetDirectArray(lUVList)
31 
32  # pack normalized vector with 3 float component into list
33  lNormalList = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]
34  lMesh.SetNormalsDirectArray(lNormalList)
35 
36  if (pArrayIds & FBGeometryArrayID.kFBGeometryArrayID_Binormal):
37  # pack normalized vector with 3 float component into list
38  lBinormalList = [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]
39  lMesh.SetBinormalsDirectArray(lBinormalList)
40 
41  if (pArrayIds & FBGeometryArrayID.kFBGeometryArrayID_Tangent):
42  # pack normalized vector with 3 float component into list
43  lTangentList = [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
44  lMesh.SetTangentsDirectArray(lTangentList)
45 
46  if (pArrayIds & FBGeometryArrayID.kFBGeometryArrayID_Color):
47  # pack color with 4 float component (R, G, B, A) into list, range[0, 1]
48  lColorList = [0.5, 0.5, 0, 0.5, 0, 0.5, 0.5, 0.5, 1, 0, 0, 0.5, 0, 1, 0, 0.5, 1, 1, 0, 0.5]
49  lMesh.SetVertexColorsDirectArray(lColorList)
50 
51  # Add one triangle TriangleStripAdd
52  lTriStrip = [0, 1, 3]
53  lMesh.TriangleStripAdd(3, lTriStrip, 0)
54 
55  # Add one triangle via TriangleListAdd
56  lTriList = [1, 3, 2]
57  lMesh.TriangleListAdd(3, lTriList, 1)
58 
59  # Add one triangle via PolygonListAdd
60  lPolygonList = [0, 1, 4]
61  lMesh.PolygonListAdd(3, 3, lPolygonList, 2)
62 
63  # Or user could set Material Mapping for each polygon directly
64  # lMatList = [0, 1, 2]
65  # lMesh.SetMaterialIndexArray(lMatList)
66 
67  # Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
68  lMesh.GeometryEnd()
69 
70  return lMesh
71 
72 # Define Geometry Array Ids
73 gArrayIds = \
74  [ \
75  0, \
76  FBGeometryArrayID.kFBGeometryArrayID_Color, \
77  FBGeometryArrayID.kFBGeometryArrayID_Tangent, \
78  FBGeometryArrayID.kFBGeometryArrayID_Binormal, \
79  FBGeometryArrayID.kFBGeometryArrayID_Tangent | FBGeometryArrayID.kFBGeometryArrayID_Binormal \
80  ]
81 
82 # Create materials
83 gMat1 = FBMaterial("Mat1")
84 gMat1.Diffuse = FBColor(0, 1, 0)
85 
86 gMat2 = FBMaterial("Mat2")
87 gMat2.Diffuse = FBColor(1, 0, 0)
88 
89 gMat3 = FBMaterial("Mat3")
90 gMat3.Diffuse = FBColor(0, 0, 1)
91 
92 # Create Shader Manager
93 gShaderMgr = FBShaderManager()
94 
95 # Define CgFx Source File Path
96 gCgFxSrcFile = os.path.abspath(os.path.join(FBSystem().ApplicationPath, "../config/Scripts/Samples/Geometry/DiffuseUVNormalTangentBinormal.cgfx"))
97 # print gCgFxSrcFile
98 
99 gNumOfInstance = len(gArrayIds)
100 for lIndex in range(0, gNumOfInstance):
101  #create a cube but we will replace its geometry
102  lModel = FBModelCube("myModel")
103 
104  # Replace the geometry instance
105  lModel.Geometry = CreateMultiPatchMesh(gArrayIds[lIndex])
106 
107  # Connect Material
108  lModel.SetVector( FBVector3d( (lIndex * 2.0 - gNumOfInstance) * 55, 50, 50 ) )
109 
110  if (lIndex == 0):
111  lModel.ConnectSrc(gMat1)
112  lModel.ConnectSrc(gMat2)
113  lModel.ConnectSrc(gMat3)
114 
115  elif (lIndex == 1):
116  lModel.ShadingMode = FBModelShadingMode.kFBModelShadingFlat
117 
118  else:
119  lCgFxShader = gShaderMgr.CreateShader("CgFxShader")
120 
121  lProp = lCgFxShader.PropertyList.Find("CgFxPath")
122  lProp.SetString(gCgFxSrcFile)
123 
124  lProp = lCgFxShader.PropertyList.Find("Diffuse/UV/Normal/Tangent/Binormal")
125  lProp.SetString( str(lIndex) + ".5")
126 
127  lModel.ConnectSrc(lCgFxShader)
128 
129  # The object must be set visible to be present in the system.
130  lModel.Visible = True
131  lModel.Show = True
132