6
$\begingroup$
RegionPlot3D[
x^2 + y^2 + z^2 <= 1, {x, -1, 0.75}, {y, -1, 0.75}, {z, -1, 1}, 
ImageSize -> 400, Boxed -> True, Axes -> True, 
Lighting -> {{"Directional", Gray, ImageScaled[{2, 0, 2}]}}]  

I got output as below:
enter image description here

How to apply different color to each surface as below? enter image description here

$\endgroup$
  • $\begingroup$ That's not that easy: The polygons of the indicated surfaces are stored in a common GraphicsGroup within within the generated Graphics3D object. At least to me, it is not clear how to seperate these polygons in order to assign different Directives to them... $\endgroup$ – Henrik Schumacher Mar 17 '18 at 16:17
6
$\begingroup$
mesh = {Range[-1, .75, 1.75/15], Range[-1, .75, 1.75/15], Range[-1, 1, 2/15]};
pp3d = ParametricPlot3D[{{x, .75, z}, {.75, x, z}}, {x, -1, 0.75}, {z, -1, 1}, 
  RegionFunction -> (#^2 + #2^2 + #3^2 <= 1 &), PlotStyle -> {Glow @ Red, Glow @ Blue},
  MeshFunctions -> {# &, #2 &, #3 &}, Mesh -> mesh];
cp3d = ContourPlot3D[x^2 + (y)^2 + z^2 == 1, {x, -1, 0.75}, {y, -1, 0.75}, {z, -1, 1}, 
  ImageSize -> 400, Boxed -> True, Axes -> True, 
  MeshFunctions -> {# &, #2 &, #3 &}, Mesh -> mesh,
  Lighting -> {{"Directional", Gray, ImageScaled[{2, 0, 2}]}}]
Show[cp3d, pp3d]

enter image description here

Update: an alternative way to post-process RegionPlot3D output:

rp3d = RegionPlot3D[x^2 + y^2 + z^2 <= 1, {x, -1, 0.75}, {y, -1, 0.75}, {z, -1, 1}, 
  ImageSize -> 400, Boxed -> True, Axes -> True, 
  Lighting -> {{"Directional", Gray, ImageScaled[{2, 0, 2}]}}]; 

Normal[rp3d] /. {p : Polygon[{{a_, _, _} ..}, ___] :> {Glow @ Blue, p},
  p : Polygon[{{_, b_, _} ..}, ___] :> {Glow @ Red, p}}

enter image description here

Update 2: if you don't mind blending of colors you can also use Lighting option settings

lighting = {{"Directional", Gray, ImageScaled[{2, 0, 2}]},
  {"Spot", Blue, {1.5, 0, 0}, Pi/2}, {"Spot", Red, {0, -.75, 0}, Pi}}; 
RegionPlot3D[x^2 + (y)^2 + z^2 <= 1, {x, -1, 0.75}, {y, -1, 0.75}, {z, -1, 1}, 
 ImageSize -> 400, Boxed -> True, Axes -> True, Lighting -> lighting]

enter image description here

$\endgroup$
5
$\begingroup$

Replace the polygons of the clipped surface according to their x or y coordinates. The tricky business is that the polygons of the two clipped faces are mixed together in two groups, as @Henrik observed, so I reconstructed an equivalent polygon for each face using ConvexHullMesh. This necessitates projecting the points on each face to 2D and then lifting the convex hull back up to 3D.

plot = RegionPlot3D[
  x^2 + y^2 + z^2 <= 1, {x, -1, 0.75}, {y, -1, 0.75}, {z, -1, 1}, 
  ImageSize -> 400, Boxed -> True, 
  Axes -> True,(*Lighting->{{"Ambient", White}},*)
  Lighting -> {{"Directional", Gray, ImageScaled[{2, 0, 2}]}}, 
  AxesLabel -> Automatic]

With[{coords = Cases[plot, GraphicsComplex[c_, __] :> c, Infinity]},
 plot /. p_Polygon /; 
    coords[[1, p[[1, 2, 2]], 1]] == 0.75 || 
     coords[[1, p[[1, 2, 2]], 2]] == 0.75 :>
   {Red, Cases[
     Cases[plot, {_, 0.75, _}, Infinity][[All, {3, 1}]] // 
        ConvexHullMesh // Show // Normal, 
     Polygon[ch_] :> 
      Polygon[RotateLeft[PadRight[ch, {Automatic, 3}, 0.75], {0, 1}]],
      Infinity],
    Blue, 
    Cases[Cases[plot, {0.75, _, _}, Infinity][[All, {2, 3}]] // 
        ConvexHullMesh // Show // Normal, 
     Polygon[ch_] :> Polygon[PadLeft[ch, {Automatic, 3}, 0.75]], 
     Infinity]}
 ]

Mathematica graphics

The odd lighting makes the blue color almost black, except at certain angles. This is as easy as it is because the desired surfaces are exactly on the plot boundaries (and because the faces are convex).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.