你好,我想把matl​ab的三维图形转为g​lb/gltf格式的​3D文件

4 views (last 30 days)
永伦 刘
永伦 刘 on 4 Mar 2023
Answered: Varun on 17 Apr 2023
我想把matlab的三维图形转为glb/gltf格式的3D文件,我安装了matlab2glb和GL Transmission Format for MATLAB Version 1.3.0.0 by Rohan Chabukswar两个插件(函数),均未得到解决,我使用了函数提供的例子,也未能正常运行,提示路径里无法检索到函数的定义内容。
具体的代码如下:
[X,Y]=meshgrid(-8:.5:8);
R=sqrt(X.^2 + Y.^2) + eps;
Z=sin(R)./R;
m=mesh(X,Y,Z);
%Get the rectangular faces that MATLAB would use. We will use these to create edges.
[E,V]=surf2patch(X,Y,Z);
E=reshape(E(:,[1 2 2 3 3 4 4 1])',2,[])';
% Since the direction of the edge doesn't matter, we sort it so the lower-indexed vertex is first.
E=sort(E,2); %#ok<UDIM>
% This enables us to discard duplicate edges.
E=unique(E,'rows');
% Get the triangular faces. We will use this to prevent the mesh from being see-through by adding a double-sided white surface.
[F,~,~]=surf2patch(X,Y,Z,'triangles');
% The colours are based on the height, i.e., the Z value. So we first scale the Z value to be between 0 and 1.
C0=(V(:,3)-min(V(:,3)))./(max(V(:,3))-min(V(:,3)));
% Get the colourmap
cmap=colormap;
% And use interpolation to get the colour values per vertex.
C=interp1(linspace(0,1,size(cmap,1))',cmap,C0);
% Scale vertices based on data aspect ratio and plot aspect ratio to make the 3D Object look as much like the MATLAB plot as possible.
V=V./daspect.*pbaspect;
% Rotate the vertices since we are more used to Z-axis being "up" and Y-axis being "back".
V=V*[0 0 1;1 0 0;0 1 0];
% Create the GLTF object.
gltf=GLTF();
% Add a white material which will prevent the mesh from being see-through.
white_idx=gltf.addMaterial('baseColorFactor',ones(1,3),'doubleSided',true);
% Add a mesh with the vertices and faces. Use the white material.
mesh_idx=gltf.addMesh(V,'indices',F,'material',white_idx);
% Add a second primitive to the same mesh with vertices, edges, and colours. Use mode "LINES".
gltf.addPrimitiveToMesh(mesh_idx,V,'indices',E,'COLOR',C,'mode',"LINES");
% Instantiate the mesh in a node.
gltf.addNode('mesh',mesh_idx);
% Write the GLTF file.
gltf.writeGLTF("mesh.gltf");
输入后,其提示的错误为:“Method 'addMaterial' is not defined for class 'GLTF' or is removed from MATLAB's search path.”
可是路径明明是正确的:↓
把matlab的三维图形转为glb格式我一直无法解决,希望您们可以帮帮我,感谢您们!
sincerely!

Answers (1)

Varun
Varun on 17 Apr 2023
Hello 永伦,
As per my understanding, you are trying to use the GL Transmission Format for MATLAB Version 1.3.0.0 by Rohan Chabukswar, which is available on MATLAB file exchange. I tried to run the code on my end and faced the same issue as you had reported. I resolved it by creating a class folder. Since, the functions are defined in individual files, I made a class folder named @GLTF and transferred all the files of the class to it.
Note: For any specific questions pertaining to how the GLTF class works, we urge you to contact the author of the GL Transmission Format for MATLAB Version 1.3.0.0 directly, since the MATLAB File exchange is a public forum for users to share their custom-coded programs without any involvement from Mathworks.
Hope this helps!

Categories

Find more on 二维图和三维图 in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!