def parse(self, frag: Shader, vert: Shader) -> str: if self.input_type == "uniform": frag.add_uniform(f'{self.variable_type} {self.variable_name}', link=self.variable_name) vert.add_uniform(f'{self.variable_type} {self.variable_name}', link=self.variable_name) if self.variable_type == "sampler2D": frag.add_uniform('vec2 screenSize', link='_screenSize') return f'texture({self.variable_name}, gl_FragCoord.xy / screenSize).rgb' return self.variable_name else: if self.input_source == "frag": frag.add_in(f'{self.variable_type} {self.variable_name}') return self.variable_name # Reroute input from vertex shader to fragment shader (input must exist!) else: vert.add_out(f'{self.variable_type} out_{self.variable_name}') frag.add_in(f'{self.variable_type} out_{self.variable_name}') vert.write(f'out_{self.variable_name} = {self.variable_name};') return 'out_' + self.variable_name
def finalize(frag: Shader, vert: Shader): """Checks the given fragment shader for completeness and adds variable initializations if required. TODO: Merge with make_finalize? """ if frag.contains('pos') and not frag.contains('vec3 pos'): frag.write_attrib('vec3 pos = -n;') if frag.contains('vVec') and not frag.contains('vec3 vVec'): # For worlds, the camera seems to be always at origin in # Blender, so we can just use the normals as the incoming vector frag.write_attrib('vec3 vVec = n;') for var in ('bposition', 'mposition', 'wposition'): if (frag.contains(var) and not frag.contains(f'vec3 {var}')) or vert.contains(var): frag.add_in(f'vec3 {var}') vert.add_out(f'vec3 {var}') vert.write(f'{var} = pos;') if frag.contains('wtangent') and not frag.contains('vec3 wtangent'): frag.write_attrib('vec3 wtangent = vec3(0.0);') if frag.contains('texCoord') and not frag.contains('vec2 texCoord'): frag.add_in('vec2 texCoord') vert.add_out('vec2 texCoord') # World has no UV map vert.write('texCoord = vec2(1.0, 1.0);')
def parse(self, frag: Shader, vert: Shader) -> str: if self.input_type == "uniform": frag.add_uniform(f'{self.variable_type} {self.variable_name}', link=self.variable_name) return self.variable_name else: if self.input_source == "frag": frag.add_in(f'{self.variable_type} {self.variable_name}') return self.variable_name # Reroute input from vertex shader to fragment shader (input must exist!) else: vert.add_out(f'{self.variable_type} out_{self.variable_name}') frag.add_in(f'{self.variable_type} out_{self.variable_name}') vert.write(f'out_{self.variable_name} = {self.variable_name};') return 'out_' + self.variable_name
def write_tex_coords(con_mesh: ShaderContext, vert: Shader, frag: Shader, tese: Optional[Shader]): rpdat = arm.utils.get_rp() if con_mesh.is_elem('tex'): vert.add_out('vec2 texCoord') vert.add_uniform('float texUnpack', link='_texUnpack') if mat_state.material.arm_tilesheet_flag: if mat_state.material.arm_particle_flag and rpdat.arm_particles == 'On': make_particle.write_tilesheet(vert) else: vert.add_uniform('vec2 tilesheetOffset', '_tilesheetOffset') vert.write_attrib( 'texCoord = tex * texUnpack + tilesheetOffset;') else: vert.write_attrib('texCoord = tex * texUnpack;') if tese is not None: 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.add_uniform('float texUnpack', link='_texUnpack') vert.write_attrib('texCoord1 = tex1 * texUnpack;') if tese is not None: tese.write_pre = True make_tess.interpolate(tese, 'texCoord1', 2, declare_out=frag.contains('texCoord1')) tese.write_pre = False
def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader, con: ShaderContext): """Generates the shader code for the given world.""" world_name = arm.utils.safestr(world.name) world.world_defs = '' rpdat = arm.utils.get_rp() wrd = bpy.data.worlds['Arm'] if callback is not None: callback() # film_transparent, do not render if bpy.context.scene is not None and bpy.context.scene.render.film_transparent: world.world_defs += '_EnvCol' frag.add_uniform('vec3 backgroundCol', link='_backgroundCol') frag.write('fragColor.rgb = backgroundCol;') return parser_state = ParserState(ParserContext.WORLD, world) parser_state.con = con parser_state.curshader = frag parser_state.frag = frag parser_state.vert = vert cycles.state = parser_state # Traverse world node tree is_parsed = False if world.node_tree is not None: output_node = node_utils.get_node_by_type(world.node_tree, 'OUTPUT_WORLD') if output_node is not None: is_parsed = parse_world_output(world, output_node, frag) # No world nodes/no output node, use background color if not is_parsed: solid_mat = rpdat.arm_material_model == 'Solid' if rpdat.arm_irradiance and not solid_mat: world.world_defs += '_Irr' col = world.color world.arm_envtex_color = [col[0], col[1], col[2], 1.0] world.arm_envtex_strength = 1.0 # Irradiance/Radiance: clear to color if no texture or sky is provided if rpdat.arm_irradiance or rpdat.arm_irradiance: if '_EnvSky' not in world.world_defs and '_EnvTex' not in world.world_defs and '_EnvImg' not in world.world_defs: # Irradiance json file name world.arm_envtex_name = world_name world.arm_envtex_irr_name = world_name write_probes.write_color_irradiance(world_name, world.arm_envtex_color) # Clouds enabled if rpdat.arm_clouds and world.arm_use_clouds: world.world_defs += '_EnvClouds' # Also set this flag globally so that the required textures are # included wrd.world_defs += '_EnvClouds' frag_write_clouds(world, frag) if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvImg' in world.world_defs or '_EnvClouds' in world.world_defs: frag.add_uniform('float envmapStrength', link='_envmapStrength') # Clear background color if '_EnvCol' in world.world_defs: frag.write('fragColor.rgb = backgroundCol;') elif '_EnvTex' in world.world_defs and '_EnvLDR' in world.world_defs: frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(2.2));') if '_EnvClouds' in world.world_defs: frag.write( 'if (n.z > 0.0) fragColor.rgb = mix(fragColor.rgb, traceClouds(fragColor.rgb, n), clamp(n.z * 5.0, 0, 1));' ) if '_EnvLDR' in world.world_defs: frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));') # Mark as non-opaque frag.write('fragColor.a = 0.0;') # Hack to make procedural textures work frag_bpos = ( frag.contains('bposition') and not frag.contains('vec3 bposition')) or vert.contains('bposition') if frag_bpos: frag.add_in('vec3 bposition') vert.add_out('vec3 bposition') # Use normals for now vert.write('bposition = nor;') frag_mpos = ( frag.contains('mposition') and not frag.contains('vec3 mposition')) or vert.contains('mposition') if frag_mpos: frag.add_in('vec3 mposition') vert.add_out('vec3 mposition') # Use normals for now vert.write('mposition = nor;') if frag.contains('texCoord') and not frag.contains('vec2 texCoord'): frag.add_in('vec2 texCoord') vert.add_out('vec2 texCoord') # World has no UV map vert.write('texCoord = vec2(1.0, 1.0);')