Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mesh normals textures #20

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
163 changes: 108 additions & 55 deletions osimConverters/DecorativeGeometryImplementationGltf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def init(self):
self.mapMobilizedBodyIndexToNodeIndex = {}
self.processingPath = False
self.currentPathMaterial = None
self.currentTexture = 'default'
self.mapPathToMaterialIndex = {}
self.mapPathsToNodeIds = {}
self.mapPathsToNodeTypes = {}
Expand Down Expand Up @@ -198,6 +199,7 @@ def implementMeshFileGeometry(self, arg0):
arg0.getMeshFile().casefold().endswith(".obj")):
polygonalMesh = osim.PolygonalMesh()
polygonalMesh.loadFile(arg0.getMeshFile())
print("Loading file", arg0.getMeshFile())
self.createGLTFNodeAndMeshFromPolygonalMesh(arg0, "Mesh"+arg0.getMeshFile(), polygonalMesh, self.getMaterialIndexByType())
else:
raise ValueError("Unsupported file extension")
Expand Down Expand Up @@ -348,23 +350,27 @@ def addDefaultMaterials(self):
self.MaterialGrouping["WrapCylinder"] = "Wrapping"
self.MaterialGrouping["WrapEllipsoid"] = "Wrapping"
self.MaterialGrouping["WrapTorus"] = "Wrapping"
self.mapTypesToMaterialIndex["Mesh"] = self.addMaterialToGltf("default", [.93, .84, .77, 1.0], 0.5)
self.mapTypesToMaterialIndex["Mesh"] = self.addMaterialToGltf("default", [0.8, 0.8, 0.8, 1.0], 0.5)
self.mapTypesToMaterialIndex["Wrapping"] = self.addMaterialToGltf("obstacle", [0, .9, .9, 0.7], 1.0)
self.mapTypesToMaterialIndex["Marker"] = self.addMaterialToGltf("markerMat", [1.0, .6, .8, 1.0], 1.0)
self.mapTypesToMaterialIndex["Force"] = self.addMaterialToGltf("forceMat", [0, .9, 0, 0.7], .9)
self.mapTypesToMaterialIndex["ExpMarker"] = self.addMaterialToGltf("expMarkerMat", [0, 0, 0.9, 1.0], 0.9)
self.mapTypesToMaterialIndex["IMU"] = self.addMaterialToGltf("imuMat", [.8, .8, .8, 1.0], 1.0)
self.mapTypesToMaterialIndex["Mesh.Metal"] = self.addMaterialToGltf("metal", [.9, 0.9, 0.9, 1.0], 1.0, 'Metal')
self.mapTypesToMaterialIndex["Mesh.Bone"] = self.addMaterialToGltf("bone", [.93, .84, .77, 1.0], 0.5, 'Bone')


def addMaterialToGltf(self, matName, color4, metallicFactor):
def addMaterialToGltf(self, matName, color4, metallicFactor, texture=None):
newMaterial = Material()
newMaterial.name = matName
newMaterial.alphaCutoff = ""
pbr = PbrMetallicRoughness() # Use PbrMetallicRoughness
pbr.baseColorFactor = color4 # solid red
pbr.baseColorFactor = color4
pbr.metallicFactor = metallicFactor
newMaterial.pbrMetallicRoughness = pbr
newMaterial.doubleSided = True
# if (texture!= None):
# newMaterial.normalTexture=0
self.materials.append(newMaterial)
return len(self.materials)-1

Expand All @@ -374,12 +380,18 @@ def getMaterialIndexByType(self):
if (materialGrouping is not None):
mat = self.mapTypesToMaterialIndex.get(materialGrouping)
else:
mat = self.mapTypesToMaterialIndex.get(componentType)
mat = self.getMeshMaterialId(componentType)
if (mat is not None):
return mat
else:
return 1

def getMeshMaterialId(self, compType):
if (self.currentTexture != None and self.currentTexture != ''):
return self.mapTypesToMaterialIndex.get(compType+'.'+self.currentTexture)
return self.mapTypesToMaterialIndex.get(compType)


def setNodeTransformFromDecoration(self, node: Node, mesh: Mesh, decorativeGeometry: osim.DecorativeGeometry):
bd = decorativeGeometry.getBodyId()
bdNode = self.mapMobilizedBodyIndexToNodes[bd]
Expand Down Expand Up @@ -498,21 +510,36 @@ def addGltfMeshForPolygonalMesh(self, polyMesh: osim.PolygonalMesh, mat: int):
for v in range(numVerts):
npArray_points[v] = polyMesh.getVertexPosition(v).to_numpy()

# convert arbitrary side faces into triangles as supported by gltf format
# convert arbitrary n-sided faces into triangles as supported by gltf format
numFaces = polyMesh.getNumFaces()
trisList = []
faceVertList = []
hasNormalsAtFaceVertex = polyMesh.hasNormalsAtFaces()
hastextureAtFaceVertex = polyMesh.hasTextureCoordinatesAtFaces()
normalsAccessorIndex = None
textureAccessorIndex = None

indices = []
for f in range(numFaces):
nv = polyMesh.getNumVerticesForFace(f)
vertexIdList = []
saveLength = len(faceVertList)
faceVerticesViz = []
for i in range(nv):
vertexIdList.append(polyMesh.getFaceVertex(f, i))
# Group indices into triangles
trisList.append(vertexIdList[:3])
for tri in range(3, len(vertexIdList)):
trisList.append([vertexIdList[0], vertexIdList[tri-1], vertexIdList[tri]])

byteLength = numVerts*3*4;
encoded_result = base64.b64encode(npArray_points).decode("ascii")
faceVertList.append([f, i])
faceVerticesViz.append(saveLength+i)

for tri in range(nv-2):
indices.append(faceVerticesViz[0])
indices.append(faceVerticesViz[tri+1])
indices.append(faceVerticesViz[tri+2])

npArray_verts = np.zeros((len(faceVertList), 3), dtype="float32")
for faceVert in range(len(faceVertList)):
faceVertIndex = faceVertList[faceVert]
vertexIndex = polyMesh.getFaceVertex(faceVertIndex[0], faceVertIndex[1])
npArray_verts[faceVert] = polyMesh.getVertexPosition(vertexIndex).to_numpy()

byteLength = len(faceVertList)*3*4;
encoded_result = base64.b64encode(npArray_verts).decode("ascii")
buffer = Buffer()
buffer.byteLength = byteLength;
buffer.uri = f"data:application/octet-stream;base64,{encoded_result}";
Expand All @@ -531,7 +558,7 @@ def addGltfMeshForPolygonalMesh(self, polyMesh: osim.PolygonalMesh, mat: int):
pointAccessor.byteOffset = 0
pointAccessor.type = VEC3
pointAccessor.componentType = FLOAT
pointAccessor.count = numVerts
pointAccessor.count = len(faceVertList)

min_pt = npArray_points.min(axis=0)
max_pt = npArray_points.max(axis=0)
Expand All @@ -541,20 +568,62 @@ def addGltfMeshForPolygonalMesh(self, polyMesh: osim.PolygonalMesh, mat: int):
pointAccessorIndex = len(self.accessors)-1

# Now the normals
# normalsFilter = vtk.vtkPolyDataNormals();
# normalsFilter.SetInputData(triPolys)
# normalsFilter.ComputePointNormalsOn()
# normalsFilter.Update()
# normalsData = normalsFilter.GetOutput().GetPointData().GetNormals();
# self.writeBufferAndView(normalsData, ARRAY_BUFFER)
# normalsAccessor = Accessor()
# normalsAccessor.bufferView= len(self.bufferViews)-1
# normalsAccessor.byteOffset = 0
# normalsAccessor.type = VEC3
# normalsAccessor.componentType = FLOAT
# normalsAccessor.count = pointData.GetNumberOfTuples()
# self.accessors.append(normalsAccessor)
# normalsAccessorIndex = len(self.accessors)-1
if (hasNormalsAtFaceVertex):
byteLength = len(faceVertList)*3*4;
npArray_normals = np.zeros((len(faceVertList), 3), dtype="float32")
for faceVert in range(len(faceVertList)):
faceVertIndex = faceVertList[faceVert]
npArray_normals[faceVert] = polyMesh.getVertexNormal(faceVertIndex[0], faceVertIndex[1]).to_numpy()

encoded_result = base64.b64encode(npArray_normals).decode("ascii")
buffer = Buffer()
buffer.byteLength = byteLength;
buffer.uri = f"data:application/octet-stream;base64,{encoded_result}";
self.buffers.append(buffer);

bufferView = BufferView()
bufferView.buffer = len(self.buffers)-1
bufferView.byteOffset = 0
bufferView.byteLength = byteLength
bufferView.target = ARRAY_BUFFER
self.bufferViews.append(bufferView);
normalsAccessor = Accessor()
normalsAccessor.bufferView= len(self.bufferViews)-1
normalsAccessor.byteOffset = 0
normalsAccessor.type = VEC3
normalsAccessor.componentType = FLOAT
normalsAccessor.count = len(faceVertList)
self.accessors.append(normalsAccessor)
normalsAccessorIndex = len(self.accessors)-1

if (hastextureAtFaceVertex):
byteLength = len(faceVertList)*2*4;
npArray_textureCoords = np.zeros((len(faceVertList), 2), dtype="float32")
for faceVert in range(len(faceVertList)):
faceVertIndex = faceVertList[faceVert]
uvVec2 = polyMesh.getVertexTextureCoordinate(faceVertIndex[0], faceVertIndex[1])
npArray_textureCoords[faceVert] = np.array([uvVec2.get(0), uvVec2.get(1)])

encoded_result = base64.b64encode(npArray_textureCoords).decode("ascii")
buffer = Buffer()
buffer.byteLength = byteLength;
buffer.uri = f"data:application/octet-stream;base64,{encoded_result}";
self.buffers.append(buffer);

bufferView = BufferView()
bufferView.buffer = len(self.buffers)-1
bufferView.byteOffset = 0
bufferView.byteLength = byteLength
bufferView.target = ARRAY_BUFFER
self.bufferViews.append(bufferView);
textureAccessor = Accessor()
textureAccessor.bufferView= len(self.bufferViews)-1
textureAccessor.byteOffset = 0
textureAccessor.type = VEC2
textureAccessor.componentType = FLOAT
textureAccessor.count = len(faceVertList)
self.accessors.append(textureAccessor)
textureAccessorIndex = len(self.accessors)-1

# now vertices
primitive = Primitive()
Expand All @@ -564,8 +633,8 @@ def addGltfMeshForPolygonalMesh(self, polyMesh: osim.PolygonalMesh, mat: int):
else:
primitive.material = mat

byteLength = len(trisList)*3*4;
npArray_indices = np.array(trisList, dtype=np.uint32)
npArray_indices = np.array(indices, dtype=np.uint32)
byteLength = len(indices)*4;
encoded_result = base64.b64encode(npArray_indices).decode("ascii")
buffer = Buffer()
buffer.byteLength = byteLength;
Expand All @@ -583,35 +652,19 @@ def addGltfMeshForPolygonalMesh(self, polyMesh: osim.PolygonalMesh, mat: int):
indexAccessor.byteOffset = 0
indexAccessor.type = SCALAR
indexAccessor.componentType = UNSIGNED_INT
indexAccessor.count = len(trisList)*3;
primitive.indices = len(self.accessors)
indexAccessor.count = len(npArray_indices);
self.accessors.append(indexAccessor);
primitive.indices = len(self.accessors) - 1
primitive.attributes.POSITION= pointAccessorIndex
# primitive.attributes.NORMAL = normalsAccessorIndex
if (normalsAccessorIndex!=None):
primitive.attributes.NORMAL = normalsAccessorIndex
if (textureAccessorIndex!=None):
primitive.attributes.TEXCOORD_0 = textureAccessorIndex

newMesh = Mesh()
newMesh.primitives.append(primitive)
return newMesh;

# def writeBufferAndView(self, inData: vtk.vtkDataArray, bufferViewTarget: int):
# nt = inData.GetNumberOfTuples()
# nc = inData.GetNumberOfComponents()
# ne = inData.GetElementComponentSize()
# npArray_points = np.array(inData)
# count = nt * nc;
# byteLength = ne * count;
# encoded_result = base64.b64encode(npArray_points).decode("ascii")
# buffer = Buffer()
# buffer.byteLength = byteLength;
# buffer.uri = f"data:application/octet-stream;base64,{encoded_result}";
# self.buffers.append(buffer);

# bufferView = BufferView()
# bufferView.buffer = len(self.buffers)-1
# bufferView.byteOffset = 0
# bufferView.byteLength = byteLength
# bufferView.target = bufferViewTarget
# self.bufferViews.append(bufferView);

def createGLTFLineStrip(self, point0, point1):
cylMesh = osim.PolygonalMesh().createCylinderMesh(osim.UnitVec3(0., 1., 0.), .005, 0.5)
cylMesh.transformMesh(osim.Transform(osim.Vec3(0., 0.5, 0.0)))
Expand Down
Loading