How to color different sides of a surface differently?
5 次查看(过去 30 天)
显示 更早的评论
Hello I've got a problem with coloring a surface using colormap.
I created a 3D patch object using isosurface and then plotted it with patch
[fvf,fvv,fvc] = isosurface(KXi,KYi,KZi,Edis,EF);
p1 = patch('Vertices', fvv, 'Faces', fvf,'FaceColor','green');
daspect([1,1,1])
view(3); axis tight
camlight
lighting gouraud
This creates a sphere like surface around the origin with some holes in it. But now I have to color the inside of the sphere (meaning the other side of the surface) differently than the outside. I've played a little bit with the lighting, but found nothing suitable. I've also tried plotting another patch a little bit inside the sphere, with a different color:
hold on
p2 = patch('Vertices', fvv*0.999, 'Faces', fvf,'FaceColor','red');
But if I make the inner sphere too small, the gap between the two becomes visible at the edges of the holes, and if I make it too large, it overlaps a little bit with the inner one.
Can anyone help me to do this in a better way? I'd really appreciate an answer to this!
Thanks!
0 个评论
回答(1 个)
Gautam
2025-2-10
Hello Astan,
To colour the inside of the sphere differently from the outside without creating gaps or overlaps, you can use face normals to determine which side of the surface is visible and color them accordingly
Here's a sample code
% Create the isosurface
[fvf, fvv, fvc] = isosurface(KXi, KYi, KZi, Edis, EF);
% Calculate face normals
faceNormals = isonormals(KXi, KYi, KZi, Edis, fvv);
% Determine which faces are oriented outward and inward
% This is a simple heuristic: assuming the origin is inside,
% faces with normals pointing away from the origin are "outside"
faceCenters = (fvv(fvf(:,1), :) + fvv(fvf(:,2), :) + fvv(fvf(:,3), :)) / 3;
directionToOrigin = -faceCenters; % Vector pointing from face center to origin
dotProduct = dot(faceNormals, directionToOrigin, 2);
% Create logical indices for outside and inside faces based on dot product
outsideFaces = dotProduct > 0;
insideFaces = ~outsideFaces;
% Create the patch object
p1 = patch('Vertices', fvv, 'Faces', fvf(outsideFaces, :), ...
'FaceColor', 'green', 'EdgeColor', 'none');
hold on;
p2 = patch('Vertices', fvv, 'Faces', fvf(insideFaces, :), ...
'FaceColor', 'red', 'EdgeColor', 'none');
% Set up the view and lighting
daspect([1, 1, 1]);
view(3);
axis tight;
camlight;
lighting gouraud;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!