2016-12-19 01:25:22 +01:00
|
|
|
import bpy
|
2017-03-15 12:30:14 +01:00
|
|
|
import arm.make_state as state
|
|
|
|
import arm.material.mat_state as mat_state
|
|
|
|
import arm.material.mat_utils as mat_utils
|
|
|
|
import arm.material.cycles as cycles
|
|
|
|
import arm.material.make_skin as make_skin
|
|
|
|
import arm.material.make_tess as make_tess
|
|
|
|
import arm.utils
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
is_displacement = False
|
2017-04-22 15:08:44 +02:00
|
|
|
write_material_attribs = None
|
2017-05-11 23:09:26 +02:00
|
|
|
write_material_attribs_post = None
|
|
|
|
write_vertex_attribs = None
|
2016-12-20 00:39:18 +01:00
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
def make(context_id, rid):
|
2016-12-20 00:39:18 +01:00
|
|
|
con_mesh = mat_state.data.add_context({ 'name': context_id, 'depth_write': True, 'compare_mode': 'less', 'cull_mode': 'clockwise' })
|
|
|
|
|
2016-12-19 01:25:22 +01:00
|
|
|
if rid == 'forward':
|
2017-04-26 14:21:22 +02:00
|
|
|
if bpy.data.cameras[0].rp_materials == 'Full':
|
|
|
|
make_forward(con_mesh)
|
|
|
|
else:
|
|
|
|
make_forward_restricted(con_mesh)
|
2016-12-19 01:25:22 +01:00
|
|
|
elif rid == 'deferred':
|
2016-12-20 00:39:18 +01:00
|
|
|
make_deferred(con_mesh)
|
2017-03-23 13:18:13 +01:00
|
|
|
elif rid == 'deferred_plus':
|
|
|
|
make_deferred_plus(con_mesh)
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-03-21 03:06:38 +01:00
|
|
|
make_finalize(con_mesh)
|
|
|
|
|
2016-12-20 00:39:18 +01:00
|
|
|
return con_mesh
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
def make_finalize(con_mesh):
|
|
|
|
vert = con_mesh.vert
|
|
|
|
frag = con_mesh.frag
|
|
|
|
geom = con_mesh.geom
|
|
|
|
tesc = con_mesh.tesc
|
|
|
|
tese = con_mesh.tese
|
|
|
|
|
|
|
|
# Additional values referenced in cycles
|
2017-03-21 03:06:38 +01:00
|
|
|
# TODO: enable from cycles.py
|
2017-03-14 20:43:54 +01:00
|
|
|
if frag.contains('dotNV') and not frag.contains('float dotNV'):
|
|
|
|
frag.prepend('float dotNV = max(dot(n, vVec), 0.0);')
|
|
|
|
|
2017-03-23 12:01:25 +01:00
|
|
|
write_wpos = False
|
2017-03-14 20:43:54 +01:00
|
|
|
if frag.contains('vVec') and not frag.contains('vec3 vVec'):
|
|
|
|
if is_displacement:
|
|
|
|
tese.add_out('vec3 eyeDir')
|
|
|
|
tese.add_uniform('vec3 eye', '_cameraPosition')
|
|
|
|
tese.write('eyeDir = eye - wposition;')
|
2017-03-23 12:01:25 +01:00
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
else:
|
|
|
|
vert.add_out('vec3 eyeDir')
|
|
|
|
vert.add_uniform('vec3 eye', '_cameraPosition')
|
|
|
|
vert.write('eyeDir = eye - wposition;')
|
2017-03-23 12:01:25 +01:00
|
|
|
write_wpos = True
|
2017-03-14 20:43:54 +01:00
|
|
|
frag.prepend('vec3 vVec = normalize(eyeDir);')
|
|
|
|
|
2017-03-23 12:01:25 +01:00
|
|
|
export_wpos = False
|
2017-03-14 20:43:54 +01:00
|
|
|
if frag.contains('wposition') and not frag.contains('vec3 wposition'):
|
|
|
|
if not is_displacement: # Displacement always outputs wposition
|
2017-03-23 12:01:25 +01:00
|
|
|
export_wpos = True
|
|
|
|
|
|
|
|
if export_wpos:
|
|
|
|
vert.add_uniform('mat4 W', '_worldMatrix')
|
|
|
|
vert.add_out('vec3 wposition')
|
|
|
|
vert.write_pre = True
|
|
|
|
vert.write('wposition = vec4(W * spos).xyz;')
|
|
|
|
vert.write_pre = False
|
|
|
|
elif write_wpos:
|
|
|
|
vert.add_uniform('mat4 W', '_worldMatrix')
|
|
|
|
vert.write_pre = True
|
|
|
|
vert.write('vec3 wposition = vec4(W * spos).xyz;')
|
|
|
|
vert.write_pre = False
|
2017-03-14 20:43:54 +01:00
|
|
|
|
2016-12-21 00:51:04 +01:00
|
|
|
def make_base(con_mesh, parse_opacity):
|
2017-03-14 20:43:54 +01:00
|
|
|
global is_displacement
|
2017-04-22 15:08:44 +02:00
|
|
|
global write_material_attribs
|
2017-05-11 23:09:26 +02:00
|
|
|
global write_material_attribs_post
|
|
|
|
global write_vertex_attribs
|
2017-03-14 20:43:54 +01:00
|
|
|
|
2016-12-19 01:25:22 +01:00
|
|
|
vert = con_mesh.make_vert()
|
2016-12-21 00:51:04 +01:00
|
|
|
frag = con_mesh.make_frag()
|
2016-12-19 01:25:22 +01:00
|
|
|
geom = None
|
|
|
|
tesc = None
|
|
|
|
tese = None
|
|
|
|
|
2017-03-11 01:50:47 +01:00
|
|
|
vert.add_uniform('mat3 N', '_normalMatrix')
|
2017-03-23 12:01:25 +01:00
|
|
|
vert.write_main_header('vec4 spos = vec4(pos, 1.0);')
|
2016-12-19 01:25:22 +01:00
|
|
|
|
|
|
|
if mat_utils.disp_linked(mat_state.output_node):
|
2017-03-14 20:43:54 +01:00
|
|
|
is_displacement = True
|
2016-12-19 01:25:22 +01:00
|
|
|
tesc = con_mesh.make_tesc()
|
|
|
|
tese = con_mesh.make_tese()
|
|
|
|
tesc.ins = vert.outs
|
|
|
|
tese.ins = tesc.outs
|
|
|
|
frag.ins = tese.outs
|
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
vert.add_uniform('mat4 W', '_worldMatrix')
|
|
|
|
vert.add_out('vec3 wposition')
|
|
|
|
vert.write('wposition = vec4(W * spos).xyz;')
|
|
|
|
|
2016-12-19 01:25:22 +01:00
|
|
|
const = {}
|
2017-03-12 17:29:22 +01:00
|
|
|
const['name'] = 'tessLevel'
|
2017-03-21 03:06:38 +01:00
|
|
|
const['vec2'] = [mat_state.material.height_tess_inner, mat_state.material.height_tess_outer]
|
2017-03-14 20:43:54 +01:00
|
|
|
mat_state.bind_constants.append(const)
|
2017-03-12 17:29:22 +01:00
|
|
|
tesc.add_uniform('vec2 tessLevel')
|
2016-12-20 00:39:18 +01:00
|
|
|
make_tess.tesc_levels(tesc)
|
|
|
|
make_tess.interpolate(tese, 'wposition', 3, declare_out=True)
|
|
|
|
make_tess.interpolate(tese, 'wnormal', 3, declare_out=True, normalize=True)
|
2016-12-19 01:25:22 +01:00
|
|
|
# No displacement
|
|
|
|
else:
|
2017-03-14 20:43:54 +01:00
|
|
|
is_displacement = False
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.ins = vert.outs
|
2017-05-11 23:09:26 +02:00
|
|
|
written = False
|
|
|
|
if write_vertex_attribs != None:
|
|
|
|
written = write_vertex_attribs(vert)
|
|
|
|
if written == False:
|
|
|
|
vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix')
|
|
|
|
vert.write('gl_Position = WVP * spos;')
|
2016-12-19 01:25:22 +01:00
|
|
|
|
|
|
|
frag.add_include('../../Shaders/compiled.glsl')
|
|
|
|
|
2017-05-01 22:25:36 +02:00
|
|
|
written = False
|
2017-04-22 15:08:44 +02:00
|
|
|
if write_material_attribs != None:
|
2017-05-25 20:14:53 +02:00
|
|
|
written = write_material_attribs(con_mesh, frag)
|
2017-05-01 22:25:36 +02:00
|
|
|
if written == False:
|
2017-04-22 15:08:44 +02:00
|
|
|
frag.write('vec3 basecol;')
|
|
|
|
frag.write('float roughness;')
|
|
|
|
frag.write('float metallic;')
|
|
|
|
frag.write('float occlusion;')
|
|
|
|
if parse_opacity:
|
|
|
|
frag.write('float opacity;')
|
2017-05-25 16:48:41 +02:00
|
|
|
cycles.parse(mat_state.nodes, con_mesh, vert, frag, geom, tesc, tese, parse_opacity=parse_opacity)
|
2017-05-11 23:09:26 +02:00
|
|
|
if write_material_attribs_post != None:
|
2017-05-25 20:14:53 +02:00
|
|
|
write_material_attribs_post(con_mesh, frag)
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('tex'):
|
2016-12-19 01:25:22 +01:00
|
|
|
vert.add_out('vec2 texCoord')
|
|
|
|
vert.write('texCoord = tex;')
|
|
|
|
if tese != None:
|
|
|
|
# TODO: also includes texCoord1
|
|
|
|
tese.write_pre = True
|
2016-12-20 00:39:18 +01:00
|
|
|
make_tess.interpolate(tese, 'texCoord', 2, declare_out=frag.contains('texCoord'))
|
2016-12-19 01:25:22 +01:00
|
|
|
tese.write_pre = False
|
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('tex1'):
|
2016-12-19 01:25:22 +01:00
|
|
|
vert.add_out('vec2 texCoord1')
|
|
|
|
vert.write('texCoord1 = tex1;')
|
|
|
|
if tese != None:
|
|
|
|
tese.write_pre = True
|
2016-12-20 00:39:18 +01:00
|
|
|
make_tess.interpolate(tese, 'texCoord1', 2, declare_out=frag.contains('texCoord1'))
|
2016-12-19 01:25:22 +01:00
|
|
|
tese.write_pre = False
|
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('col'):
|
2016-12-19 01:25:22 +01:00
|
|
|
vert.add_out('vec3 vcolor')
|
|
|
|
vert.write('vcolor = col;')
|
|
|
|
if tese != None:
|
|
|
|
tese.write_pre = True
|
2016-12-20 00:39:18 +01:00
|
|
|
make_tess.interpolate(tese, 'vcolor', 3, declare_out=frag.contains('vcolor'))
|
2016-12-19 01:25:22 +01:00
|
|
|
tese.write_pre = False
|
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('tang'):
|
2016-12-19 01:25:22 +01:00
|
|
|
if tese != None:
|
|
|
|
vert.add_out('vec3 wnormal')
|
|
|
|
vert.add_out('vec3 wtangent')
|
2017-05-25 16:48:41 +02:00
|
|
|
write_norpos(con_mesh, vert)
|
2017-03-11 01:50:47 +01:00
|
|
|
vert.write('wtangent = normalize(N * tang);')
|
2016-12-19 01:25:22 +01:00
|
|
|
tese.add_out('mat3 TBN')
|
2016-12-20 00:39:18 +01:00
|
|
|
make_tess.interpolate(tese, 'wtangent', 3, normalize=True)
|
2016-12-19 01:25:22 +01:00
|
|
|
tese.write('vec3 wbitangent = normalize(cross(wnormal, wtangent));')
|
|
|
|
tese.write('TBN = mat3(wtangent, wbitangent, wnormal);')
|
|
|
|
else:
|
|
|
|
vert.add_out('mat3 TBN')
|
2017-05-25 16:48:41 +02:00
|
|
|
write_norpos(con_mesh, vert, declare=True)
|
2017-03-11 01:50:47 +01:00
|
|
|
vert.write('vec3 tangent = normalize(N * tang);')
|
2016-12-19 01:25:22 +01:00
|
|
|
vert.write('vec3 bitangent = normalize(cross(wnormal, tangent));')
|
|
|
|
vert.write('TBN = mat3(tangent, bitangent, wnormal);')
|
|
|
|
else:
|
|
|
|
vert.add_out('vec3 wnormal')
|
2017-05-25 16:48:41 +02:00
|
|
|
write_norpos(con_mesh, vert)
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.write_pre = True
|
2017-03-23 12:01:25 +01:00
|
|
|
frag.write_main_header('vec3 n = normalize(wnormal);')
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.write_pre = False
|
|
|
|
|
|
|
|
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);')
|
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
def write_norpos(con_mesh, vert, declare=False):
|
2016-12-19 01:25:22 +01:00
|
|
|
prep = ''
|
|
|
|
if declare:
|
|
|
|
prep = 'vec3 '
|
|
|
|
vert.write_pre = True
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('bone'):
|
2016-12-19 01:25:22 +01:00
|
|
|
make_skin.skin_pos(vert)
|
2017-03-11 01:50:47 +01:00
|
|
|
vert.write(prep + 'wnormal = normalize(N * (nor + 2.0 * cross(skinA.xyz, cross(skinA.xyz, nor) + skinA.w * nor)));')
|
2016-12-19 01:25:22 +01:00
|
|
|
else:
|
2017-03-11 01:50:47 +01:00
|
|
|
vert.write(prep + 'wnormal = normalize(N * nor);')
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('off'):
|
2016-12-21 00:51:04 +01:00
|
|
|
vert.write('spos.xyz += off;')
|
2016-12-19 01:25:22 +01:00
|
|
|
vert.write_pre = False
|
|
|
|
|
2016-12-20 00:39:18 +01:00
|
|
|
def make_deferred(con_mesh):
|
2016-12-21 00:51:04 +01:00
|
|
|
wrd = bpy.data.worlds['Arm']
|
|
|
|
make_base(con_mesh, parse_opacity=False)
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2016-12-20 00:39:18 +01:00
|
|
|
frag = con_mesh.frag
|
2016-12-21 00:51:04 +01:00
|
|
|
vert = con_mesh.vert
|
2017-01-08 13:21:54 +01:00
|
|
|
tese = con_mesh.tese
|
2016-12-21 00:51:04 +01:00
|
|
|
|
2017-06-23 15:47:51 +02:00
|
|
|
gapi = arm.utils.get_gapi()
|
2017-01-14 12:44:43 +01:00
|
|
|
if '_Veloc' in wrd.rp_defs:
|
2016-12-21 00:51:04 +01:00
|
|
|
frag.add_out('vec4[3] fragColor')
|
2017-01-08 13:21:54 +01:00
|
|
|
if tese == None:
|
|
|
|
vert.add_uniform('mat4 prevWVP', link='_prevWorldViewProjectionMatrix')
|
|
|
|
vert.add_out('vec4 wvpposition')
|
|
|
|
vert.add_out('vec4 prevwvpposition')
|
|
|
|
vert.write('wvpposition = gl_Position;')
|
|
|
|
vert.write('prevwvpposition = prevWVP * spos;')
|
|
|
|
else:
|
|
|
|
vert.add_uniform('mat4 prevW', link='_prevWorldMatrix')
|
|
|
|
vert.add_out('vec3 prevwposition')
|
|
|
|
vert.write('prevwposition = vec4(prevW * spos).xyz;')
|
|
|
|
tese.add_out('vec4 wvpposition')
|
|
|
|
tese.add_out('vec4 prevwvpposition')
|
|
|
|
tese.add_uniform('mat4 prevVP', '_prevViewProjectionMatrix')
|
|
|
|
tese.write('wvpposition = gl_Position;')
|
|
|
|
make_tess.interpolate(tese, 'prevwposition', 3)
|
|
|
|
tese.write('prevwvpposition = prevVP * vec4(prevwposition, 1.0);')
|
2017-06-23 15:47:51 +02:00
|
|
|
elif gapi.startswith('direct3d'):
|
|
|
|
vert.add_out('vec4 wvpposition')
|
|
|
|
vert.write('wvpposition = gl_Position;')
|
|
|
|
frag.add_out('vec4[2] fragColor')
|
2016-12-21 00:51:04 +01:00
|
|
|
else:
|
|
|
|
frag.add_out('vec4[2] fragColor')
|
|
|
|
|
|
|
|
# Pack gbuffer
|
2016-12-20 00:39:18 +01:00
|
|
|
frag.add_include('../../Shaders/std/gbuffer.glsl')
|
|
|
|
frag.write('n /= (abs(n.x) + abs(n.y) + abs(n.z));')
|
|
|
|
frag.write('n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);')
|
2017-03-11 01:50:47 +01:00
|
|
|
# TODO: store_depth
|
2017-06-23 15:47:51 +02:00
|
|
|
if gapi.startswith('direct3d'):
|
|
|
|
frag.write('fragColor[0] = vec4(n.xy, packFloat(metallic, roughness), 1.0 - ((wvpposition.z / wvpposition.w) * 0.5 + 0.5));')
|
|
|
|
else:
|
|
|
|
frag.write('fragColor[0] = vec4(n.xy, packFloat(metallic, roughness), 1.0 - gl_FragCoord.z);')
|
2017-05-23 15:01:56 +02:00
|
|
|
if '_SSS' in wrd.rp_defs:
|
|
|
|
frag.add_uniform('int materialID')
|
|
|
|
frag.write('fragColor[1] = vec4(basecol.rgb, materialID + clamp(occlusion, 0.0, 1.0 - 0.001));')
|
|
|
|
else:
|
|
|
|
frag.write('fragColor[1] = vec4(basecol.rgb, occlusion);')
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-01-14 12:44:43 +01:00
|
|
|
if '_Veloc' in wrd.rp_defs:
|
2016-12-21 00:51:04 +01:00
|
|
|
frag.write('vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5;')
|
|
|
|
frag.write('vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5;')
|
|
|
|
frag.write('fragColor[2].rg = vec2(posa - posb);')
|
|
|
|
|
2016-12-20 00:39:18 +01:00
|
|
|
return con_mesh
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-03-23 13:18:13 +01:00
|
|
|
def make_deferred_plus(con_mesh):
|
|
|
|
wrd = bpy.data.worlds['Arm']
|
|
|
|
vert = con_mesh.make_vert()
|
|
|
|
frag = con_mesh.make_frag()
|
|
|
|
|
2017-03-28 14:30:51 +02:00
|
|
|
frag.add_out('vec4[3] fragColor')
|
2017-03-23 13:18:13 +01:00
|
|
|
|
|
|
|
vert.add_uniform('mat3 N', '_normalMatrix')
|
|
|
|
vert.write_main_header('vec4 spos = vec4(pos, 1.0);')
|
|
|
|
|
|
|
|
frag.ins = vert.outs
|
|
|
|
vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix')
|
|
|
|
vert.write('gl_Position = WVP * spos;')
|
|
|
|
|
|
|
|
frag.add_include('../../Shaders/compiled.glsl')
|
|
|
|
|
2017-03-28 14:30:51 +02:00
|
|
|
vert.add_out('vec2 texCoord')
|
|
|
|
|
2017-06-08 23:58:18 +02:00
|
|
|
con_mesh.add_elem('tex', 2) #### Add using cycles.py
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('tex'):
|
2017-03-23 13:18:13 +01:00
|
|
|
vert.write('texCoord = tex;')
|
2017-03-28 14:30:51 +02:00
|
|
|
else:
|
|
|
|
vert.write('texCoord = vec2(0.0);')
|
2017-03-23 13:18:13 +01:00
|
|
|
|
|
|
|
vert.add_out('vec3 wnormal')
|
2017-05-25 16:48:41 +02:00
|
|
|
write_norpos(con_mesh, vert)
|
2017-03-23 13:18:13 +01:00
|
|
|
frag.write_pre = True
|
|
|
|
frag.write_main_header('vec3 n = normalize(wnormal);')
|
|
|
|
frag.write_pre = False
|
|
|
|
|
2017-03-28 14:30:51 +02:00
|
|
|
frag.add_uniform('float materialID', link='_objectInfoMaterialIndex')
|
|
|
|
|
2017-03-23 13:18:13 +01:00
|
|
|
# Pack gbuffer
|
|
|
|
frag.add_include('../../Shaders/std/gbuffer.glsl')
|
|
|
|
frag.write('n /= (abs(n.x) + abs(n.y) + abs(n.z));')
|
|
|
|
frag.write('n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);')
|
2017-03-28 14:30:51 +02:00
|
|
|
frag.write('fragColor[0] = vec4(n.xy, fract(texCoord));')
|
|
|
|
frag.write('fragColor[1] = vec4(materialID, 0.0, 0.0, 0.0);')
|
|
|
|
frag.write('fragColor[2] = vec4(dFdx(texCoord), dFdy(texCoord));')
|
|
|
|
# + tangent space
|
2017-03-23 13:18:13 +01:00
|
|
|
|
2017-04-26 14:21:22 +02:00
|
|
|
def make_forward_restricted(con_mesh):
|
|
|
|
wrd = bpy.data.worlds['Arm']
|
|
|
|
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);')
|
|
|
|
frag.ins = vert.outs
|
|
|
|
vert.add_uniform('mat4 WVP', '_worldViewProjectionMatrix')
|
|
|
|
vert.write('gl_Position = WVP * spos;')
|
|
|
|
|
|
|
|
frag.add_include('../../Shaders/compiled.glsl')
|
|
|
|
frag.write('vec3 basecol;')
|
2017-05-25 16:48:41 +02:00
|
|
|
cycles.parse(mat_state.nodes, con_mesh, vert, frag, geom, tesc, tese, basecol_only=True, parse_opacity=False, parse_displacement=False)
|
2017-04-26 14:21:22 +02:00
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('tex'):
|
2017-04-26 14:21:22 +02:00
|
|
|
vert.add_out('vec2 texCoord')
|
|
|
|
vert.write('texCoord = tex;')
|
|
|
|
|
2017-05-25 16:48:41 +02:00
|
|
|
if con_mesh.is_elem('col'):
|
2017-04-26 14:21:22 +02:00
|
|
|
vert.add_out('vec3 vcolor')
|
|
|
|
vert.write('vcolor = col;')
|
|
|
|
|
|
|
|
vert.add_out('vec3 wnormal')
|
2017-05-25 16:48:41 +02:00
|
|
|
write_norpos(con_mesh, vert)
|
2017-04-26 14:21:22 +02:00
|
|
|
frag.write_pre = True
|
|
|
|
frag.write_main_header('vec3 n = normalize(wnormal);')
|
|
|
|
frag.write_pre = False
|
|
|
|
|
|
|
|
frag.add_uniform('vec3 lightColor', '_lampColor')
|
|
|
|
frag.add_uniform('vec3 lightDir', '_lampDirection')
|
|
|
|
frag.add_uniform('float envmapStrength', link='_envmapStrength')
|
|
|
|
|
|
|
|
if '_NoShadows' in wrd.world_defs:
|
|
|
|
is_shadows = False
|
|
|
|
else:
|
|
|
|
is_shadows = True
|
|
|
|
|
|
|
|
frag.write('float visibility = 1.0;')
|
|
|
|
frag.write('float dotNL = dot(n, lightDir);')
|
|
|
|
|
|
|
|
if is_shadows:
|
|
|
|
vert.add_out('vec4 lampPos')
|
|
|
|
vert.add_uniform('mat4 LWVP', '_biasLampWorldViewProjectionMatrix')
|
|
|
|
vert.write('lampPos = LWVP * spos;')
|
|
|
|
frag.add_include('../../Shaders/std/shadows.glsl')
|
|
|
|
frag.add_uniform('sampler2D shadowMap', included=True)
|
|
|
|
frag.add_uniform('float shadowsBias', '_lampShadowsBias')
|
|
|
|
frag.write(' if (lampPos.w > 0.0) {')
|
|
|
|
frag.write(' vec3 lpos = lampPos.xyz / lampPos.w;')
|
|
|
|
# frag.write(' visibility *= PCF(lpos.xy, lpos.z - shadowsBias);')
|
|
|
|
frag.write(' const float texelSize = 1.0 / shadowmapSize.x;')
|
|
|
|
frag.write(' visibility = 0.0;')
|
|
|
|
frag.write(' visibility += float(texture(shadowMap, lpos.xy).r + shadowsBias > lpos.z);')
|
|
|
|
frag.write(' visibility += float(texture(shadowMap, lpos.xy + vec2(texelSize, 0.0)).r + shadowsBias > lpos.z) * 0.5;')
|
|
|
|
frag.write(' visibility += float(texture(shadowMap, lpos.xy + vec2(-texelSize, 0.0)).r + shadowsBias > lpos.z) * 0.25;')
|
|
|
|
frag.write(' visibility += float(texture(shadowMap, lpos.xy + vec2(0.0, texelSize)).r + shadowsBias > lpos.z) * 0.5;')
|
|
|
|
frag.write(' visibility += float(texture(shadowMap, lpos.xy + vec2(0.0, -texelSize)).r + shadowsBias > lpos.z) * 0.25;')
|
|
|
|
frag.write(' visibility /= 2.5;')
|
|
|
|
frag.write(' visibility = max(visibility, 0.5);')
|
|
|
|
# frag.write(' visibility = max(float(texture(shadowMap, lpos.xy).r + shadowsBias > lpos.z), 0.5);')
|
|
|
|
frag.write(' }')
|
|
|
|
|
|
|
|
frag.write('vec3 direct = basecol * max(dotNL, 0.1);')
|
|
|
|
|
|
|
|
frag.add_out('vec4 fragColor')
|
|
|
|
frag.write('fragColor = vec4(direct * lightColor * visibility, 1.0);')
|
|
|
|
|
|
|
|
if '_LDR' in bpy.data.worlds['Arm'].rp_defs:
|
|
|
|
frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));')
|
|
|
|
|
2016-12-21 00:51:04 +01:00
|
|
|
def make_forward(con_mesh):
|
|
|
|
make_forward_base(con_mesh)
|
|
|
|
|
2016-12-21 19:15:51 +01:00
|
|
|
frag = con_mesh.frag
|
2016-12-21 00:51:04 +01:00
|
|
|
frag.add_out('vec4 fragColor')
|
|
|
|
|
|
|
|
frag.write('fragColor = vec4(direct * lightColor * visibility + indirect * occlusion * envmapStrength, 1.0);')
|
|
|
|
|
2017-01-14 12:44:43 +01:00
|
|
|
if '_LDR' in bpy.data.worlds['Arm'].rp_defs:
|
2017-04-28 12:43:40 +02:00
|
|
|
frag.add_include('../../Shaders/std/tonemap.glsl')
|
|
|
|
frag.write('fragColor.rgb = tonemapFilmic(fragColor.rgb);')
|
|
|
|
# frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));')
|
2016-12-21 00:51:04 +01:00
|
|
|
|
|
|
|
def make_forward_base(con_mesh, parse_opacity=False):
|
|
|
|
wrd = bpy.data.worlds['Arm']
|
|
|
|
make_base(con_mesh, parse_opacity=parse_opacity)
|
2016-12-20 00:39:18 +01:00
|
|
|
vert = con_mesh.vert
|
|
|
|
frag = con_mesh.frag
|
|
|
|
tese = con_mesh.tese
|
2016-12-19 01:25:22 +01:00
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
if is_displacement:
|
|
|
|
tese.add_out('vec3 eyeDir')
|
|
|
|
tese.add_uniform('vec3 eye', '_cameraPosition')
|
|
|
|
tese.write('eyeDir = eye - wposition;')
|
|
|
|
else:
|
|
|
|
vert.add_out('vec3 wposition')
|
|
|
|
vert.add_uniform('mat4 W', '_worldMatrix')
|
|
|
|
vert.write('wposition = vec4(W * spos).xyz;')
|
|
|
|
vert.add_out('vec3 eyeDir')
|
|
|
|
vert.add_uniform('vec3 eye', '_cameraPosition')
|
|
|
|
vert.write('eyeDir = eye - wposition;')
|
|
|
|
|
2017-04-26 17:37:30 +02:00
|
|
|
frag.prepend(""" vec3 vVec = normalize(eyeDir);
|
|
|
|
float dotNV = max(dot(n, vVec), 0.0);""")
|
2017-03-14 20:43:54 +01:00
|
|
|
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.add_include('../../Shaders/std/brdf.glsl')
|
|
|
|
frag.add_include('../../Shaders/std/math.glsl')
|
|
|
|
frag.add_uniform('vec3 lightColor', '_lampColor')
|
|
|
|
frag.add_uniform('vec3 lightDir', '_lampDirection')
|
|
|
|
frag.add_uniform('vec3 lightPos', '_lampPosition')
|
|
|
|
frag.add_uniform('int lightType', '_lampType')
|
2017-03-12 17:29:22 +01:00
|
|
|
frag.add_uniform('vec2 spotlightData', '_spotlampData') # cutoff, cutoff - exponent
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.add_uniform('float envmapStrength', link='_envmapStrength')
|
2017-01-23 20:41:45 +01:00
|
|
|
|
|
|
|
if '_Irr' in wrd.world_defs:
|
|
|
|
frag.add_include('../../Shaders/std/shirr.glsl')
|
2017-02-07 11:50:21 +01:00
|
|
|
frag.add_uniform('vec4 shirr[7]', link='_envmapIrradiance', included=True)
|
2017-01-23 20:41:45 +01:00
|
|
|
if '_Rad' in wrd.world_defs:
|
|
|
|
frag.add_uniform('sampler2D senvmapRadiance', link='_envmapRadiance')
|
|
|
|
frag.add_uniform('sampler2D senvmapBrdf', link='_envmapBrdf')
|
|
|
|
frag.add_uniform('int envmapNumMipmaps', link='_envmapNumMipmaps')
|
2016-12-20 00:39:18 +01:00
|
|
|
|
2017-01-23 00:48:59 +01:00
|
|
|
if '_NoShadows' in wrd.world_defs:
|
|
|
|
is_shadows = False
|
|
|
|
else:
|
|
|
|
is_shadows = True
|
2016-12-20 00:39:18 +01:00
|
|
|
if '_PCSS' in wrd.world_defs:
|
|
|
|
is_pcss = True
|
|
|
|
else:
|
|
|
|
is_pcss = False
|
|
|
|
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.write('float visibility = 1.0;')
|
2017-03-20 21:02:23 +01:00
|
|
|
frag.write('vec3 lp = lightPos - wposition;')
|
2017-03-02 10:15:22 +01:00
|
|
|
frag.write('vec3 l;')
|
|
|
|
frag.write('if (lightType == 0) l = lightDir;')
|
2017-03-20 21:02:23 +01:00
|
|
|
frag.write('else { l = normalize(lp); visibility *= attenuate(distance(wposition, lightPos)); }')
|
2017-03-14 20:43:54 +01:00
|
|
|
frag.write('vec3 h = normalize(vVec + l);')
|
2017-03-02 10:15:22 +01:00
|
|
|
frag.write('float dotNL = dot(n, l);')
|
|
|
|
frag.write('float dotNH = dot(n, h);')
|
2017-03-14 20:43:54 +01:00
|
|
|
frag.write('float dotVH = dot(vVec, h);')
|
2017-03-02 10:15:22 +01:00
|
|
|
|
2017-01-23 00:48:59 +01:00
|
|
|
if is_shadows:
|
|
|
|
if tese != None:
|
|
|
|
tese.add_out('vec4 lampPos')
|
2017-02-28 14:13:50 +01:00
|
|
|
tese.add_uniform('mat4 LVP', '_biasLampViewProjectionMatrix')
|
2017-03-20 21:02:23 +01:00
|
|
|
tese.add_uniform('int lightShadow', '_lampCastShadow')
|
|
|
|
tese.write('if (lightShadow == 1) lampPos = LVP * vec4(wposition, 1.0);')
|
2017-01-23 00:48:59 +01:00
|
|
|
else:
|
|
|
|
vert.add_out('vec4 lampPos')
|
2017-02-28 14:13:50 +01:00
|
|
|
vert.add_uniform('mat4 LWVP', '_biasLampWorldViewProjectionMatrix')
|
2017-03-20 21:02:23 +01:00
|
|
|
vert.add_uniform('int lightShadow', '_lampCastShadow')
|
|
|
|
vert.write('if (lightShadow == 1) lampPos = LWVP * spos;')
|
2017-01-23 00:48:59 +01:00
|
|
|
|
|
|
|
if is_pcss:
|
|
|
|
frag.add_include('../../Shaders/std/shadows_pcss.glsl')
|
|
|
|
frag.add_uniform('sampler2D snoise', link='_noise64', included=True)
|
|
|
|
frag.add_uniform('float lampSizeUV', link='_lampSizeUV', included=True)
|
|
|
|
else:
|
|
|
|
frag.add_include('../../Shaders/std/shadows.glsl')
|
|
|
|
frag.add_uniform('sampler2D shadowMap', included=True)
|
2017-04-16 14:46:35 +02:00
|
|
|
frag.add_uniform('samplerCube shadowMapCube', included=True)
|
2017-01-23 00:48:59 +01:00
|
|
|
frag.add_uniform('bool receiveShadow')
|
|
|
|
frag.add_uniform('float shadowsBias', '_lampShadowsBias')
|
2017-03-20 21:02:23 +01:00
|
|
|
frag.add_uniform('int lightShadow', '_lampCastShadow')
|
|
|
|
frag.add_uniform('vec2 lightPlane', '_lampPlane')
|
2017-01-23 00:48:59 +01:00
|
|
|
|
2017-03-20 21:02:23 +01:00
|
|
|
frag.write('if (receiveShadow) {')
|
|
|
|
frag.write(' if (lightShadow == 1 && lampPos.w > 0.0) {')
|
|
|
|
frag.write(' vec3 lpos = lampPos.xyz / lampPos.w;')
|
2017-03-02 10:15:22 +01:00
|
|
|
# frag.write('float bias = clamp(shadowsBias * 1.0 * tan(acos(clamp(dotNL, 0.0, 1.0))), 0.0, 0.01);')
|
2017-01-23 00:48:59 +01:00
|
|
|
if is_pcss:
|
2017-03-20 21:02:23 +01:00
|
|
|
frag.write(' visibility *= PCSS(lpos.xy, lpos.z - shadowsBias);')
|
2017-01-23 00:48:59 +01:00
|
|
|
else:
|
2017-03-20 21:02:23 +01:00
|
|
|
frag.write(' visibility *= PCF(lpos.xy, lpos.z - shadowsBias);')
|
|
|
|
frag.write(' }')
|
|
|
|
frag.write(' else if (lightShadow == 2) visibility *= PCFCube(lp, -l, shadowsBias, lightPlane);')
|
2017-01-23 00:48:59 +01:00
|
|
|
frag.write('}')
|
2016-12-19 01:25:22 +01:00
|
|
|
|
|
|
|
frag.write('if (lightType == 2) {')
|
2017-03-03 14:36:01 +01:00
|
|
|
frag.write(' float spotEffect = dot(lightDir, l);')
|
2017-03-11 01:50:47 +01:00
|
|
|
frag.write(' if (spotEffect < spotlightData.x) {')
|
|
|
|
frag.write(' visibility *= smoothstep(spotlightData.y, spotlightData.x, spotEffect);')
|
2017-03-03 14:36:01 +01:00
|
|
|
frag.write(' }')
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.write('}')
|
|
|
|
|
|
|
|
frag.write('vec3 albedo = surfaceAlbedo(basecol, metallic);')
|
|
|
|
frag.write('vec3 f0 = surfaceF0(basecol, metallic);')
|
2017-03-03 14:36:01 +01:00
|
|
|
frag.write('vec3 direct;')
|
|
|
|
|
|
|
|
if '_PolyLight' in wrd.world_defs:
|
|
|
|
frag.add_include('../../Shaders/std/ltc.glsl')
|
|
|
|
frag.add_uniform('sampler2D sltcMat', link='_ltcMat')
|
|
|
|
frag.add_uniform('sampler2D sltcMag', link='_ltcMag')
|
|
|
|
frag.add_uniform('vec3 lampArea0', link='_lampArea0')
|
|
|
|
frag.add_uniform('vec3 lampArea1', link='_lampArea1')
|
|
|
|
frag.add_uniform('vec3 lampArea2', link='_lampArea2')
|
|
|
|
frag.add_uniform('vec3 lampArea3', link='_lampArea3')
|
|
|
|
frag.write('if (lightType == 3) {')
|
|
|
|
frag.write(' float theta = acos(dotNV);')
|
|
|
|
frag.write(' vec2 tuv = vec2(roughness, theta / (0.5 * PI));')
|
|
|
|
frag.write(' tuv = tuv * LUT_SCALE + LUT_BIAS;')
|
|
|
|
frag.write(' vec4 t = texture(sltcMat, tuv);')
|
|
|
|
frag.write(' mat3 invM = mat3(vec3(1.0, 0.0, t.y), vec3(0.0, t.z, 0.0), vec3(t.w, 0.0, t.x));')
|
2017-03-14 20:43:54 +01:00
|
|
|
frag.write(' float ltcspec = ltcEvaluate(n, vVec, dotNV, wposition, invM, lampArea0, lampArea1, lampArea2, lampArea3);')
|
2017-03-03 14:36:01 +01:00
|
|
|
frag.write(' ltcspec *= texture(sltcMag, tuv).a;')
|
2017-03-14 20:43:54 +01:00
|
|
|
frag.write(' float ltcdiff = ltcEvaluate(n, vVec, dotNV, wposition, mat3(1.0), lampArea0, lampArea1, lampArea2, lampArea3);')
|
2017-03-03 14:36:01 +01:00
|
|
|
frag.write(' direct = albedo * ltcdiff + ltcspec;')
|
|
|
|
frag.write('}')
|
|
|
|
frag.write('else {')
|
|
|
|
frag.tab += 1
|
|
|
|
|
|
|
|
frag.write('direct = lambertDiffuseBRDF(albedo, dotNL);')
|
2016-12-19 01:25:22 +01:00
|
|
|
frag.write('direct += specularBRDF(f0, roughness, dotNL, dotNH, dotNV, dotVH);')
|
|
|
|
|
2017-03-03 14:36:01 +01:00
|
|
|
if '_PolyLight' in wrd.world_defs:
|
|
|
|
frag.write('}')
|
|
|
|
frag.tab -= 1
|
|
|
|
|
2017-01-23 20:41:45 +01:00
|
|
|
if '_Irr' in wrd.world_defs:
|
|
|
|
frag.write('vec3 indirect = (shIrradiance(n, 2.2) / PI) * albedo;')
|
|
|
|
|
|
|
|
if '_Rad' in wrd.world_defs:
|
2017-03-14 20:43:54 +01:00
|
|
|
frag.write('vec3 reflectionWorld = reflect(-vVec, n);')
|
2017-01-23 20:41:45 +01:00
|
|
|
frag.write('float lod = getMipFromRoughness(roughness, envmapNumMipmaps);')
|
|
|
|
frag.write('vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;')
|
|
|
|
if '_EnvLDR' in wrd.world_defs:
|
|
|
|
frag.write('prefilteredColor = pow(prefilteredColor, vec3(2.2));')
|
|
|
|
frag.write('vec2 envBRDF = texture(senvmapBrdf, vec2(roughness, 1.0 - dotNV)).xy;')
|
|
|
|
frag.write('indirect += prefilteredColor * (f0 * envBRDF.x + envBRDF.y);')
|
|
|
|
else:
|
|
|
|
frag.write('vec3 indirect = albedo;')
|