-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_pbr_material_with_textures.html
145 lines (122 loc) · 7.03 KB
/
create_pbr_material_with_textures.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html>
<head>
<meta property="og:description" content="A literate program in the rhipy repository." />
<meta name="description" content="A literate program in the rhipy repository." />
<meta name="author" content="Nathan 'jesterKing' Letwory">
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<h1>Creating a physically based material with textures</h1>
<p>TLDR; See
<a href="https://github.com/jesterKing/rhipy/blob/master/create_pbr_material_with_textures.py">here</a>
for the final script.</p>
<p>For this tool we'll be using <code>NewContentFromTypeId</code> to create the render content
we want. The GUIDs for the contents we need are provided by the <code>ContentUuids</code>
class in the <code>Rhino.Render</code> namespace.</p>
<p>We want to create a bitmap texture and a physically based material. We'll take
the GUIDs for these in several variables.</p>
<div class="codefragment">
<div class="fragmentname"><<used guids>>= </div>
<div class="code">
<pre><code>bitmap_texture_type_guid = rr.ContentUuids.BitmapTextureType
pbr_material_type_guid = rr.ContentUuids.PhysicallyBasedMaterialType
</code></pre>
</div>
</div><p>Creation of the texture is done with <code>NewContentFromTypeId</code>, giving it
<code>bitmap_texture_type_guid</code>. And since we are creating a bitmap texture in
<code>bmtex</code> we'll set its <code>Filename</code> property to the path of a file we have on disk.</p>
<div class="codefragment">
<div class="fragmentname"><<create bitmap texture>>= </div>
<div class="code">
<pre><code>bmtex = rr.RenderContentType.NewContentFromTypeId(bitmap_texture_type_guid)
bmtex.Filename = <span class="hljs-string">"C:\\Users\\Nathan\\Pictures\\uvtester.png"</span>
simtex = bmtex.SimulatedTexture(rr.RenderTexture.TextureGeneration.Allow)
</code></pre>
</div>
</div><p>You see from <code><<create bitmap texture>></code> that we also retrieved the simulated
texture <code>simtex</code> for our bitmap texture <code>bmtex</code>. We will need this later when
we get to assign the texture to the base color slot of the PBR material.</p>
<p>Now that we have our texture set up we can create our PBR material <code>pbr_rm</code>.
Again we use <code>NewContentFromTypeId</code>, this time passing it
<code>pbr_material_type_guid</code>.</p>
<p>It is important to pay attention here, since using a PBR material is not as
straightforward as it could be. The actual PBR API is found through the type
<code>Rhino.DocObjects.PhysicallyBasedMaterial</code>. We can get to this from our original
<code>pbr_rm</code>, but it requires two hoops to jump through: create the simulated
material <code>sim</code>, and from that query the <code>PhysicallyBasedMaterial</code> through the
<code>PhysicallyBased</code> property of the simulated material.</p>
<p>This gives us the <code>Rhino.DocObjects.PhysicallyBasedMaterial</code> that has the API for
accessing the PBR properties and setting the textures.</p>
<div class="codefragment">
<div class="fragmentname"><<create pbr material and jump through hoops>>= </div>
<div class="code">
<pre><code>
pbr_rm = rr.RenderContentType.NewContentFromTypeId(pbr_material_type_guid)
sim = pbr_rm.SimulatedMaterial(rr.RenderTexture.TextureGeneration.Allow)
pbr = sim.PhysicallyBased
</code></pre>
</div>
</div><p>We have now in <code>pbr</code> an instance of <code>Rhino.DocObjects.PhysicallyBasedMaterial</code>
through which we can set the properties we want to set. In this example we'll
make the material a glass-like material. <code>Opacity</code> we set to 0.0 and
<code>OpacityIOR</code> will be 1.52. The <code>BaseColor</code> we set now to white, but that doesn't
matter much, because we also set the texture.</p>
<p>Note that the texture we pass on is a <code>Rhino.DocObjects.Texture</code>, not a
<code>Rhino.Render.SimulatedTexture</code>. Luckily we can get the correct type by using
the <code>Texture()</code> method on the <code>SimulatedTexture</code> <code>simtex</code>. The Texture we set to
the <code>PBR_BaseColor</code> slot, which we find through <code>Rhino.DocObjects.TextureType</code>.</p>
<div class="codefragment">
<div class="fragmentname"><<set PBR properties and texture>>= </div>
<div class="code">
<pre><code>
pbr.Opacity = <span class="hljs-number">0.0</span>
pbr.OpacityIOR = <span class="hljs-number">1.52</span>
pbr.BaseColor = rh.Display.Color4f.White
pbr.SetTexture(simtex.Texture(), rd.TextureType.PBR_BaseColor)
</code></pre>
</div>
</div><p>There is one more step left: add the render content to the document. We want to
use the <code>RenderMaterials</code> table on the <code>RhinoDoc</code>, because this ensures we can
add a material without having to directly assign it to an object. Yet it shows
up in the Materials panel of Rhino.</p>
<p>We have now <code>pbr</code> that is a <code>Rhino.DocObjects.PhysicallyBasedMaterial</code>, yet we
need an instance of <code>Rhino.Render.RenderMaterial</code>. There is the <code>FromMaterial</code>
function on <code> Rhino.Render.RenderMaterial</code>, but that takes a
<code>Rhino.DocObjects.Material</code>. We are in luck, since the <code>PhysicallyBasedMaterial</code>
we have provides us with a <code>Material</code> property that gives us that
<code>Rhino.DocObjects.Material</code> instance we're looking for. We passed that into the
<code>FromMaterial</code> function, along with the document we're working with.</p>
<p>Now we can also set a name, and finally add it to the document <code>RenderMaterials</code>
table.</p>
<div class="codefragment">
<div class="fragmentname"><<convert into render material>>= </div>
<div class="code">
<pre><code>
new_pbr = rr.RenderMaterial.FromMaterial(pbr.Material, sc.doc)
new_pbr.Name = <span class="hljs-string">"My Own PBR Glass"</span>
sc.doc.RenderMaterials.Add(new_pbr)
</code></pre>
</div>
</div><p>In the end we have the entire script that creates us a PBR material with a
texture in the base color slot.</p>
<div class="codefragment">
<div class="fragmentname"><<create pbr with textures.*>>= ./create_pbr_material_with_textures.py $</div>
<div class="code">
<pre><code><span class="hljs-keyword">import</span> Rhino <span class="hljs-keyword">as</span> rh
<span class="hljs-keyword">import</span> Rhino.Render <span class="hljs-keyword">as</span> rr
<span class="hljs-keyword">import</span> Rhino.DocObjects <span class="hljs-keyword">as</span> rd
<span class="hljs-keyword">import</span> scriptcontext <span class="hljs-keyword">as</span> sc
<span class="literate-tag-name"><<used guids>></span>
<span class="literate-tag-name"><<create bitmap texture>></span>
<span class="literate-tag-name"><<create pbr material and jump through hoops>></span>
<span class="literate-tag-name"><<set PBR properties and texture>></span>
<span class="literate-tag-name"><<convert into render material>></span>
</code></pre>
</div>
</div>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
</script>
</body>
</html>