Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/opaque_rendering.py
928 views
1
import numpy as np
2
3
import yt
4
5
ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")
6
7
# We start by building a default volume rendering scene
8
9
im, sc = yt.volume_render(ds, field=("gas", "density"), fname="v0.png", sigma_clip=6.0)
10
11
sc.camera.set_width(ds.arr(0.1, "code_length"))
12
tf = sc.get_source().transfer_function
13
tf.clear()
14
tf.add_layers(
15
4, 0.01, col_bounds=[-27.5, -25.5], alpha=np.logspace(-3, 0, 4), colormap="RdBu_r"
16
)
17
sc.save("v1.png", sigma_clip=6.0)
18
19
# In this case, the default alphas used (np.logspace(-3,0,Nbins)) does not
20
# accentuate the outer regions of the galaxy. Let's start by bringing up the
21
# alpha values for each contour to go between 0.1 and 1.0
22
23
tf = sc.get_source().transfer_function
24
tf.clear()
25
tf.add_layers(
26
4, 0.01, col_bounds=[-27.5, -25.5], alpha=np.logspace(0, 0, 4), colormap="RdBu_r"
27
)
28
sc.save("v2.png", sigma_clip=6.0)
29
30
# Now let's set the grey_opacity to True. This should make the inner portions
31
# start to be obscured
32
33
tf.grey_opacity = True
34
sc.save("v3.png", sigma_clip=6.0)
35
36
# That looks pretty good, but let's start bumping up the opacity.
37
38
tf.clear()
39
tf.add_layers(
40
4,
41
0.01,
42
col_bounds=[-27.5, -25.5],
43
alpha=10.0 * np.ones(4, dtype="float64"),
44
colormap="RdBu_r",
45
)
46
sc.save("v4.png", sigma_clip=6.0)
47
48
# Let's bump up again to see if we can obscure the inner contour.
49
50
tf.clear()
51
tf.add_layers(
52
4,
53
0.01,
54
col_bounds=[-27.5, -25.5],
55
alpha=30.0 * np.ones(4, dtype="float64"),
56
colormap="RdBu_r",
57
)
58
sc.save("v5.png", sigma_clip=6.0)
59
60
# Now we are losing sight of everything. Let's see if we can obscure the next
61
# layer
62
63
tf.clear()
64
tf.add_layers(
65
4,
66
0.01,
67
col_bounds=[-27.5, -25.5],
68
alpha=100.0 * np.ones(4, dtype="float64"),
69
colormap="RdBu_r",
70
)
71
sc.save("v6.png", sigma_clip=6.0)
72
73
# That is very opaque! Now lets go back and see what it would look like with
74
# grey_opacity = False
75
76
tf.grey_opacity = False
77
sc.save("v7.png", sigma_clip=6.0)
78
79
# That looks pretty different, but the main thing is that you can see that the
80
# inner contours are somewhat visible again.
81
82