Changeset View
Changeset View
Standalone View
Standalone View
io_scene_x3d/export_x3d.py
| Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
| # h3d defines | # h3d defines | ||||
| H3D_TOP_LEVEL = 'TOP_LEVEL_TI' | H3D_TOP_LEVEL = 'TOP_LEVEL_TI' | ||||
| H3D_CAMERA_FOLLOW = 'CAMERA_FOLLOW_TRANSFORM' | H3D_CAMERA_FOLLOW = 'CAMERA_FOLLOW_TRANSFORM' | ||||
| H3D_VIEW_MATRIX = 'view_matrix' | H3D_VIEW_MATRIX = 'view_matrix' | ||||
| def clamp_color(col): | def clight_color(col): | ||||
| return tuple([max(min(c, 1.0), 0.0) for c in col]) | return tuple([max(min(c, 1.0), 0.0) for c in col]) | ||||
| def matrix_direction_neg_z(matrix): | def matrix_direction_neg_z(matrix): | ||||
| return (matrix.to_3x3() * mathutils.Vector((0.0, 0.0, -1.0))).normalized()[:] | return (matrix.to_3x3() * mathutils.Vector((0.0, 0.0, -1.0))).normalized()[:] | ||||
| def prefix_quoted_str(value, prefix): | def prefix_quoted_str(value, prefix): | ||||
| ▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | def h3d_shader_glsl_frag_patch(filepath, scene, global_vars, frag_uniform_var_map): | ||||
| for l in h3d_file: | for l in h3d_file: | ||||
| if l.startswith("void main(void)"): | if l.startswith("void main(void)"): | ||||
| lines.append("\n") | lines.append("\n") | ||||
| lines.append("// h3d custom vars begin\n") | lines.append("// h3d custom vars begin\n") | ||||
| for v in global_vars: | for v in global_vars: | ||||
| lines.append("%s\n" % v) | lines.append("%s\n" % v) | ||||
| lines.append("// h3d custom vars end\n") | lines.append("// h3d custom vars end\n") | ||||
| lines.append("\n") | lines.append("\n") | ||||
| elif l.lstrip().startswith("lamp_visibility_other("): | elif l.lstrip().startswith("light_visibility_other("): | ||||
| w = l.split(', ') | w = l.split(', ') | ||||
| last_transform = w[1] + "_transform" # XXX - HACK!!! | last_transform = w[1] + "_transform" # XXX - HACK!!! | ||||
| w[1] = '(view_matrix * %s_transform * vec4(%s.x, %s.y, %s.z, 1.0)).xyz' % (w[1], w[1], w[1], w[1]) | w[1] = '(view_matrix * %s_transform * vec4(%s.x, %s.y, %s.z, 1.0)).xyz' % (w[1], w[1], w[1], w[1]) | ||||
| l = ", ".join(w) | l = ", ".join(w) | ||||
| elif l.lstrip().startswith("lamp_visibility_sun_hemi("): | elif l.lstrip().startswith("light_visibility_sun_hemi("): | ||||
| w = l.split(', ') | w = l.split(', ') | ||||
| w[0] = w[0][len("lamp_visibility_sun_hemi(") + 1:] | w[0] = w[0][len("light_visibility_sun_hemi(") + 1:] | ||||
| if not h3d_is_object_view(scene, frag_uniform_var_map[w[0]]): | if not h3d_is_object_view(scene, frag_uniform_var_map[w[0]]): | ||||
| w[0] = '(mat3(normalize(view_matrix[0].xyz), normalize(view_matrix[1].xyz), normalize(view_matrix[2].xyz)) * -%s)' % w[0] | w[0] = '(mat3(normalize(view_matrix[0].xyz), normalize(view_matrix[1].xyz), normalize(view_matrix[2].xyz)) * -%s)' % w[0] | ||||
| else: | else: | ||||
| w[0] = ('(mat3(normalize((view_matrix*%s)[0].xyz), normalize((view_matrix*%s)[1].xyz), normalize((view_matrix*%s)[2].xyz)) * -%s)' % | w[0] = ('(mat3(normalize((view_matrix*%s)[0].xyz), normalize((view_matrix*%s)[1].xyz), normalize((view_matrix*%s)[2].xyz)) * -%s)' % | ||||
| (last_transform, last_transform, last_transform, w[0])) | (last_transform, last_transform, last_transform, w[0])) | ||||
| l = "\tlamp_visibility_sun_hemi(" + ", ".join(w) | l = "\tlight_visibility_sun_hemi(" + ", ".join(w) | ||||
| elif l.lstrip().startswith("lamp_visibility_spot_circle("): | elif l.lstrip().startswith("light_visibility_spot_circle("): | ||||
| w = l.split(', ') | w = l.split(', ') | ||||
| w[0] = w[0][len("lamp_visibility_spot_circle(") + 1:] | w[0] = w[0][len("light_visibility_spot_circle(") + 1:] | ||||
| if not h3d_is_object_view(scene, frag_uniform_var_map[w[0]]): | if not h3d_is_object_view(scene, frag_uniform_var_map[w[0]]): | ||||
| w[0] = '(mat3(normalize(view_matrix[0].xyz), normalize(view_matrix[1].xyz), normalize(view_matrix[2].xyz)) * -%s)' % w[0] | w[0] = '(mat3(normalize(view_matrix[0].xyz), normalize(view_matrix[1].xyz), normalize(view_matrix[2].xyz)) * -%s)' % w[0] | ||||
| else: | else: | ||||
| w[0] = ('(mat3(normalize((view_matrix*%s)[0].xyz), normalize((view_matrix*%s)[1].xyz), normalize((view_matrix*%s)[2].xyz)) * %s)' % | w[0] = ('(mat3(normalize((view_matrix*%s)[0].xyz), normalize((view_matrix*%s)[1].xyz), normalize((view_matrix*%s)[2].xyz)) * %s)' % | ||||
| (last_transform, last_transform, last_transform, w[0])) | (last_transform, last_transform, last_transform, w[0])) | ||||
| l = "\tlamp_visibility_spot_circle(" + ", ".join(w) | l = "\tlight_visibility_spot_circle(" + ", ".join(w) | ||||
| lines.append(l) | lines.append(l) | ||||
| h3d_file.close() | h3d_file.close() | ||||
| h3d_file = open(filepath, 'w', encoding='utf-8') | h3d_file = open(filepath, 'w', encoding='utf-8') | ||||
| h3d_file.writelines(lines) | h3d_file.writelines(lines) | ||||
| h3d_file.close() | h3d_file.close() | ||||
| Show All 34 Lines | def export(file, | ||||
| from xml.sax.saxutils import quoteattr, escape | from xml.sax.saxutils import quoteattr, escape | ||||
| if name_decorations: | if name_decorations: | ||||
| # If names are decorated, the uuid map can be split up | # If names are decorated, the uuid map can be split up | ||||
| # by type for efficiency of collision testing | # by type for efficiency of collision testing | ||||
| # since objects of different types will always have | # since objects of different types will always have | ||||
| # different decorated names. | # different decorated names. | ||||
| uuid_cache_object = {} # object | uuid_cache_object = {} # object | ||||
| uuid_cache_lamp = {} # 'LA_' + object.name | uuid_cache_light = {} # 'LA_' + object.name | ||||
| uuid_cache_view = {} # object, different namespace | uuid_cache_view = {} # object, different namespace | ||||
| uuid_cache_mesh = {} # mesh | uuid_cache_mesh = {} # mesh | ||||
| uuid_cache_material = {} # material | uuid_cache_material = {} # material | ||||
| uuid_cache_image = {} # image | uuid_cache_image = {} # image | ||||
| uuid_cache_world = {} # world | uuid_cache_world = {} # world | ||||
| CA_ = 'CA_' | CA_ = 'CA_' | ||||
| OB_ = 'OB_' | OB_ = 'OB_' | ||||
| ME_ = 'ME_' | ME_ = 'ME_' | ||||
| IM_ = 'IM_' | IM_ = 'IM_' | ||||
| WO_ = 'WO_' | WO_ = 'WO_' | ||||
| MA_ = 'MA_' | MA_ = 'MA_' | ||||
| LA_ = 'LA_' | LA_ = 'LA_' | ||||
| group_ = 'group_' | group_ = 'group_' | ||||
| else: | else: | ||||
| # If names are not decorated, it may be possible for two objects to | # If names are not decorated, it may be possible for two objects to | ||||
| # have the same name, so there has to be a unified dictionary to | # have the same name, so there has to be a unified dictionary to | ||||
| # prevent uuid collisions. | # prevent uuid collisions. | ||||
| uuid_cache = {} | uuid_cache = {} | ||||
| uuid_cache_object = uuid_cache # object | uuid_cache_object = uuid_cache # object | ||||
| uuid_cache_lamp = uuid_cache # 'LA_' + object.name | uuid_cache_light = uuid_cache # 'LA_' + object.name | ||||
| uuid_cache_view = uuid_cache # object, different namespace | uuid_cache_view = uuid_cache # object, different namespace | ||||
| uuid_cache_mesh = uuid_cache # mesh | uuid_cache_mesh = uuid_cache # mesh | ||||
| uuid_cache_material = uuid_cache # material | uuid_cache_material = uuid_cache # material | ||||
| uuid_cache_image = uuid_cache # image | uuid_cache_image = uuid_cache # image | ||||
| uuid_cache_world = uuid_cache # world | uuid_cache_world = uuid_cache # world | ||||
| del uuid_cache | del uuid_cache | ||||
| CA_ = '' | CA_ = '' | ||||
| OB_ = '' | OB_ = '' | ||||
| ▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | def writeFog(ident, world): | ||||
| mparam = world.mist_settings | mparam = world.mist_settings | ||||
| else: | else: | ||||
| return | return | ||||
| if mparam.use_mist: | if mparam.use_mist: | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<Fog ' % ident))) | fw('%s<Fog ' % ident))) | ||||
| fw('fogType="%s"\n' % ('LINEAR' if (mtype == 'LINEAR') else 'EXPONENTIAL')) | fw('fogType="%s"\n' % ('LINEAR' if (mtype == 'LINEAR') else 'EXPONENTIAL')) | ||||
| fw(ident_step + 'color="%.3f %.3f %.3f"\n' % clamp_color(world.horizon_color)) | fw(ident_step + 'color="%.3f %.3f %.3f"\n' % clight_color(world.horizon_color)) | ||||
| fw(ident_step + 'visibilityRange="%.3f"\n' % mparam.depth) | fw(ident_step + 'visibilityRange="%.3f"\n' % mparam.depth) | ||||
| fw(ident_step + '/>\n') | fw(ident_step + '/>\n') | ||||
| else: | else: | ||||
| return | return | ||||
| def writeNavigationInfo(ident, scene, has_lamp): | def writeNavigationInfo(ident, scene, has_light): | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<NavigationInfo ' % ident))) | fw('%s<NavigationInfo ' % ident))) | ||||
| fw('headlight="%s"\n' % bool_as_str(not has_lamp)) | fw('headlight="%s"\n' % bool_as_str(not has_light)) | ||||
| fw(ident_step + 'visibilityLimit="0.0"\n') | fw(ident_step + 'visibilityLimit="0.0"\n') | ||||
| fw(ident_step + 'type=\'"EXAMINE", "ANY"\'\n') | fw(ident_step + 'type=\'"EXAMINE", "ANY"\'\n') | ||||
| fw(ident_step + 'avatarSize="0.25, 1.75, 0.75"\n') | fw(ident_step + 'avatarSize="0.25, 1.75, 0.75"\n') | ||||
| fw(ident_step + '/>\n') | fw(ident_step + '/>\n') | ||||
| def writeTransform_begin(ident, matrix, def_id): | def writeTransform_begin(ident, matrix, def_id): | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<Transform ' % ident))) | fw('%s<Transform ' % ident))) | ||||
| Show All 15 Lines | def writeTransform_begin(ident, matrix, def_id): | ||||
| return ident | return ident | ||||
| def writeTransform_end(ident): | def writeTransform_end(ident): | ||||
| ident = ident[:-1] | ident = ident[:-1] | ||||
| fw('%s</Transform>\n' % ident) | fw('%s</Transform>\n' % ident) | ||||
| return ident | return ident | ||||
| def writeSpotLight(ident, obj, matrix, lamp, world): | def writeSpotLight(ident, obj, matrix, lamp, world): | ||||
| # note, lamp_id is not re-used | # note, light_id is not re-used | ||||
| lamp_id = quoteattr(unique_name(obj, LA_ + obj.name, uuid_cache_lamp, clean_func=clean_def, sep="_")) | light_id = quoteattr(unique_name(obj, LA_ + obj.name, uuid_cache_light, clean_func=clean_def, sep="_")) | ||||
| if world: | if world: | ||||
| ambi = world.ambient_color | ambi = world.ambient_color | ||||
| amb_intensity = ((ambi[0] + ambi[1] + ambi[2]) / 3.0) / 2.5 | amb_intensity = ((ambi[0] + ambi[1] + ambi[2]) / 3.0) / 2.5 | ||||
| del ambi | del ambi | ||||
| else: | else: | ||||
| amb_intensity = 0.0 | amb_intensity = 0.0 | ||||
| # compute cutoff and beamwidth | # compute cutoff and beamwidth | ||||
| intensity = min(lamp.energy / 1.75, 1.0) | intensity = min(lamp.energy / 1.75, 1.0) | ||||
| beamWidth = lamp.spot_size * 0.37 | beamWidth = lamp.spot_size * 0.37 | ||||
| # beamWidth=((lamp.spotSize*math.pi)/180.0)*.37 | # beamWidth=((lamp.spotSize*math.pi)/180.0)*.37 | ||||
| cutOffAngle = beamWidth * 1.3 | cutOffAngle = beamWidth * 1.3 | ||||
| orientation = matrix_direction_neg_z(matrix) | orientation = matrix_direction_neg_z(matrix) | ||||
| location = matrix.to_translation()[:] | location = matrix.to_translation()[:] | ||||
| radius = lamp.distance * math.cos(beamWidth) | radius = lamp.distance * math.cos(beamWidth) | ||||
| # radius = lamp.dist*math.cos(beamWidth) | # radius = lamp.dist*math.cos(beamWidth) | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<SpotLight ' % ident))) | fw('%s<SpotLight ' % ident))) | ||||
| fw('DEF=%s\n' % lamp_id) | fw('DEF=%s\n' % light_id) | ||||
| fw(ident_step + 'radius="%.4f"\n' % radius) | fw(ident_step + 'radius="%.4f"\n' % radius) | ||||
| fw(ident_step + 'ambientIntensity="%.4f"\n' % amb_intensity) | fw(ident_step + 'ambientIntensity="%.4f"\n' % amb_intensity) | ||||
| fw(ident_step + 'intensity="%.4f"\n' % intensity) | fw(ident_step + 'intensity="%.4f"\n' % intensity) | ||||
| fw(ident_step + 'color="%.4f %.4f %.4f"\n' % clamp_color(lamp.color)) | fw(ident_step + 'color="%.4f %.4f %.4f"\n' % clight_color(lamp.color)) | ||||
| fw(ident_step + 'beamWidth="%.4f"\n' % beamWidth) | fw(ident_step + 'beamWidth="%.4f"\n' % beamWidth) | ||||
| fw(ident_step + 'cutOffAngle="%.4f"\n' % cutOffAngle) | fw(ident_step + 'cutOffAngle="%.4f"\n' % cutOffAngle) | ||||
| fw(ident_step + 'direction="%.4f %.4f %.4f"\n' % orientation) | fw(ident_step + 'direction="%.4f %.4f %.4f"\n' % orientation) | ||||
| fw(ident_step + 'location="%.4f %.4f %.4f"\n' % location) | fw(ident_step + 'location="%.4f %.4f %.4f"\n' % location) | ||||
| fw(ident_step + '/>\n') | fw(ident_step + '/>\n') | ||||
| def writeDirectionalLight(ident, obj, matrix, lamp, world): | def writeDirectionalLight(ident, obj, matrix, lamp, world): | ||||
| # note, lamp_id is not re-used | # note, light_id is not re-used | ||||
| lamp_id = quoteattr(unique_name(obj, LA_ + obj.name, uuid_cache_lamp, clean_func=clean_def, sep="_")) | light_id = quoteattr(unique_name(obj, LA_ + obj.name, uuid_cache_light, clean_func=clean_def, sep="_")) | ||||
| if world: | if world: | ||||
| ambi = world.ambient_color | ambi = world.ambient_color | ||||
| # ambi = world.amb | # ambi = world.amb | ||||
| amb_intensity = ((float(ambi[0] + ambi[1] + ambi[2])) / 3.0) / 2.5 | amb_intensity = ((float(ambi[0] + ambi[1] + ambi[2])) / 3.0) / 2.5 | ||||
| else: | else: | ||||
| ambi = 0 | ambi = 0 | ||||
| amb_intensity = 0.0 | amb_intensity = 0.0 | ||||
| intensity = min(lamp.energy / 1.75, 1.0) | intensity = min(lamp.energy / 1.75, 1.0) | ||||
| orientation = matrix_direction_neg_z(matrix) | orientation = matrix_direction_neg_z(matrix) | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<DirectionalLight ' % ident))) | fw('%s<DirectionalLight ' % ident))) | ||||
| fw('DEF=%s\n' % lamp_id) | fw('DEF=%s\n' % light_id) | ||||
| fw(ident_step + 'ambientIntensity="%.4f"\n' % amb_intensity) | fw(ident_step + 'ambientIntensity="%.4f"\n' % amb_intensity) | ||||
| fw(ident_step + 'color="%.4f %.4f %.4f"\n' % clamp_color(lamp.color)) | fw(ident_step + 'color="%.4f %.4f %.4f"\n' % clight_color(lamp.color)) | ||||
| fw(ident_step + 'intensity="%.4f"\n' % intensity) | fw(ident_step + 'intensity="%.4f"\n' % intensity) | ||||
| fw(ident_step + 'direction="%.4f %.4f %.4f"\n' % orientation) | fw(ident_step + 'direction="%.4f %.4f %.4f"\n' % orientation) | ||||
| fw(ident_step + '/>\n') | fw(ident_step + '/>\n') | ||||
| def writePointLight(ident, obj, matrix, lamp, world): | def writePointLight(ident, obj, matrix, lamp, world): | ||||
| # note, lamp_id is not re-used | # note, light_id is not re-used | ||||
| lamp_id = quoteattr(unique_name(obj, LA_ + obj.name, uuid_cache_lamp, clean_func=clean_def, sep="_")) | light_id = quoteattr(unique_name(obj, LA_ + obj.name, uuid_cache_light, clean_func=clean_def, sep="_")) | ||||
| if world: | if world: | ||||
| ambi = world.ambient_color | ambi = world.ambient_color | ||||
| # ambi = world.amb | # ambi = world.amb | ||||
| amb_intensity = ((float(ambi[0] + ambi[1] + ambi[2])) / 3.0) / 2.5 | amb_intensity = ((float(ambi[0] + ambi[1] + ambi[2])) / 3.0) / 2.5 | ||||
| else: | else: | ||||
| ambi = 0.0 | ambi = 0.0 | ||||
| amb_intensity = 0.0 | amb_intensity = 0.0 | ||||
| intensity = min(lamp.energy / 1.75, 1.0) | intensity = min(lamp.energy / 1.75, 1.0) | ||||
| location = matrix.to_translation()[:] | location = matrix.to_translation()[:] | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<PointLight ' % ident))) | fw('%s<PointLight ' % ident))) | ||||
| fw('DEF=%s\n' % lamp_id) | fw('DEF=%s\n' % light_id) | ||||
| fw(ident_step + 'ambientIntensity="%.4f"\n' % amb_intensity) | fw(ident_step + 'ambientIntensity="%.4f"\n' % amb_intensity) | ||||
| fw(ident_step + 'color="%.4f %.4f %.4f"\n' % clamp_color(lamp.color)) | fw(ident_step + 'color="%.4f %.4f %.4f"\n' % clight_color(lamp.color)) | ||||
| fw(ident_step + 'intensity="%.4f"\n' % intensity) | fw(ident_step + 'intensity="%.4f"\n' % intensity) | ||||
| fw(ident_step + 'radius="%.4f" \n' % lamp.distance) | fw(ident_step + 'radius="%.4f" \n' % lamp.distance) | ||||
| fw(ident_step + 'location="%.4f %.4f %.4f"\n' % location) | fw(ident_step + 'location="%.4f %.4f %.4f"\n' % location) | ||||
| fw(ident_step + '/>\n') | fw(ident_step + '/>\n') | ||||
| def writeIndexedFaceSet(ident, obj, mesh, matrix, world): | def writeIndexedFaceSet(ident, obj, mesh, matrix, world): | ||||
| obj_id = quoteattr(unique_name(obj, OB_ + obj.name, uuid_cache_object, clean_func=clean_def, sep="_")) | obj_id = quoteattr(unique_name(obj, OB_ + obj.name, uuid_cache_object, clean_func=clean_def, sep="_")) | ||||
| ▲ Show 20 Lines • Show All 495 Lines • ▼ Show 20 Lines | def writeMaterial(ident, material, world): | ||||
| if material.use_shadeless: | if material.use_shadeless: | ||||
| ambient = 1.0 | ambient = 1.0 | ||||
| shininess = 0.0 | shininess = 0.0 | ||||
| specColor = emitColor = diffuseColor | specColor = emitColor = diffuseColor | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<Material ' % ident))) | fw('%s<Material ' % ident))) | ||||
| fw('DEF=%s\n' % material_id) | fw('DEF=%s\n' % material_id) | ||||
| fw(ident_step + 'diffuseColor="%.3f %.3f %.3f"\n' % clamp_color(diffuseColor)) | fw(ident_step + 'diffuseColor="%.3f %.3f %.3f"\n' % clight_color(diffuseColor)) | ||||
| fw(ident_step + 'specularColor="%.3f %.3f %.3f"\n' % clamp_color(specColor)) | fw(ident_step + 'specularColor="%.3f %.3f %.3f"\n' % clight_color(specColor)) | ||||
| fw(ident_step + 'emissiveColor="%.3f %.3f %.3f"\n' % clamp_color(emitColor)) | fw(ident_step + 'emissiveColor="%.3f %.3f %.3f"\n' % clight_color(emitColor)) | ||||
| fw(ident_step + 'ambientIntensity="%.3f"\n' % ambient) | fw(ident_step + 'ambientIntensity="%.3f"\n' % ambient) | ||||
| fw(ident_step + 'shininess="%.3f"\n' % shininess) | fw(ident_step + 'shininess="%.3f"\n' % shininess) | ||||
| fw(ident_step + 'transparency="%s"\n' % transp) | fw(ident_step + 'transparency="%s"\n' % transp) | ||||
| fw(ident_step + '/>\n') | fw(ident_step + '/>\n') | ||||
| def writeMaterialH3D(ident, material, world, | def writeMaterialH3D(ident, material, world, | ||||
| obj, gpu_shader): | obj, gpu_shader): | ||||
| material_id = quoteattr(unique_name(material, 'MA_' + material.name, uuid_cache_material, clean_func=clean_def, sep="_")) | material_id = quoteattr(unique_name(material, 'MA_' + material.name, uuid_cache_material, clean_func=clean_def, sep="_")) | ||||
| Show All 14 Lines | def writeMaterialH3D(ident, material, world, | ||||
| #~ GPU_DATA_16F 7 | #~ GPU_DATA_16F 7 | ||||
| #~ GPU_DATA_1F 2 | #~ GPU_DATA_1F 2 | ||||
| #~ GPU_DATA_1I 1 | #~ GPU_DATA_1I 1 | ||||
| #~ GPU_DATA_2F 3 | #~ GPU_DATA_2F 3 | ||||
| #~ GPU_DATA_3F 4 | #~ GPU_DATA_3F 4 | ||||
| #~ GPU_DATA_4F 5 | #~ GPU_DATA_4F 5 | ||||
| #~ GPU_DATA_4UB 8 | #~ GPU_DATA_4UB 8 | ||||
| #~ GPU_DATA_9F 6 | #~ GPU_DATA_9F 6 | ||||
| #~ GPU_DYNAMIC_LAMP_DYNCO 7 | #~ GPU_DYNAMIC_LIGHT_DYNCO 7 | ||||
| #~ GPU_DYNAMIC_LAMP_DYNCOL 11 | #~ GPU_DYNAMIC_LIGHT_DYNCOL 11 | ||||
| #~ GPU_DYNAMIC_LAMP_DYNENERGY 10 | #~ GPU_DYNAMIC_LIGHT_DYNENERGY 10 | ||||
| #~ GPU_DYNAMIC_LAMP_DYNIMAT 8 | #~ GPU_DYNAMIC_LIGHT_DYNIMAT 8 | ||||
| #~ GPU_DYNAMIC_LAMP_DYNPERSMAT 9 | #~ GPU_DYNAMIC_LIGHT_DYNPERSMAT 9 | ||||
| #~ GPU_DYNAMIC_LAMP_DYNVEC 6 | #~ GPU_DYNAMIC_LIGHT_DYNVEC 6 | ||||
| #~ GPU_DYNAMIC_OBJECT_COLOR 5 | #~ GPU_DYNAMIC_OBJECT_COLOR 5 | ||||
| #~ GPU_DYNAMIC_OBJECT_IMAT 4 | #~ GPU_DYNAMIC_OBJECT_IMAT 4 | ||||
| #~ GPU_DYNAMIC_OBJECT_MAT 2 | #~ GPU_DYNAMIC_OBJECT_MAT 2 | ||||
| #~ GPU_DYNAMIC_OBJECT_VIEWIMAT 3 | #~ GPU_DYNAMIC_OBJECT_VIEWIMAT 3 | ||||
| #~ GPU_DYNAMIC_OBJECT_VIEWMAT 1 | #~ GPU_DYNAMIC_OBJECT_VIEWMAT 1 | ||||
| #~ GPU_DYNAMIC_SAMPLER_2DBUFFER 12 | #~ GPU_DYNAMIC_SAMPLER_2DBUFFER 12 | ||||
| #~ GPU_DYNAMIC_SAMPLER_2DIMAGE 13 | #~ GPU_DYNAMIC_SAMPLER_2DIMAGE 13 | ||||
| #~ GPU_DYNAMIC_SAMPLER_2DSHADOW 14 | #~ GPU_DYNAMIC_SAMPLER_2DSHADOW 14 | ||||
| ▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | def writeMaterialH3D(ident, material, world, | ||||
| for uniform in gpu_shader['uniforms']: | for uniform in gpu_shader['uniforms']: | ||||
| if uniform['type'] == gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE: | if uniform['type'] == gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE: | ||||
| field_descr = " <!--- Dynamic Sampler 2d Image -->" | field_descr = " <!--- Dynamic Sampler 2d Image -->" | ||||
| fw('%s<field name="%s" type="SFNode" accessType="inputOutput">%s\n' % (ident, uniform['varname'], field_descr)) | fw('%s<field name="%s" type="SFNode" accessType="inputOutput">%s\n' % (ident, uniform['varname'], field_descr)) | ||||
| writeImageTexture(ident + '\t', uniform['image']) | writeImageTexture(ident + '\t', uniform['image']) | ||||
| fw('%s</field>\n' % ident) | fw('%s</field>\n' % ident) | ||||
| elif uniform['type'] == gpu.GPU_DYNAMIC_LAMP_DYNCO: | elif uniform['type'] == gpu.GPU_DYNAMIC_LIGHT_DYNCO: | ||||
| lamp_obj = uniform['lamp'] | light_obj = uniform['lamp'] | ||||
| frag_uniform_var_map[uniform['varname']] = lamp_obj | frag_uniform_var_map[uniform['varname']] = light_obj | ||||
| if uniform['datatype'] == gpu.GPU_DATA_3F: # should always be true! | if uniform['datatype'] == gpu.GPU_DATA_3F: # should always be true! | ||||
| lamp_obj_id = quoteattr(unique_name(lamp_obj, LA_ + lamp_obj.name, uuid_cache_lamp, clean_func=clean_def, sep="_")) | light_obj_id = quoteattr(unique_name(light_obj, LA_ + light_obj.name, uuid_cache_light, clean_func=clean_def, sep="_")) | ||||
| lamp_obj_transform_id = quoteattr(unique_name(lamp_obj, lamp_obj.name, uuid_cache_object, clean_func=clean_def, sep="_")) | light_obj_transform_id = quoteattr(unique_name(light_obj, light_obj.name, uuid_cache_object, clean_func=clean_def, sep="_")) | ||||
| value = '%.6f %.6f %.6f' % (global_matrix * lamp_obj.matrix_world).to_translation()[:] | value = '%.6f %.6f %.6f' % (global_matrix * light_obj.matrix_world).to_translation()[:] | ||||
| field_descr = " <!--- Lamp DynCo '%s' -->" % lamp_obj.name | field_descr = " <!--- Lamp DynCo '%s' -->" % light_obj.name | ||||
| fw('%s<field name="%s" type="SFVec3f" accessType="inputOutput" value="%s" />%s\n' % (ident, uniform['varname'], value, field_descr)) | fw('%s<field name="%s" type="SFVec3f" accessType="inputOutput" value="%s" />%s\n' % (ident, uniform['varname'], value, field_descr)) | ||||
| # ------------------------------------------------------ | # ------------------------------------------------------ | ||||
| # shader-patch | # shader-patch | ||||
| field_descr = " <!--- Lamp DynCo '%s' (shader patch) -->" % lamp_obj.name | field_descr = " <!--- Lamp DynCo '%s' (shader patch) -->" % light_obj.name | ||||
| fw('%s<field name="%s_transform" type="SFMatrix4f" accessType="inputOutput" />%s\n' % (ident, uniform['varname'], field_descr)) | fw('%s<field name="%s_transform" type="SFMatrix4f" accessType="inputOutput" />%s\n' % (ident, uniform['varname'], field_descr)) | ||||
| # transform | # transform | ||||
| frag_vars.append("uniform mat4 %s_transform;" % uniform['varname']) | frag_vars.append("uniform mat4 %s_transform;" % uniform['varname']) | ||||
| h3d_material_route.append( | h3d_material_route.append( | ||||
| '<ROUTE fromNode=%s fromField="accumulatedForward" toNode=%s toField="%s_transform" />%s' % | '<ROUTE fromNode=%s fromField="accumulatedForward" toNode=%s toField="%s_transform" />%s' % | ||||
| (suffix_quoted_str(lamp_obj_transform_id, _TRANSFORM), material_id, uniform['varname'], field_descr)) | (suffix_quoted_str(light_obj_transform_id, _TRANSFORM), material_id, uniform['varname'], field_descr)) | ||||
| h3d_material_route.append( | h3d_material_route.append( | ||||
| '<ROUTE fromNode=%s fromField="location" toNode=%s toField="%s" /> %s' % | '<ROUTE fromNode=%s fromField="location" toNode=%s toField="%s" /> %s' % | ||||
| (lamp_obj_id, material_id, uniform['varname'], field_descr)) | (light_obj_id, material_id, uniform['varname'], field_descr)) | ||||
| # ------------------------------------------------------ | # ------------------------------------------------------ | ||||
| else: | else: | ||||
| assert(0) | assert(0) | ||||
| elif uniform['type'] == gpu.GPU_DYNAMIC_LAMP_DYNCOL: | elif uniform['type'] == gpu.GPU_DYNAMIC_LIGHT_DYNCOL: | ||||
| # odd we have both 3, 4 types. | # odd we have both 3, 4 types. | ||||
| lamp_obj = uniform['lamp'] | light_obj = uniform['lamp'] | ||||
| frag_uniform_var_map[uniform['varname']] = lamp_obj | frag_uniform_var_map[uniform['varname']] = light_obj | ||||
| lamp = lamp_obj.data | lamp = light_obj.data | ||||
| value = '%.6f %.6f %.6f' % (lamp.color * lamp.energy)[:] | value = '%.6f %.6f %.6f' % (lamp.color * lamp.energy)[:] | ||||
| field_descr = " <!--- Lamp DynColor '%s' -->" % lamp_obj.name | field_descr = " <!--- Lamp DynColor '%s' -->" % light_obj.name | ||||
| if uniform['datatype'] == gpu.GPU_DATA_3F: | if uniform['datatype'] == gpu.GPU_DATA_3F: | ||||
| fw('%s<field name="%s" type="SFVec3f" accessType="inputOutput" value="%s" />%s\n' % (ident, uniform['varname'], value, field_descr)) | fw('%s<field name="%s" type="SFVec3f" accessType="inputOutput" value="%s" />%s\n' % (ident, uniform['varname'], value, field_descr)) | ||||
| elif uniform['datatype'] == gpu.GPU_DATA_4F: | elif uniform['datatype'] == gpu.GPU_DATA_4F: | ||||
| fw('%s<field name="%s" type="SFVec4f" accessType="inputOutput" value="%s 1.0" />%s\n' % (ident, uniform['varname'], value, field_descr)) | fw('%s<field name="%s" type="SFVec4f" accessType="inputOutput" value="%s 1.0" />%s\n' % (ident, uniform['varname'], value, field_descr)) | ||||
| else: | else: | ||||
| assert(0) | assert(0) | ||||
| elif uniform['type'] == gpu.GPU_DYNAMIC_LAMP_DYNENERGY: | elif uniform['type'] == gpu.GPU_DYNAMIC_LIGHT_DYNENERGY: | ||||
| # not used ? | # not used ? | ||||
| assert(0) | assert(0) | ||||
| elif uniform['type'] == gpu.GPU_DYNAMIC_LAMP_DYNVEC: | elif uniform['type'] == gpu.GPU_DYNAMIC_LIGHT_DYNVEC: | ||||
| lamp_obj = uniform['lamp'] | light_obj = uniform['lamp'] | ||||
| frag_uniform_var_map[uniform['varname']] = lamp_obj | frag_uniform_var_map[uniform['varname']] = light_obj | ||||
| if uniform['datatype'] == gpu.GPU_DATA_3F: | if uniform['datatype'] == gpu.GPU_DATA_3F: | ||||
| lamp_obj = uniform['lamp'] | light_obj = uniform['lamp'] | ||||
| value = '%.6f %.6f %.6f' % ((global_matrix * lamp_obj.matrix_world).to_quaternion() * mathutils.Vector((0.0, 0.0, 1.0))).normalized()[:] | value = '%.6f %.6f %.6f' % ((global_matrix * light_obj.matrix_world).to_quaternion() * mathutils.Vector((0.0, 0.0, 1.0))).normalized()[:] | ||||
| field_descr = " <!--- Lamp DynDirection '%s' -->" % lamp_obj.name | field_descr = " <!--- Lamp DynDirection '%s' -->" % light_obj.name | ||||
| fw('%s<field name="%s" type="SFVec3f" accessType="inputOutput" value="%s" />%s\n' % (ident, uniform['varname'], value, field_descr)) | fw('%s<field name="%s" type="SFVec3f" accessType="inputOutput" value="%s" />%s\n' % (ident, uniform['varname'], value, field_descr)) | ||||
| # route so we can have the lamp update the view | # route so we can have the lamp update the view | ||||
| if h3d_is_object_view(scene, lamp_obj): | if h3d_is_object_view(scene, light_obj): | ||||
| lamp_id = quoteattr(unique_name(lamp_obj, LA_ + lamp_obj.name, uuid_cache_lamp, clean_func=clean_def, sep="_")) | light_id = quoteattr(unique_name(light_obj, LA_ + light_obj.name, uuid_cache_light, clean_func=clean_def, sep="_")) | ||||
| h3d_material_route.append( | h3d_material_route.append( | ||||
| '<ROUTE fromNode=%s fromField="direction" toNode=%s toField="%s" />%s' % | '<ROUTE fromNode=%s fromField="direction" toNode=%s toField="%s" />%s' % | ||||
| (lamp_id, material_id, uniform['varname'], field_descr)) | (light_id, material_id, uniform['varname'], field_descr)) | ||||
| else: | else: | ||||
| assert(0) | assert(0) | ||||
| elif uniform['type'] == gpu.GPU_DYNAMIC_OBJECT_VIEWIMAT: | elif uniform['type'] == gpu.GPU_DYNAMIC_OBJECT_VIEWIMAT: | ||||
| frag_uniform_var_map[uniform['varname']] = None | frag_uniform_var_map[uniform['varname']] = None | ||||
| if uniform['datatype'] == gpu.GPU_DATA_16F: | if uniform['datatype'] == gpu.GPU_DATA_16F: | ||||
| field_descr = " <!--- Object View Matrix Inverse '%s' -->" % obj.name | field_descr = " <!--- Object View Matrix Inverse '%s' -->" % obj.name | ||||
| ▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | def writeBackground(ident, world): | ||||
| if world is None: | if world is None: | ||||
| return | return | ||||
| # note, not re-used | # note, not re-used | ||||
| world_id = quoteattr(unique_name(world, WO_ + world.name, uuid_cache_world, clean_func=clean_def, sep="_")) | world_id = quoteattr(unique_name(world, WO_ + world.name, uuid_cache_world, clean_func=clean_def, sep="_")) | ||||
| blending = world.use_sky_blend, world.use_sky_paper, world.use_sky_real | blending = world.use_sky_blend, world.use_sky_paper, world.use_sky_real | ||||
| grd_triple = clamp_color(world.horizon_color) | grd_triple = clight_color(world.horizon_color) | ||||
| sky_triple = clamp_color(world.zenith_color) | sky_triple = clight_color(world.zenith_color) | ||||
| mix_triple = clamp_color((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3)) | mix_triple = clight_color((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3)) | ||||
| ident_step = ident + (' ' * (-len(ident) + \ | ident_step = ident + (' ' * (-len(ident) + \ | ||||
| fw('%s<Background ' % ident))) | fw('%s<Background ' % ident))) | ||||
| fw('DEF=%s\n' % world_id) | fw('DEF=%s\n' % world_id) | ||||
| # No Skytype - just Hor color | # No Skytype - just Hor color | ||||
| if blending == (False, False, False): | if blending == (False, False, False): | ||||
| fw(ident_step + 'groundColor="%.3f %.3f %.3f"\n' % grd_triple) | fw(ident_step + 'groundColor="%.3f %.3f %.3f"\n' % grd_triple) | ||||
| fw(ident_step + 'skyColor="%.3f %.3f %.3f"\n' % grd_triple) | fw(ident_step + 'skyColor="%.3f %.3f %.3f"\n' % grd_triple) | ||||
| ▲ Show 20 Lines • Show All 120 Lines • ▼ Show 20 Lines | def export_object(ident, obj_main_parent, obj_main, obj_children): | ||||
| # done | # done | ||||
| writeIndexedFaceSet(ident, obj, me, obj_matrix, world) | writeIndexedFaceSet(ident, obj, me, obj_matrix, world) | ||||
| # free mesh created with create_mesh() | # free mesh created with create_mesh() | ||||
| if do_remove: | if do_remove: | ||||
| bpy.data.meshes.remove(me) | bpy.data.meshes.remove(me) | ||||
| elif obj_type == 'LAMP': | elif obj_type == 'LIGHT': | ||||
| data = obj.data | data = obj.data | ||||
| datatype = data.type | datatype = data.type | ||||
| if datatype == 'POINT': | if datatype == 'POINT': | ||||
| writePointLight(ident, obj, obj_matrix, data, world) | writePointLight(ident, obj, obj_matrix, data, world) | ||||
| elif datatype == 'SPOT': | elif datatype == 'SPOT': | ||||
| writeSpotLight(ident, obj, obj_matrix, data, world) | writeSpotLight(ident, obj, obj_matrix, data, world) | ||||
| elif datatype == 'SUN': | elif datatype == 'SUN': | ||||
| writeDirectionalLight(ident, obj, obj_matrix, data, world) | writeDirectionalLight(ident, obj, obj_matrix, data, world) | ||||
| Show All 35 Lines | def export_main(): | ||||
| objects = [obj for obj in scene.objects if obj.is_visible(scene) and obj.select] | objects = [obj for obj in scene.objects if obj.is_visible(scene) and obj.select] | ||||
| else: | else: | ||||
| objects = [obj for obj in scene.objects if obj.is_visible(scene)] | objects = [obj for obj in scene.objects if obj.is_visible(scene)] | ||||
| print('Info: starting X3D export to %r...' % file.name) | print('Info: starting X3D export to %r...' % file.name) | ||||
| ident = '' | ident = '' | ||||
| ident = writeHeader(ident) | ident = writeHeader(ident) | ||||
| writeNavigationInfo(ident, scene, any(obj.type == 'LAMP' for obj in objects)) | writeNavigationInfo(ident, scene, any(obj.type == 'LIGHT' for obj in objects)) | ||||
| writeBackground(ident, world) | writeBackground(ident, world) | ||||
| writeFog(ident, world) | writeFog(ident, world) | ||||
| ident = '\t\t' | ident = '\t\t' | ||||
| if use_hierarchy: | if use_hierarchy: | ||||
| objects_hierarchy = build_hierarchy(objects) | objects_hierarchy = build_hierarchy(objects) | ||||
| else: | else: | ||||
| ▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines | |||||