Mesh

Gói: aspose.threed.entities (aspose-3d-foss 26.1.0)

Mesh lưu trữ hình học đa giác dưới dạng danh sách các điểm điều khiển (vị trí đỉnh) và danh sách các mặt đa giác. Mỗi mặt đa giác là một danh sách các chỉ số bắt đầu từ 0 vào mảng các điểm điều khiển. Các mặt có thể là tam giác, tứ giác hoặc các đa giác có số đỉnh cao hơn. Dữ liệu bổ sung cho mỗi đỉnh; các vector pháp tuyến, tọa độ UV, màu đỉnh; được đính kèm dưới dạng VertexElement lớp.

class Mesh(Geometry):

Methods

A3DObjectSceneObjectEntityGeometryMesh


Methods

Xây dựng một lưới tam giác đơn từ đầu:

from aspose.threed import Scene
from aspose.threed.entities import Mesh
from aspose.threed.utilities import Vector4

# Create the mesh and add three vertex positions
# IMPORTANT: control_points returns a copy of the internal list.
# Append to _control_points directly — appending to control_points discards the vertex.
# This is a known library limitation; a public mutation API does not yet exist.
mesh = Mesh()
mesh._control_points.append(Vector4(0.0, 0.0, 0.0, 1.0))   # vertex 0
mesh._control_points.append(Vector4(1.0, 0.0, 0.0, 1.0))   # vertex 1
mesh._control_points.append(Vector4(0.5, 1.0, 0.0, 1.0))   # vertex 2

# Define one triangle face using vertex indices
mesh.create_polygon(0, 1, 2)

# Attach to a scene and save
scene = Scene()
scene.root_node.create_child_node("triangle", mesh)
scene.save("triangle.stl")

Xây dựng một lưới tứ giác và chia thành tam giác trước khi xuất:

from aspose.threed import Scene
from aspose.threed.entities import Mesh
from aspose.threed.utilities import Vector4

mesh = Mesh()
# Four corners of a unit square in the XZ plane
# Use _control_points to mutate the backing list (control_points returns a copy)
for x, z in [(0, 0), (1, 0), (1, 1), (0, 1)]:
    mesh._control_points.append(Vector4(float(x), 0.0, float(z), 1.0))

# One quad face
mesh.create_polygon(0, 1, 2, 3)
print(f"Polygons before triangulate: {mesh.polygon_count}")  # 1

triangulated = mesh.triangulate()
print(f"Polygons after triangulate: {triangulated.polygon_count}")  # 2

scene = Scene()
scene.root_node.create_child_node("quad", triangulated)
scene.save("quad.glb")

Methods

control_points, polygon_count, polygons, edges, và vertex_elements đều là các thuộc tính; truy cập chúng mà không cần dấu ngoặc đơn.

MethodsMethodsMethodsMethods
control_pointslist[Vector4]đọcMảng vị trí đỉnh. Mỗi mục là một Vector4(x, y, z, w) nơi w1.0 cho dữ liệu vị trí. Trả về một bản sao — không thêm vào danh sách đã trả về. Sử dụng mesh._control_points.append(v) để thêm các đỉnh (giới hạn đã biết; API thay đổi công khai chưa khả dụng).
polygon_countintđọcSố lượng mặt đa giác được định nghĩa trên lưới này.
polygonslist[list[int]]đọcTất cả các định nghĩa mặt dưới dạng danh sách các danh sách chỉ mục. Mỗi danh sách bên trong chứa các chỉ mục đỉnh (vào control_points) cho một mặt.
edgeslist[int]đọcDữ liệu chỉ mục cạnh thô. Chủ yếu dùng cho nội bộ và các truy vấn tôpô tiên tiến.
vertex_elementslist[VertexElement]đọcTất cả các lớp phần tử đỉnh hiện đang gắn vào lưới này (normals, UVs, colours, v.v.).
visibleboolđọc/ghiMethods False, lưới sẽ bị ẩn trong các trình xem tôn trọng tính hiển thị.
cast_shadowsboolđọc/ghiLiệu lưới này có tạo bóng trong các trình render hỗ trợ bản đồ bóng hay không.
receive_shadowsboolđọc/ghiLiệu lưới này có nhận bóng từ các hình học tạo bóng khác hay không.

Methods

create_polygon(*indices)

Xác định một mặt đa giác mới bằng cách cung cấp các chỉ mục đỉnh theo thứ tự. Các chỉ mục tham chiếu tới vị trí trong control_points. Chấp nhận ba hoặc nhiều chỉ mục hơn cho tam giác, tứ giác và n-gon.

MethodsMethodsMethods
*indicesintCác đối số chỉ mục đỉnh theo thứ tự quấn (thông thường ngược chiều kim đồng hồ khi nhìn từ bên ngoài).

Trả về: None

mesh = Mesh()
# ... populate control_points ...
mesh.create_polygon(0, 1, 2)       # triangle
mesh.create_polygon(0, 1, 2, 3)    # quad
print(mesh.polygon_count)          # 2

triangulate()

Trả về một mới Mesh nơi mỗi đa giác đã được chia thành các tam giác bằng cách sử dụng fan triangulation. Lưới gốc không bị thay đổi. Hữu ích trước khi xuất sang các định dạng yêu cầu chỉ có hình học tam giác (như STL hoặc một số pipeline glTF).

Trả về: Mesh; một lưới mới chỉ chứa các tam giác.

from aspose.threed.entities import Mesh
from aspose.threed.utilities import Vector4

mesh = Mesh()
# Use _control_points to mutate the backing list (control_points returns a copy)
for v in [(0,0,0), (1,0,0), (1,1,0), (0,1,0)]:
    mesh._control_points.append(Vector4(*v, 1.0))
mesh.create_polygon(0, 1, 2, 3)   # one quad

tri_mesh = mesh.triangulate()
print(tri_mesh.polygon_count)      # 2

to_mesh()

Trả về đối tượng này Mesh như một Mesh đối tượng. Đối với Mesh đối tượng, đây là một phép toán đồng nhất (trả về self). Được định nghĩa trên Geometry lớp cơ sở để cung cấp giao diện chuyển đổi đồng nhất khi làm việc với Geometry các tham chiếu.

Trả về: Mesh

from aspose.threed.entities import Geometry

def ensure_mesh(geom: Geometry):
    return geom.to_mesh()

create_element(element_type, mapping_mode, reference_mode)

Thêm một VertexElement lớp có kiểu được chỉ định vào lưới. Sử dụng để gắn normals, tangents, binormals, vertex colours và smoothing groups.

MethodsMethodsMethods
element_typeVertexElementTypeKiểu dữ liệu mà lớp này chứa (ví dụ,., VertexElementType.NORMAL).
mapping_modeMappingModeCách dữ liệu được ánh xạ tới hình học: CONTROL_POINT, POLYGON_VERTEX, POLYGON, v.v.
reference_modeReferenceModeCách chỉ mục được sử dụng: DIRECT hoặc INDEX_TO_DIRECT.

Trả về: VertexElement

from aspose.threed.entities import Mesh, VertexElementType, MappingMode, ReferenceMode
from aspose.threed.utilities import Vector4

mesh = Mesh()
# Use _control_points to mutate the backing list (control_points returns a copy)
mesh._control_points.append(Vector4(0, 0, 0, 1))
mesh._control_points.append(Vector4(1, 0, 0, 1))
mesh._control_points.append(Vector4(0.5, 1, 0, 1))
mesh.create_polygon(0, 1, 2)

normal_element = mesh.create_element(
    VertexElementType.NORMAL,
    MappingMode.CONTROL_POINT,
    ReferenceMode.DIRECT,
)

create_element_uv(uv_mapping, mapping_mode, reference_mode)

Thêm một lớp tọa độ UV vào lưới. Đây là phương pháp ưu tiên để gắn dữ liệu texture coordinate.

MethodsMethodsMethods
uv_mappingTextureMappingMục đích của kênh UV: DIFFUSE, SPECULAR, NORMAL, AMBIENT, v.v.
mapping_modeMappingModeCách UVs được ánh xạ tới các phần tử hình học.
reference_modeReferenceModeChế độ chỉ mục: DIRECT hoặc INDEX_TO_DIRECT.

Trả về: VertexElementUV

from aspose.threed.entities import Mesh, TextureMapping, MappingMode, ReferenceMode

mesh = Mesh()
# ... define control_points and polygons ...
uv_element = mesh.create_element_uv(
    TextureMapping.DIFFUSE,
    MappingMode.POLYGON_VERTEX,
    ReferenceMode.INDEX_TO_DIRECT,
)

Xem thêm

 Tiếng Việt