cd35614150567be143725e849f1a63c15311410e
[carveJwlIkooP6JGAAIwe30JlM.git] / skaterift_blender / sr_mat.py
1 print( "sr_mat" )
2
3 def material_tex_image(v):
4 #{
5 return {
6 "Image Texture":
7 {
8 "image": F"{v}"
9 }
10 }
11 #}
12
13 cxr_graph_mapping = \
14 {
15 # Default shader setup
16 "Principled BSDF":
17 {
18 "Base Color":
19 {
20 "Image Texture":
21 {
22 "image": "tex_diffuse"
23 },
24 "Mix":
25 {
26 "A": material_tex_image("tex_diffuse"),
27 "B": material_tex_image("tex_decal")
28 },
29 },
30 "Normal":
31 {
32 "Normal Map":
33 {
34 "Color": material_tex_image("tex_normal")
35 }
36 }
37 },
38 "Emission":
39 {
40 "Color": material_tex_image("tex_diffuse")
41 }
42 }
43
44 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
45 #
46 def material_info(mat):
47 #{
48 info = {}
49
50 # Using the cxr_graph_mapping as a reference, go through the shader
51 # graph and gather all $props from it.
52 #
53 def _graph_read( node_def, node=None, depth=0 ):
54 #{
55 nonlocal mat
56 nonlocal info
57
58 # Find rootnodes
59 #
60 if node == None:
61 #{
62 _graph_read.extracted = []
63
64 done = False
65 for node_idname in node_def:
66 #{
67 for n in mat.node_tree.nodes:
68 #{
69 if n.name == node_idname:
70 #{
71 node_def = node_def[node_idname]
72 node = n
73 done = True
74 break
75 #}
76 #}
77 if done: break
78 #}
79 #}
80
81 for link in node_def:
82 #{
83 link_def = node_def[link]
84
85 if isinstance( link_def, dict ):
86 #{
87 node_link = None
88 for x in node.inputs:
89 #{
90 if isinstance( x, bpy.types.NodeSocketColor ):
91 #{
92 if link == x.name:
93 #{
94 node_link = x
95 break
96 #}
97 #}
98 #}
99
100 if node_link and node_link.is_linked:
101 #{
102 # look for definitions for the connected node type
103 #
104 from_node = node_link.links[0].from_node
105
106 node_name = from_node.name.split('.')[0]
107 if node_name in link_def:
108 #{
109 from_node_def = link_def[ node_name ]
110
111 _graph_read( from_node_def, from_node, depth+1 )
112 #}
113 #}
114 else:
115 #{
116 if "default" in link_def:
117 #{
118 prop = link_def['default']
119 info[prop] = node_link.default_value
120 #}
121 #}
122 #}
123 else:
124 #{
125 prop = link_def
126 info[prop] = getattr( node, link )
127 #}
128 #}
129 #}
130
131 _graph_read( cxr_graph_mapping )
132 return info
133 #}
134
135 def sr_compile_material( mat ):
136 #{
137 if mat == None:
138 return 0
139 if mat.name in sr_compile.material_cache:
140 return sr_compile.material_cache[mat.name]
141
142 print( F'[SR] Compiling material {mat.name}' )
143
144 index = (len(sr_compile.material_data)//sizeof(mdl_material))+1
145 sr_compile.material_cache[mat.name] = index
146
147 m = mdl_material()
148 m.pstr_name = sr_compile_string( mat.name )
149
150 flags = 0x00
151 if mat.SR_data.collision:
152 #{
153 flags |= 0x2 # collision flag
154 if (mat.SR_data.shader != 'invisible') and \
155 (mat.SR_data.shader != 'boundary'):
156 #{
157 if mat.SR_data.skate_surface: flags |= 0x1
158 if mat.SR_data.grow_grass: flags |= 0x4
159 if mat.SR_data.grind_surface: flags |= 0x8
160 if mat.SR_data.preview_visibile: flags |= 0x40
161 #}
162 if mat.SR_data.shader == 'invisible': flags |= 0x10
163 if mat.SR_data.shader == 'boundary': flags |= (0x10|0x20)
164 if mat.SR_data.shader == 'walking': flags |= (0x10|0x80)
165 #}
166
167 m.flags = flags
168 m.surface_prop = int(mat.SR_data.surface_prop)
169 inf = material_info( mat )
170
171 msg = vg_msg()
172 buf = (c_uint8 * 512)()
173 sr_lib.vg_msg_init( pointer(msg), buf, 512 )
174
175 if mat.SR_data.tex_diffuse_rt >= 0:
176 #{
177 m.tex_diffuse = 0x80000000 | mat.SR_data.tex_diffuse_rt
178 #}
179 else:
180 #{
181 if 'tex_diffuse' in inf:
182 #{
183 shader_prop_texture( msg, 'tex_diffuse', inf['tex_diffuse'] )
184 #}
185 #}
186
187 if mat.SR_data.shader == 'standard': m.shader = 0
188 if mat.SR_data.shader == 'standard_cutout': m.shader = 1
189 if mat.SR_data.shader == 'foliage': m.shader = 10
190 if mat.SR_data.shader == 'terrain_blend':
191 #{
192 m.shader = 2
193 shader_prop_rgba( msg, "sand_colour", mat.SR_data.sand_colour )
194 shader_prop_f32( msg, "blend_offset", 2, mat.SR_data.blend_offset )
195 #}
196 if mat.SR_data.shader == 'vertex_blend':
197 #{
198 m.shader = 3
199 shader_prop_f32( msg, "blend_offset", 2, mat.SR_data.blend_offset )
200 #}
201 if mat.SR_data.shader == 'water':
202 #{
203 m.shader = 4
204 shader_prop_rgba( msg, 'shore_colour', mat.SR_data.shore_colour )
205 shader_prop_rgba( msg, 'deep_colour', mat.SR_data.ocean_colour )
206 shader_prop_f32( msg, 'fog_scale', 1, (mat.SR_data.water_fog,) )
207 shader_prop_f32( msg, 'fresnel', 1, (mat.SR_data.water_fresnel,) )
208 shader_prop_f32( msg, 'water_scale', 1, (mat.SR_data.water_scale,) )
209 shader_prop_f32( msg, 'wave_speed', 4, mat.SR_data.water_rate )
210 #}
211 if mat.SR_data.shader == 'invisible':
212 #{
213 m.shader = 5
214 #}
215 if mat.SR_data.shader == 'boundary':
216 #{
217 m.shader = 6
218 #}
219 if mat.SR_data.shader == 'fxglow':
220 #{
221 m.shader = 7
222 #}
223 if mat.SR_data.shader == 'cubemap':
224 #{
225 m.shader = 8
226
227 shader_prop_u32( msg, 'cubemap', sr_entity_id( mat.SR_data.cubemap ) )
228 shader_prop_rgba( msg, 'tint', mat.SR_data.tint )
229 #}
230 if mat.SR_data.shader == 'walking':
231 #{
232 m.shader = 9
233 #}
234
235 # sr_lib.vg_msg_print( byref(msg), msg.cur.co )
236
237 m.props.kvs.offset = len( sr_compile.shader_data )
238 m.props.kvs.size = msg.cur.co
239
240 sr_compile.shader_data.extend( bytearray(buf[:msg.cur.co]) )
241 sr_compile.material_data.extend( bytearray(m) )
242 return index
243 #}
244