def write_vertpos(vert): billboard = mat_state.material.arm_billboard particle = mat_state.material.arm_particle # Particles if particle != 'off': if particle == 'gpu': make_particle.write(vert, particle_info=cycles.particle_info) # Billboards if billboard == 'spherical': vert.add_uniform('mat4 WV', '_worldViewMatrix') vert.add_uniform('mat4 P', '_projectionMatrix') vert.write( 'gl_Position = P * (WV * vec4(0.0, 0.0, spos.z, 1.0) + vec4(spos.x, spos.y, 0.0, 0.0));' ) else: vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix') vert.write('gl_Position = WVP * spos;') else: # Billboards if billboard == 'spherical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixSphere') elif billboard == 'cylindrical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixCylinder') else: # off vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix') vert.write('gl_Position = WVP * spos;')
def make(context_id, rpasses, shadowmap=False): is_disp = mat_utils.disp_linked(mat_state.output_node) vs = [{'name': 'pos', 'data': 'short4norm'}] if is_disp: vs.append({'name': 'nor', 'data': 'short2norm'}) con_depth = mat_state.data.add_context({ 'name': context_id, 'vertex_elements': vs, 'depth_write': True, 'compare_mode': 'less', 'cull_mode': 'clockwise', 'color_writes_red': [False], 'color_writes_green': [False], 'color_writes_blue': [False], 'color_writes_alpha': [False] }) vert = con_depth.make_vert() frag = con_depth.make_frag() geom = None tesc = None tese = None vert.write_attrib('vec4 spos = vec4(pos.xyz, 1.0);') parse_opacity = 'translucent' in rpasses or mat_state.material.arm_discard if parse_opacity: frag.write('vec3 n;') # Discard at compile time frag.write('float dotNV;') frag.write('float opacity;') if con_depth.is_elem('bone'): make_skin.skin_pos(vert) if con_depth.is_elem('ipos'): make_inst.inst_pos(con_depth, vert) rpdat = arm.utils.get_rp() if mat_state.material.arm_particle_flag and rpdat.arm_particles == 'On': make_particle.write(vert, shadowmap=shadowmap) if is_disp: if rpdat.arm_rp_displacement == 'Vertex': frag.ins = vert.outs vert.add_uniform('mat3 N', '_normalMatrix') vert.write('vec3 wnormal = normalize(N * vec3(nor.xy, pos.w));') cycles.parse(mat_state.nodes, con_depth, vert, frag, geom, tesc, tese, parse_surface=False, parse_opacity=parse_opacity) if con_depth.is_elem('tex'): vert.add_out('vec2 texCoord') ## vs only, remove out vert.add_uniform('float texUnpack', link='_texUnpack') vert.write_attrib('texCoord = tex * texUnpack;') if con_depth.is_elem('tex1'): vert.add_out('vec2 texCoord1') ## vs only, remove out vert.add_uniform('float texUnpack', link='_texUnpack') vert.write_attrib('texCoord1 = tex1 * texUnpack;') if con_depth.is_elem('col'): vert.add_out('vec3 vcolor') vert.write_attrib('vcolor = col.rgb;') vert.write('wposition += wnormal * disp;') if shadowmap: vert.add_uniform('mat4 LVP', '_lightViewProjectionMatrix') vert.write('gl_Position = LVP * vec4(wposition, 1.0);') else: vert.add_uniform('mat4 VP', '_viewProjectionMatrix') vert.write('gl_Position = VP * vec4(wposition, 1.0);') else: # Tessellation tesc = con_depth.make_tesc() tese = con_depth.make_tese() tesc.ins = vert.outs tese.ins = tesc.outs frag.ins = tese.outs vert.add_out('vec3 wnormal') vert.add_uniform('mat3 N', '_normalMatrix') vert.write('wnormal = normalize(N * vec3(nor.xy, pos.w));') make_tess.tesc_levels(tesc, rpdat.arm_tess_shadows_inner, rpdat.arm_tess_shadows_outer) make_tess.interpolate(tese, 'wposition', 3) make_tess.interpolate(tese, 'wnormal', 3, normalize=True) cycles.parse(mat_state.nodes, con_depth, vert, frag, geom, tesc, tese, parse_surface=False, parse_opacity=parse_opacity) if con_depth.is_elem('tex'): vert.add_out('vec2 texCoord') vert.add_uniform('float texUnpack', link='_texUnpack') vert.write('texCoord = tex * texUnpack;') tese.write_pre = True make_tess.interpolate(tese, 'texCoord', 2, declare_out=frag.contains('texCoord')) tese.write_pre = False if con_depth.is_elem('tex1'): vert.add_out('vec2 texCoord1') vert.write('texCoord1 = tex1;') tese.write_pre = True make_tess.interpolate(tese, 'texCoord1', 2, declare_out=frag.contains('texCoord1')) tese.write_pre = False if con_depth.is_elem('col'): vert.add_out('vec3 vcolor') vert.write('vcolor = col.rgb;') tese.write_pre = True make_tess.interpolate(tese, 'vcolor', 3, declare_out=frag.contains('vcolor')) tese.write_pre = False if shadowmap: tese.add_uniform('mat4 LVP', '_lightViewProjectionMatrix') tese.write('wposition += wnormal * disp;') tese.write('gl_Position = LVP * vec4(wposition, 1.0);') else: tese.add_uniform('mat4 VP', '_viewProjectionMatrix') tese.write('wposition += wnormal * disp;') tese.write('gl_Position = VP * vec4(wposition, 1.0);') # No displacement else: frag.ins = vert.outs billboard = mat_state.material.arm_billboard if shadowmap: if billboard == 'spherical': vert.add_uniform('mat4 LWVP', '_lightWorldViewProjectionMatrixSphere') elif billboard == 'cylindrical': vert.add_uniform('mat4 LWVP', '_lightWorldViewProjectionMatrixCylinder') else: # off vert.add_uniform('mat4 LWVP', '_lightWorldViewProjectionMatrix') vert.write('gl_Position = LWVP * spos;') else: if billboard == 'spherical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixSphere') elif billboard == 'cylindrical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixCylinder') else: # off vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix') vert.write('gl_Position = WVP * spos;') if parse_opacity: cycles.parse(mat_state.nodes, con_depth, vert, frag, geom, tesc, tese, parse_surface=False, parse_opacity=True) if con_depth.is_elem('tex'): vert.add_out('vec2 texCoord') vert.add_uniform('float texUnpack', link='_texUnpack') if mat_state.material.arm_tilesheet_flag: vert.add_uniform('vec2 tilesheetOffset', '_tilesheetOffset') vert.write('texCoord = tex * texUnpack + tilesheetOffset;') else: vert.write('texCoord = tex * texUnpack;') if con_depth.is_elem('tex1'): vert.add_out('vec2 texCoord1') vert.write('texCoord1 = tex1;') if con_depth.is_elem('col'): vert.add_out('vec3 vcolor') vert.write('vcolor = col.rgb;') if parse_opacity: if mat_state.material.arm_discard: opac = mat_state.material.arm_discard_opacity_shadows else: opac = '1.0' frag.write('if (opacity < {0}) discard;'.format(opac)) make_finalize.make(con_depth) assets.vs_equal(con_depth, assets.shader_cons['depth_vert']) assets.fs_equal(con_depth, assets.shader_cons['depth_frag']) return con_depth
def make_base(con_mesh, parse_opacity): global is_displacement global write_material_attribs global write_material_attribs_post global write_vertex_attribs vert = con_mesh.make_vert() frag = con_mesh.make_frag() geom = None tesc = None tese = None vert.add_uniform('mat3 N', '_normalMatrix') vert.write_main_header(' vec4 spos = vec4(pos, 1.0);') vattr_written = False is_displacement = mat_utils.disp_linked(mat_state.output_node) if is_displacement: tesc = con_mesh.make_tesc() tese = con_mesh.make_tese() tesc.ins = vert.outs tese.ins = tesc.outs frag.ins = tese.outs vert.add_uniform('mat4 W', '_worldMatrix') vert.add_out('vec3 wposition') vert.write('wposition = vec4(W * spos).xyz;') make_tess.tesc_levels(tesc, mat_state.material.arm_tess_inner, mat_state.material.arm_tess_outer) make_tess.interpolate(tese, 'wposition', 3, declare_out=True) make_tess.interpolate(tese, 'wnormal', 3, declare_out=True, normalize=True) # No displacement else: frag.ins = vert.outs if write_vertex_attribs != None: vattr_written = write_vertex_attribs(vert) frag.add_include('../../Shaders/compiled.glsl') written = False if write_material_attribs != None: written = write_material_attribs(con_mesh, frag) if written == False: frag.write('vec3 basecol;') frag.write('float roughness;') frag.write('float metallic;') frag.write('float occlusion;') if parse_opacity: frag.write('float opacity;') cycles.parse(mat_state.nodes, con_mesh, vert, frag, geom, tesc, tese, parse_opacity=parse_opacity) if write_material_attribs_post != None: write_material_attribs_post(con_mesh, frag) if not is_displacement and not vattr_written: billboard = mat_state.material.arm_billboard particle = mat_state.material.arm_particle wrd = bpy.data.worlds['Arm'] # Particles if particle != 'off': if particle == 'gpu': make_particle.write(vert, particle_info=cycles.particle_info) # Billboards if billboard == 'spherical': vert.add_uniform('mat4 WV', '_worldViewMatrix') vert.add_uniform('mat4 P', '_projectionMatrix') vert.write( 'gl_Position = P * (WV * vec4(0.0, 0.0, spos.z, 1.0) + vec4(spos.x, spos.y, 0.0, 0.0));' ) else: vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix') vert.write('gl_Position = WVP * spos;') else: # Billboards if billboard == 'spherical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixSphere') elif billboard == 'cylindrical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixCylinder') else: # off vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix') vert.write('gl_Position = WVP * spos;') if con_mesh.is_elem('tex'): vert.add_out('vec2 texCoord') if mat_state.material.arm_tilesheet_mat: if mat_state.material.arm_particle == 'gpu': make_particle.write_tilesheet(vert) else: vert.add_uniform('vec2 tilesheetOffset', '_tilesheetOffset') vert.write('texCoord = tex + tilesheetOffset;') else: vert.write('texCoord = tex;') if tese != None: # TODO: also includes texCoord1 tese.write_pre = True make_tess.interpolate(tese, 'texCoord', 2, declare_out=frag.contains('texCoord')) tese.write_pre = False if con_mesh.is_elem('tex1'): vert.add_out('vec2 texCoord1') vert.write('texCoord1 = tex1;') if tese != None: tese.write_pre = True make_tess.interpolate(tese, 'texCoord1', 2, declare_out=frag.contains('texCoord1')) tese.write_pre = False if con_mesh.is_elem('col'): vert.add_out('vec3 vcolor') vert.write('vcolor = col;') if tese != None: tese.write_pre = True make_tess.interpolate(tese, 'vcolor', 3, declare_out=frag.contains('vcolor')) tese.write_pre = False if con_mesh.is_elem('tang'): if tese != None: vert.add_out('vec3 wnormal') vert.add_out('vec3 wtangent') write_norpos(con_mesh, vert) vert.write('wtangent = normalize(N * tang);') tese.add_out('mat3 TBN') make_tess.interpolate(tese, 'wtangent', 3, normalize=True) tese.write( 'vec3 wbitangent = normalize(cross(wnormal, wtangent));') tese.write('TBN = mat3(wtangent, wbitangent, wnormal);') else: vert.add_out('mat3 TBN') write_norpos(con_mesh, vert, declare=True) vert.write('vec3 tangent = normalize(N * tang);') vert.write('vec3 bitangent = normalize(cross(wnormal, tangent));') vert.write('TBN = mat3(tangent, bitangent, wnormal);') else: vert.add_out('vec3 wnormal') write_norpos(con_mesh, vert) frag.prepend_header('vec3 n = normalize(wnormal);') if tese != None: tese.add_uniform('mat4 VP', '_viewProjectionMatrix') # TODO: Sample disp at neightbour points to calc normal tese.write('wposition += wnormal * disp * 0.2;') tese.write('gl_Position = VP * vec4(wposition, 1.0);')
def make(context_id, rpasses, shadowmap=False): is_disp = mat_utils.disp_linked( mat_state.output_node) and mat_state.material.arm_tess_shadows vs = [{'name': 'pos', 'size': 3}] if is_disp: vs.append({'name': 'nor', 'size': 3}) con_depth = mat_state.data.add_context({ 'name': context_id, 'vertex_structure': vs, 'depth_write': True, 'compare_mode': 'less', 'cull_mode': 'clockwise', 'color_write_red': False, 'color_write_green': False, 'color_write_blue': False, 'color_write_alpha': False }) vert = con_depth.make_vert() frag = con_depth.make_frag() geom = None tesc = None tese = None vert.write_main_header('vec4 spos = vec4(pos, 1.0);') parse_opacity = 'translucent' in rpasses or mat_state.material.arm_discard if parse_opacity: frag.write('vec3 n;') # Discard at compile time frag.write('float dotNV;') frag.write('float opacity;') if con_depth.is_elem('bone'): make_skin.skin_pos(vert) if con_depth.is_elem('off'): vert.write('spos.xyz += off;') wrd = bpy.data.worlds['Arm'] if mat_state.material.arm_particle == 'gpu': make_particle.write(vert) if is_disp: tesc = con_depth.make_tesc() tese = con_depth.make_tese() tesc.ins = vert.outs tese.ins = tesc.outs frag.ins = tese.outs vert.add_out('vec3 wposition') vert.add_out('vec3 wnormal') vert.add_uniform('mat4 W', '_worldMatrix') vert.add_uniform('mat3 N', '_normalMatrix') vert.write('wnormal = normalize(N * nor);') vert.write('wposition = vec4(W * spos).xyz;') make_tess.tesc_levels(tesc, mat_state.material.arm_tess_shadows_inner, mat_state.material.arm_tess_shadows_outer) make_tess.interpolate(tese, 'wposition', 3) make_tess.interpolate(tese, 'wnormal', 3, normalize=True) cycles.parse(mat_state.nodes, con_depth, vert, frag, geom, tesc, tese, parse_surface=False, parse_opacity=parse_opacity) if con_depth.is_elem('tex'): vert.add_out('vec2 texCoord') vert.write('texCoord = tex;') tese.write_pre = True make_tess.interpolate(tese, 'texCoord', 2, declare_out=frag.contains('texCoord')) tese.write_pre = False if con_depth.is_elem('tex1'): vert.add_out('vec2 texCoord1') vert.write('texCoord1 = tex1;') tese.write_pre = True make_tess.interpolate(tese, 'texCoord1', 2, declare_out=frag.contains('texCoord1')) tese.write_pre = False if con_depth.is_elem('col'): vert.add_out('vec3 vcolor') vert.write('vcolor = col;') tese.write_pre = True make_tess.interpolate(tese, 'vcolor', 3, declare_out=frag.contains('vcolor')) tese.write_pre = False if shadowmap: tese.add_uniform('mat4 LVP', '_lampViewProjectionMatrix') tese.write('wposition += wnormal * disp * 0.2;') tese.write('gl_Position = LVP * vec4(wposition, 1.0);') else: tese.add_uniform('mat4 VP', '_viewProjectionMatrix') tese.write('wposition += wnormal * disp * 0.2;') tese.write('gl_Position = VP * vec4(wposition, 1.0);') # No displacement else: frag.ins = vert.outs billboard = mat_state.material.arm_billboard if shadowmap: if billboard == 'spherical': vert.add_uniform('mat4 LWVP', '_lampWorldViewProjectionMatrixSphere') elif billboard == 'cylindrical': vert.add_uniform('mat4 LWVP', '_lampWorldViewProjectionMatrixCylinder') else: # off vert.add_uniform('mat4 LWVP', '_lampWorldViewProjectionMatrix') vert.write('gl_Position = LWVP * spos;') else: if billboard == 'spherical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixSphere') elif billboard == 'cylindrical': vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrixCylinder') else: # off vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix') vert.write('gl_Position = WVP * spos;') if parse_opacity: cycles.parse(mat_state.nodes, con_depth, vert, frag, geom, tesc, tese, parse_surface=False, parse_opacity=True) if con_depth.is_elem('tex'): vert.add_out('vec2 texCoord') if mat_state.material.arm_tilesheet_mat: vert.add_uniform('vec2 tilesheetOffset', '_tilesheetOffset') vert.write('texCoord = tex + tilesheetOffset;') else: vert.write('texCoord = tex;') if con_depth.is_elem('tex1'): vert.add_out('vec2 texCoord1') vert.write('texCoord1 = tex1;') if con_depth.is_elem('col'): vert.add_out('vec3 vcolor') vert.write('vcolor = col;') # TODO: interleaved buffer has to match vertex structure of mesh context if not bpy.data.worlds['Arm'].arm_deinterleaved_buffers: con_depth.add_elem('nor', 3) if mat_state.con_mesh != None: if mat_state.con_mesh.is_elem('tex'): con_depth.add_elem('tex', 2) if mat_state.con_mesh.is_elem('tex1'): con_depth.add_elem('tex1', 2) if mat_state.con_mesh.is_elem('col'): con_depth.add_elem('col', 3) if mat_state.con_mesh.is_elem('tang'): con_depth.add_elem('tang', 4) # TODO: pass vbuf with proper struct gapi = arm.utils.get_gapi() if gapi.startswith('direct3d') and bpy.data.worlds[ 'Arm'].arm_deinterleaved_buffers == False: vert.write('vec3 t1 = nor; // TODO: Temp for d3d') if con_depth.is_elem('tex'): vert.write('vec2 t2 = tex; // TODO: Temp for d3d') if parse_opacity: opac = mat_state.material.arm_discard_opacity_shadows frag.write('if (opacity < {0}) discard;'.format(opac)) make_mesh.make_finalize(con_depth) assets.vs_equal(con_depth, assets.shader_cons['depth_vert']) assets.fs_equal(con_depth, assets.shader_cons['depth_frag']) return con_depth