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

WTF am I doing wrong?? 24 hours straight... #67

Open
mikeoverbay opened this issue Sep 15, 2024 · 0 comments
Open

WTF am I doing wrong?? 24 hours straight... #67

mikeoverbay opened this issue Sep 15, 2024 · 0 comments

Comments

@mikeoverbay
Copy link

and i can not figure out why it is not writing files other than .dea format.
Every path for textures and output path have need checked over and over.
I'm missing.. what?
Any help would be awesome!

    Public Sub make_glTF()

        Dim ar() As String
        If PRIMITIVES_MODE Then
            ar = Path.GetFileNameWithoutExtension(frmMain.OpenFileDialog1.FileName).Split("~")
        Else
            ar = TANK_NAME.Split(":")
        End If
        file_name = current_tank_name
        frmMain.SaveFileDialog1.InitialDirectory = My.Settings.fbx_path
        Select Case EXPORT_TYPE
            Case 1
                frmMain.SaveFileDialog1.Filter = "glTF|*.glTF"
                frmMain.SaveFileDialog1.Title = "Save glTF.."
                frmMain.SaveFileDialog1.FileName = Path.GetFileName(ar(0)) + ".glTF"
                Exit Select
            Case 2
                frmMain.SaveFileDialog1.Filter = "FBX|*.fbx"
                frmMain.SaveFileDialog1.Title = "Save FBX.."
                frmMain.SaveFileDialog1.FileName = Path.GetFileName(ar(0)) + ".fbx"
                Exit Select
            Case 3
                frmMain.SaveFileDialog1.Filter = "OBJ|*.obj"
                frmMain.SaveFileDialog1.Title = "Save OBJ.."
                frmMain.SaveFileDialog1.FileName = Path.GetFileName(ar(0)) + ".obj"
                Exit Select
            Case 4
                frmMain.SaveFileDialog1.Filter = "Collada|*.dae"
                frmMain.SaveFileDialog1.Title = "Save Collada.."
                frmMain.SaveFileDialog1.FileName = Path.GetFileName(ar(0)) + ".dae"
                Exit Select
        End Select
        frmMain.SaveFileDialog1.AddExtension = True
        frmMain.SaveFileDialog1.RestoreDirectory = True


        Dim result = frmMain.SaveFileDialog1.ShowDialog
        Dim out_path = frmMain.SaveFileDialog1.FileName

        If Not result = DialogResult.OK Then
            Return
        End If
        My.Settings.fbx_path = out_path

        Dim name As String = Path.GetFileName(ar(0))
        Dim save_path = Path.GetDirectoryName(My.Settings.fbx_path) + "\" + name
        export_fbx_textures(False, 1) 'export all textures

        Dim scene_ As New Assimp.Scene
        scene_.RootNode = New Assimp.Node("Root")
        scene_.RootNode.Name = name


        For item = 1 To _group.Length - 1
            If item = 23 Then
                Stop
            End If
            Dim off = _group(item).startVertex_

            Dim model_name = _group(item).name.Replace("/", "\")
            model_name = model_name.Replace(":", "~")
            model_name = model_name.Replace("vehicles\", "")
            model_name = model_name.Replace("primitives_processed", "pri")
            model_name = model_name.Replace("\lod0\", "\l\")

            Dim m As New Assimp.Mesh(model_name) 'create mesh

            'create mesh primitive face index set
            For i As UInt32 = 1 To _group(item).nPrimitives_
                m.Faces.Add(New Face(New Integer() {
        _group(item).indices(i).v1 - off,
        _group(item).indices(i).v2 - off,
        _group(item).indices(i).v3 - off
    }))
            Next
            Dim normals As New List(Of Vector3D)()
            For i As UInt32 = 0 To _group(item).nVertices_ - 1
                Dim normal As New Vector3D()
                normal.X = _group(item).vertices(i).nx
                normal.Y = _group(item).vertices(i).ny
                normal.Z = _group(item).vertices(i).nz
                If _group(item).color_name IsNot Nothing AndAlso _group(item).color_name.ToLower().Contains("track") Then
                    normal.Y *= 1.0F
                End If
                normals.Add(normal)
            Next
            m.Normals.AddRange(normals)

            Dim uvs As New List(Of Vector3D)()
            For i = 0 To _group(item).nVertices_ - 1
                Dim uv As New Vector3D()
                uv.X = _group(item).vertices(i).u
                uv.Y = -_group(item).vertices(i).v
                uv.Z = 0 ' Assimp UV coordinates are 3D vectors, so set Z to 0
                uvs.Add(uv)
            Next
            m.TextureCoordinateChannels(0).AddRange(uvs)

            If _group(item).has_uv2 = 1 Then
                Dim uvs2 As New List(Of Vector3D)()
                For i As UInt32 = 0 To _group(item).nVertices_ - 1
                    Dim uv2 As New Vector3D()
                    uv2.X = _group(item).vertices(i).u2
                    uv2.Y = -_group(item).vertices(i).v2
                    uv2.Z = 0 ' Assimp UV coordinates are 3D vectors, so set Z to 0
                    uvs2.Add(uv2)
                Next
                m.TextureCoordinateChannels(1).AddRange(uvs2)
            End If

            Dim vertexColors As New List(Of Color4D)()
            For i As UInt32 = 0 To _group(item).nVertices_ - 1
                Dim color As New Color4D()
                color.R = _group(item).vertices(i).index_1
                color.G = _group(item).vertices(i).index_2
                color.B = _group(item).vertices(i).index_3
                color.A = _group(item).vertices(i).index_4
                vertexColors.Add(color)
            Next
            m.VertexColorChannels(0).AddRange(vertexColors)
            For i As UInt32 = 0 To _group(item).nVertices_ - 1
                Dim v As New Vector3D()
                v.X = _group(item).vertices(i).x
                v.Y = _group(item).vertices(i).y
                v.Z = _group(item).vertices(i).z
                m.Vertices.Add(v)
            Next

            Dim co As Vector3D
            co.X = 0.6
            co.Y = 0.6
            co.Z = 0.6
            'Some turrets dont exist but are still used for translations.
            'If the are only a matrix transform, they have no textures!
            Dim m1 As New Assimp.Material()
            Dim m2 As New Assimp.Material()

            If _group(item).color_name IsNot Nothing Then
                Dim local_path = Path.GetDirectoryName(out_path)

                Dim arr = _group(item).color_name.Split("\")
                Dim DnF = name + "\" + arr(arr.Length - 1)
                Dim tx As New Assimp.TextureSlot(local_path + "\" + DnF.Replace(".dds", ".png"), Assimp.TextureType.Diffuse, 0, Assimp.TextureMapping.FromUV, 0, 1.0F, Assimp.TextureOperation.Add, Assimp.TextureWrapMode.Wrap, Assimp.TextureWrapMode.Wrap, 0)

                ' Set up the normal texture
                arr = _group(item).normal_name.Split("\")
                DnF = name + "\" + arr(arr.Length - 1)
                Dim txn As New Assimp.TextureSlot(local_path + "\" + DnF.Replace(".dds", ".png"), Assimp.TextureType.Normals, 0, Assimp.TextureMapping.FromUV, 0, 1.0F, Assimp.TextureOperation.Add, Assimp.TextureWrapMode.Wrap, Assimp.TextureWrapMode.Wrap, 0)

                ' Set up the ambient occlusion texture
                arr = _group(item).ao_name.Split("\")
                DnF = name + "\" + arr(arr.Length - 1)
                Dim txao As New Assimp.TextureSlot(local_path + "\" + DnF.Replace(".dds", ".png"), Assimp.TextureType.Unknown, 0, Assimp.TextureMapping.FromUV, 0, 1.0F, Assimp.TextureOperation.Add, Assimp.TextureWrapMode.Wrap, Assimp.TextureWrapMode.Wrap, 0)

                ' Set up the metallic-roughness texture
                arr = _group(item).metalGMM_name.Split("\")
                DnF = name + "\" + arr(arr.Length - 1)
                Dim txmr As New Assimp.TextureSlot(local_path + "\" + DnF.Replace(".dds", ".png"), Assimp.TextureType.Unknown, 0, Assimp.TextureMapping.FromUV, 0, 1.0F, Assimp.TextureOperation.Add, Assimp.TextureWrapMode.Wrap, Assimp.TextureWrapMode.Wrap, 0)

                ' Assign textures to materials
                m1.AddMaterialTexture(tx)
                m1.AddMaterialTexture(txn)
                m1.AddMaterialTexture(txao)
                m1.AddMaterialTexture(txmr)

                m2.AddMaterialTexture(tx)
                m2.AddMaterialTexture(txn)
                m2.AddMaterialTexture(txao)
                m2.AddMaterialTexture(txmr)
            End If

            Select Case EXPORT_TYPE
                Case 2, 3, 4
                    ' LambertMaterial
                    m1.Name = "Material00" + item.ToString
                    m1.ColorDiffuse = New Assimp.Color4D(0.7F, 0.7F, 0.7F, 1.0F)
                    m1.ColorAmbient = New Assimp.Color4D(0.4F, 0.4F, 0.4F, 1.0F)
                    m1.Shininess = 0.1F
                    m1.Reflectivity = 0.05F
                Case 1
                    ' PbrMaterial
                    m2.Name = "Material00" + item.ToString
                    m2.Shininess = 0.1F
                    m2.Reflectivity = 0.05F

            End Select

            Dim base As New Assimp.Node(model_name)
            base.Name = model_name


            'scene_.RootNode.Children.Add(base)
            If _group(item).color_name IsNot Nothing Then
                If EXPORT_TYPE = 2 Then
                    m.MaterialIndex = item - 1
                    scene_.Materials.Add(m1)
                Else
                    m.MaterialIndex = scene_.Materials.Count
                    scene_.Materials.Add(m2)
                End If
            End If

            'base.Meshes.Add(scene_.Meshes.Count)
            scene_.Meshes.Add(m)


            base.MeshIndices.Add(scene_.Meshes.Count - 1)

            Dim mat As New Assimp.Matrix4x4()
            Dim tMatrix(16) As Double
            For i As UInt32 = 0 To 15
                tMatrix(i) = _object(item).matrix(i)
            Next

            mat.A1 = tMatrix(0)
            mat.A2 = tMatrix(1)
            mat.A3 = tMatrix(2)
            mat.A4 = tMatrix(3)
            mat.B1 = tMatrix(4)
            mat.B2 = tMatrix(5)
            mat.B3 = tMatrix(6)
            mat.B4 = tMatrix(7)
            mat.C1 = tMatrix(8)
            mat.C2 = tMatrix(9)
            mat.C3 = tMatrix(10)
            mat.C4 = tMatrix(11)
            mat.D1 = tMatrix(12)
            mat.D2 = tMatrix(13)
            mat.D3 = tMatrix(14)
            mat.D4 = tMatrix(15)



            'Apply the matrix to the node
            If EXPORT_TYPE = 2 Then

                Dim rs As Assimp.Quaternion = New Assimp.Quaternion()
                Dim ts As Vector3D = New Vector3D()
                Dim ss As Vector3D = New Vector3D()
                mat.Decompose(ts, rs, ss)
                rs.Normalize()

                ' Apply the transformations to the node
                Dim t_v = New Assimp.Vector3D(-ts.X, ts.Y, ts.Z)
                Dim s_v = New Assimp.Vector3D(-ss.X, ss.Y, ss.Z)
                If _group(item).color_name IsNot Nothing Then

                    If _group(item).color_name.ToLower.Contains("chassis") Then
                        s_v.Z *= -1.0
                        s_v.Y *= -1.0
                    End If
                    If _group(item).color_name.ToLower.Contains("tracks") Then
                        s_v.Z *= -1.0
                        s_v.Y *= -1.0
                    End If
                    If _group(item).color_name.ToLower.Contains("gun") Then
                        s_v.Z *= -1.0
                        s_v.Y *= -1.0
                    End If
                End If
                ' Create transformation matrices
                Dim scaleMatrix = Assimp.Matrix4x4.FromScaling(s_v)
                Dim translationMatrix = Assimp.Matrix4x4.FromTranslation(t_v)
                Dim rotationMatrix = QuaternionToMatrix(rs)
                ' Combine the transformation matrices
                base.Transform = scaleMatrix * rotationMatrix * translationMatrix



            Else
                mat.A1 = tMatrix(0) * -1.0
                If _object(item).name.ToLower.Contains("turret") Then
                    mat.A4 = tMatrix(12) * -1.0
                End If

                base.Transform = mat
            End If



            scene_.RootNode.Children.Add(base)

            Debug.WriteLine(m.Name)
        Next


        ' Export the scene to a 3D format (e.g., FBX)
        Dim context As New AssimpContext()
        Try

            Dim exporter As New AssimpContext()
            Select Case EXPORT_TYPE
                Case 1
                    context.ExportFile(scene_, out_path, "glTF2")
                Case 2
                    context.ExportFile(scene_, out_path, "fbx")
                Case 3
                    context.ExportFile(scene_, out_path, "obj")
                Case 4
                    context.ExportFile(scene_, out_path, "collada")
            End Select
        Catch ex As Exception
            ' Debugging: Print the exception message
            Debug.WriteLine("Export Error: " & ex.Message)
        End Try
    End Sub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant