2012-12-05 74 views
0

我正在嘗試爲Blender 2.6x編寫一個簡單的JSON導出器,因爲我能找到的唯一一個(http://code.google.com/p/blender-machete/)不適用於2.6。我從攪拌器獲取頂點,法線和指數沒有任何問題,但儘可能地嘗試,我似乎無法弄清楚爲什麼紋理座標出錯了。紋理似乎傾斜對角地穿過一個簡單的立方體的臉,並拉伸......非常醜陋和錯誤。我一直在網上尋找一些官方出口商的來源,但我仍然無法弄清楚,所以我希望有人能給我一些提示或解決方案。Blender 2.6 JSON導出器,紋理座標問題

的一塊我使用訪問紋理座標代碼是這樣的:

# add texture coordinates to scene_data structure 
    m = bpy.context.active_object.to_mesh(bpy.context.scene, True, 'PREVIEW') 
    for j in range(len(m.tessfaces)): 
     if len(m.tessface_uv_textures) > 0: 
      scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv1.x) 
      scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv1.y) 
      scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv2.x) 
      scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv2.y) 
      scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv3.x) 
      scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv3.y) 

這是給我的紋理座標的名單,但不知何故,我做錯了,不正確的,因爲正如我上面解釋的那樣,紋理的外觀。

我不知道自己還能做些什麼,但顯示的代碼,因爲我想一切辦法,我能想到的周圍改變,所以這裏的地方上面的代碼片段的功能是:

def get_json(objects, scene): 
    """ Currently only supports one scene. 
     Exports with -Z forward, Y up. """ 

    scene_data = [] 
    mesh_number = -1 

    # iterate over each mesh 
    for i in range(len(bpy.data.objects)): 
     if bpy.data.objects[i].type == 'MESH': 

      mesh_number += 1 

      bpy.ops.object.mode_set(mode='OBJECT') 

      # convert all the mesh's faces to triangles 
      bpy.data.objects[i].select = True 
      bpy.context.scene.objects.active = bpy.data.objects[i] 

      bpy.ops.object.mode_set(mode='EDIT') 

      bpy.ops.mesh.select_all(action='SELECT') 
      bpy.ops.mesh.quads_convert_to_tris() 
      bpy.context.scene.update() 

      bpy.ops.object.mode_set(mode='OBJECT') 

      bpy.data.objects[i].select = False 

      # add data to scene_data structure 
      scene_data.append({ 
       "name"   : bpy.data.objects[i].name, 
       "vertices"  : [], 
       "indices"  : [], 
       "normals"  : [], 
       "tex_coords" : [] 
      }) 

      # iterate over all the vertices in the mesh 
      for j in range(len(bpy.data.objects[i].data.vertices)): 
       # add vertex to scene_data structure 
       scene_data[mesh_number]["vertices"].append(bpy.data.objects[i].data.vertices[j].co.x + bpy.data.objects[i].location.x) 
       scene_data[mesh_number]["vertices"].append(bpy.data.objects[i].data.vertices[j].co.z + bpy.data.objects[i].location.z) 
       scene_data[mesh_number]["vertices"].append(-(bpy.data.objects[i].data.vertices[j].co.y + bpy.data.objects[i].location.y)) 

       # add vertex normal to scene_data structure 
       scene_data[mesh_number]["normals"].append(bpy.data.objects[i].data.vertices[j].normal.x) 
       scene_data[mesh_number]["normals"].append(bpy.data.objects[i].data.vertices[j].normal.z) 
       scene_data[mesh_number]["normals"].append(-(bpy.data.objects[i].data.vertices[j].normal.y)) 

      # iterate over each face in the mesh 
      for j in range(len(bpy.data.objects[i].data.polygons)): 
       verts_in_face = bpy.data.objects[i].data.polygons[j].vertices[:] 

       # iterate over each vertex in the face 
       for k in range(len(verts_in_face)): 

        # twiddle index for -Z forward, Y up 
        index = k 
        if index == 1: index = 2 
        elif index == 2: index = 1 

        # twiddle index so we draw triangles counter-clockwise 
        if index == 0: index = 2 
        elif index == 2: index = 0 

        # add index to scene_data structure 
        scene_data[mesh_number]["indices"].append(verts_in_face[index]) 

      # add texture coordinates to scene_data structure 
      m = bpy.context.active_object.to_mesh(bpy.context.scene, True, 'PREVIEW') 
      for j in range(len(m.tessfaces)): 
       if len(m.tessface_uv_textures) > 0: 
        scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv1.x) 
        scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv1.y) 
        scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv2.x) 
        scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv2.y) 
        scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv3.x) 
        scene_data[mesh_number]["tex_coords"].append(m.tessface_uv_textures.active.data[j].uv3.y) 

    return json.dumps(scene_data, indent=4) 

請問有人請告訴我我做錯了什麼?我已經在這幾天沒有進展了。

回答

0

經進一步研究,我想我可能知道的問題,但我不知道這...

從這裏: http://www.gamedev.net/topic/602169-opengl-drawing-cube-using-gldrawelements-and-gltexcoordpointer/page_p_4811454#entry4811454

這是你當一個經典的例子不能使用共享頂點。只要引入位置以外的頂點屬性,立方體就需要有24個頂點,而不是8.立方體的頂點不是共享頂點,因爲它們沒有相同的紋理座標。

例如,前兩個三角形由索引[0,1, 2,0,0,2,3]組成,並且如果引用頂點和紋理座標 數組,則該面很好。第二個兩個三角形由 索引[0,4,5,0,5,1]組成,並且儘管頂點數組被標記爲 正確以製作立方體的第二個面,但生成的紋理 座標完全被破壞。

我說得對,這是我的問題,還是我的方式?

編輯: 通過使用更多的頂點位置幾乎得到紋理。現在唯一的問題是一個簡單的立方體的一個面將被扭曲並且對角到正確的位置。所有其他面孔看起來不錯。

這裏是一個幾乎可以工作的功能:

def get_json(objects, scene): 
    """ Currently only supports one scene. 
     Exports with -Z forward, Y up. """ 

    object_number = -1 
    scene_data = [] 

    # for every object in the scene 
    for object in bpy.context.scene.objects: 

     # if the object is a mesh  
     if object.type == 'MESH': 

      object_number += 1 

      # convert all the mesh's faces to triangles 
      bpy.ops.object.mode_set(mode='OBJECT') 
      object.select = True 
      bpy.context.scene.objects.active = object 

      # triangulate using new Blender 2.65 Triangulate modifier 
      bpy.ops.object.modifier_add(type='TRIANGULATE') 
      object.modifiers["Triangulate"].use_beauty = False 
      bpy.ops.object.modifier_apply(apply_as="DATA", modifier="Triangulate") 

      bpy.ops.object.mode_set(mode='OBJECT') 

      object.select = False 

      # add data to scene_data structure 
      scene_data.append({ 
       "name"   : object.name, 
       "vertices"  : [], 
       "indices"  : [], 
       "normals"  : [], 
       "tex_coords" : [] 
      }) 

      vertex_number = -1 

      # for each face in the object 
      for face in object.data.polygons: 
       vertices_in_face = face.vertices[:] 

       # for each vertex in the face 
       for vertex in vertices_in_face: 

        vertex_number += 1 

        # store vertices in scene_data structure 
        scene_data[object_number]["vertices"].append(object.data.vertices[vertex].co.x + object.location.x) 
        scene_data[object_number]["vertices"].append(object.data.vertices[vertex].co.z + object.location.z) 
        scene_data[object_number]["vertices"].append(-(object.data.vertices[vertex].co.y + object.location.y)) 

        # store normals in scene_data structure 
        scene_data[object_number]["normals"].append(object.data.vertices[vertex].normal.x) 
        scene_data[object_number]["normals"].append(object.data.vertices[vertex].normal.z) 
        scene_data[object_number]["normals"].append(-(object.data.vertices[vertex].normal.y)) 

        # store indices in scene_data structure 
        scene_data[object_number]["indices"].append(vertex_number) 

      # texture coordinates 
      # bug: for a simple cube, one face's texture is warped 
      mesh = object.to_mesh(bpy.context.scene, True, 'PREVIEW') 
      if len(mesh.tessface_uv_textures) > 0: 
       for data in mesh.tessface_uv_textures.active.data: 
        scene_data[object_number]["tex_coords"].append(data.uv1.x) 
        scene_data[object_number]["tex_coords"].append(data.uv1.y) 
        scene_data[object_number]["tex_coords"].append(data.uv2.x) 
        scene_data[object_number]["tex_coords"].append(data.uv2.y) 
        scene_data[object_number]["tex_coords"].append(data.uv3.x) 
        scene_data[object_number]["tex_coords"].append(data.uv3.y) 

    return json.dumps(scene_data, indent=4) 

我想知道這可能是唯一的一個面質地的原因被扭曲?我幾乎在那裏,但似乎無法找出這一個。