diff --git a/bbp_ng/PROP_virtools_material.py b/bbp_ng/PROP_virtools_material.py index 1336cd8..d994036 100644 --- a/bbp_ng/PROP_virtools_material.py +++ b/bbp_ng/PROP_virtools_material.py @@ -1,5 +1,6 @@ import bpy -from . import UTIL_virtools_types +import typing +from . import UTIL_virtools_types, UTIL_functions # todo: # some properties are not set default value @@ -8,176 +9,10 @@ from . import UTIL_virtools_types # export default from RawVirtoolsMaterial. upgrade the level of RawVirtoolsMaterial. move up # then BBP_PG_virtools_material use the default value provided by RawVirtoolsMaterial -class BBP_PG_virtools_material(bpy.types.PropertyGroup): - ambient: bpy.props.FloatVectorProperty( - name = "Ambient", - subtype = 'COLOR', - min = 0.0, - max = 1.0, - size = 3, - default = (0.3, 0.3, 0.3) - ) - - diffuse: bpy.props.FloatVectorProperty( - name = "Diffuse", - subtype = 'COLOR_GAMMA', - min = 0.0, - max = 1.0, - size = 4, - default = (0.7, 0.7, 0.7, 1.0) - ) - - specular: bpy.props.FloatVectorProperty( - name = "Specular", - subtype = 'COLOR', - min = 0.0, - max = 1.0, - size = 3, - default = (0.5, 0.5, 0.5) - ) - - emissive: bpy.props.FloatVectorProperty( - name = "Emissive", - subtype = 'COLOR', - min = 0.0, - max = 1.0, - size = 3, - default = (0.0, 0.0, 0.0) - ) - - specular_power: bpy.props.FloatProperty( - name = "Specular Power", - min = 0.0, - max = 100.0, - default = 0.0, - ) - - texture: bpy.props.PointerProperty( - type = bpy.types.Image, - name = "Texture" - ) - - texture_border_color: bpy.props.FloatVectorProperty( - name = "Texture Border Color", - subtype = 'COLOR', - min = 0.0, - max = 1.0, - size = 3, - default = (0.0, 0.0, 0.0) - ) - - texture_blend_mode: bpy.props.EnumProperty( - name = "Texture Blend Mode", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXTEXTURE_BLENDMODE, - UTIL_virtools_types.g_Annotation_VXTEXTURE_BLENDMODE - ) - ) - - texture_min_mode: bpy.props.EnumProperty( - name = "Texture Min Mode", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXTEXTURE_FILTERMODE, - UTIL_virtools_types.g_Annotation_VXTEXTURE_FILTERMODE - ) - ) - - texture_mag_mode: bpy.props.EnumProperty( - name = "Texture Mag Mode", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXTEXTURE_FILTERMODE, - UTIL_virtools_types.g_Annotation_VXTEXTURE_FILTERMODE - ) - ) - - texture_address_mode: bpy.props.EnumProperty( - name = "Texture Address Mode", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXTEXTURE_ADDRESSMODE, - UTIL_virtools_types.g_Annotation_VXTEXTURE_ADDRESSMODE - ) - ) - - source_blend: bpy.props.EnumProperty( - name = "Source Blend", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXBLEND_MODE, - UTIL_virtools_types.g_Annotation_VXBLEND_MODE - ) - ) - - dest_blend: bpy.props.EnumProperty( - name = "Dest Blend", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXBLEND_MODE, - UTIL_virtools_types.g_Annotation_VXBLEND_MODE - ) - ) - - fill_mode: bpy.props.EnumProperty( - name = "Fill Mode", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXFILL_MODE, - UTIL_virtools_types.g_Annotation_VXFILL_MODE - ) - ) - - shade_mode: bpy.props.EnumProperty( - name = "Shade Mode", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXSHADE_MODE, - UTIL_virtools_types.g_Annotation_VXSHADE_MODE - ) - ) - - enable_alpha_test: bpy.props.BoolProperty( - name = "Enable Alpha Test", - default = False, - ) - enable_alpha_blend: bpy.props.BoolProperty( - name = "Enable Alpha Blend", - default = False, - ) - enable_perspective_correction: bpy.props.BoolProperty( - name = "Enable Perspective Correction", - default = False, - ) - enable_zwrite: bpy.props.BoolProperty( - name = "Enable ZWrite", - default = True, - ) - enable_two_sided: bpy.props.BoolProperty( - name = "Enable Two Sided", - default = False, - ) - - alpha_ref: bpy.props.IntProperty( - name = "Alpha Ref", - min = 0, - max = 255, - default = 0, - ) - - alpha_func: bpy.props.EnumProperty( - name = "Alpha Func", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXCMPFUNC, - UTIL_virtools_types.g_Annotation_VXCMPFUNC - ) - ) - - z_func: bpy.props.EnumProperty( - name = "Z Func", - items = UTIL_virtools_types.generate_blender_enum_prop_entries( - UTIL_virtools_types.VXCMPFUNC, - UTIL_virtools_types.g_Annotation_VXCMPFUNC - ) - ) - - - - class RawVirtoolsMaterial(): + + # Instance Member Declarations + mDiffuse: UTIL_virtools_types.VxColor mAmbient: UTIL_virtools_types.VxColor mSpecular: UTIL_virtools_types.VxColor @@ -207,36 +42,424 @@ class RawVirtoolsMaterial(): mAlphaFunc: UTIL_virtools_types.VXCMPFUNC mZFunc: UTIL_virtools_types.VXCMPFUNC + # Default Value Declarations + + cDefaultDiffuse: typing.ClassVar[UTIL_virtools_types.VxColor] = UTIL_virtools_types.VxColor(0.7, 0.7, 0.7, 1.0) + cDefaultAmbient: typing.ClassVar[UTIL_virtools_types.VxColor] = UTIL_virtools_types.VxColor(0.3, 0.3, 0.3, 1.0) + cDefaultSpecular: typing.ClassVar[UTIL_virtools_types.VxColor] = UTIL_virtools_types.VxColor(0.5, 0.5, 0.5, 1.0) + cDefaultEmissive: typing.ClassVar[UTIL_virtools_types.VxColor] = UTIL_virtools_types.VxColor(0.0, 0.0, 0.0, 1.0) + cDefaultSpecularPower: typing.ClassVar[float] = 0.0 + + cDefaultTexture: typing.ClassVar[bpy.types.Texture | None] = None + cDefaultTextureBorderColor: typing.ClassVar[UTIL_virtools_types.VxColor] = UTIL_virtools_types.VxColor(0.0, 0.0, 0.0, 0.0) + + cDefaultTextureBlendMode: typing.ClassVar[UTIL_virtools_types.VXTEXTURE_BLENDMODE]= UTIL_virtools_types.VXTEXTURE_BLENDMODE.VXTEXTUREBLEND_MODULATEALPHA + cDefaultTextureMinMode: typing.ClassVar[UTIL_virtools_types.VXTEXTURE_FILTERMODE] = UTIL_virtools_types.VXTEXTURE_FILTERMODE.VXTEXTUREFILTER_LINEAR + cDefaultTextureMagMode: typing.ClassVar[UTIL_virtools_types.VXTEXTURE_FILTERMODE] = UTIL_virtools_types.VXTEXTURE_FILTERMODE.VXTEXTUREFILTER_LINEAR + cDefaultTextureAddressMode: typing.ClassVar[UTIL_virtools_types.VXTEXTURE_ADDRESSMODE] = UTIL_virtools_types.VXTEXTURE_ADDRESSMODE.VXTEXTURE_ADDRESSWRAP + + cDefaultSourceBlend: typing.ClassVar[UTIL_virtools_types.VXBLEND_MODE] = UTIL_virtools_types.VXBLEND_MODE.VXBLEND_ONE + cDefaultDestBlend: typing.ClassVar[UTIL_virtools_types.VXBLEND_MODE] = UTIL_virtools_types.VXBLEND_MODE.VXBLEND_ZERO + cDefaultFillMode: typing.ClassVar[UTIL_virtools_types.VXFILL_MODE] = UTIL_virtools_types.VXFILL_MODE.VXFILL_SOLID + cDefaultShadeMode: typing.ClassVar[UTIL_virtools_types.VXSHADE_MODE] = UTIL_virtools_types.VXSHADE_MODE.VXSHADE_GOURAUD + + cDefaultEnableAlphaTest: typing.ClassVar[bool] = False + cDefaultEnableAlphaBlend: typing.ClassVar[bool] = False + cDefaultEnablePerspectiveCorrection: typing.ClassVar[bool] = True + cDefaultEnableZWrite: typing.ClassVar[bool] = True + cDefaultEnableTwoSided: typing.ClassVar[bool] = False + + cDefaultAlphaRef: typing.ClassVar[int] = 0 + cDefaultAlphaFunc: typing.ClassVar[UTIL_virtools_types.VXCMPFUNC] = UTIL_virtools_types.VXCMPFUNC.VXCMP_ALWAYS + cDefaultZFunc: typing.ClassVar[UTIL_virtools_types.VXCMPFUNC] = UTIL_virtools_types.VXCMPFUNC.VXCMP_LESSEQUAL + def __init__(self): # assign default value for each component - self.mDiffuse = (0.7, 0.7, 0.7, 1.0) - self.mAmbient = (0.3, 0.3, 0.3, 1.0) - self.mSpecular = (0.5, 0.5, 0.5, 1.0) - self.mSpecularPower = 0.0 - self.mEmissive = (0.0, 0.0, 0.0, 1.0) - self.mEnableTwoSided = False - self.mTexture = None - self.mTextureMinMode = UTIL_virtools_types.VXTEXTURE_FILTERMODE.VXTEXTUREFILTER_LINEAR - self.mTextureMagMode = UTIL_virtools_types.VXTEXTURE_FILTERMODE.VXTEXTUREFILTER_LINEAR - self.mSourceBlend = UTIL_virtools_types.VXBLEND_MODE.VXBLEND_ONE - self.mDestBlend = UTIL_virtools_types.VXBLEND_MODE.VXBLEND_ZERO - self.mEnableAlphaBlend = False - self.mShadeMode = UTIL_virtools_types.VXSHADE_MODE.VXSHADE_GOURAUD - self.mFillMode = UTIL_virtools_types.VXFILL_MODE.VXFILL_SOLID - self.mEnableAlphaTest = False - self.mEnableZWrite = True + self.mDiffuse = RawVirtoolsMaterial.cDefaultDiffuse.clone() + self.mAmbient = RawVirtoolsMaterial.cDefaultAmbient.clone() + self.mSpecular = RawVirtoolsMaterial.cDefaultSpecular.clone() + self.mSpecularPower = RawVirtoolsMaterial.cDefaultSpecularPower + self.mEmissive = RawVirtoolsMaterial.cDefaultEmissive.clone() + self.mEnableTwoSided = RawVirtoolsMaterial.cDefaultEnableTwoSided + self.mTexture = RawVirtoolsMaterial.cDefaultTexture + self.mTextureMinMode = RawVirtoolsMaterial.cDefaultTextureMinMode + self.mTextureMagMode = RawVirtoolsMaterial.cDefaultTextureMagMode + self.mSourceBlend = RawVirtoolsMaterial.cDefaultSourceBlend + self.mDestBlend = RawVirtoolsMaterial.cDefaultDestBlend + self.mEnableAlphaBlend = RawVirtoolsMaterial.cDefaultEnableAlphaBlend + self.mShadeMode = RawVirtoolsMaterial.cDefaultShadeMode + self.mFillMode = RawVirtoolsMaterial.cDefaultFillMode + self.mEnableAlphaTest = RawVirtoolsMaterial.cDefaultEnableAlphaTest + self.mEnableZWrite = RawVirtoolsMaterial.cDefaultEnableZWrite - self.mEnablePerspectiveCorrection = True - self.mTextureBlendMode = UTIL_virtools_types.VXTEXTURE_BLENDMODE.VXTEXTUREBLEND_MODULATEALPHA - self.mTextureAddressMode = UTIL_virtools_types.VXTEXTURE_ADDRESSMODE.VXTEXTURE_ADDRESSWRAP - self.mZFunc = UTIL_virtools_types.VXCMPFUNC.VXCMP_LESSEQUAL - self.mAlphaFunc = UTIL_virtools_types.VXCMPFUNC.VXCMP_ALWAYS - self.mTextureBorderColor = (0.0, 0.0, 0.0, 0.0) - self.mAlphaRef = 0 + self.mEnablePerspectiveCorrection = RawVirtoolsMaterial.cDefaultEnablePerspectiveCorrection + self.mTextureBlendMode = RawVirtoolsMaterial.cDefaultTextureBlendMode + self.mTextureAddressMode = RawVirtoolsMaterial.cDefaultTextureAddressMode + self.mZFunc = RawVirtoolsMaterial.cDefaultZFunc + self.mAlphaFunc = RawVirtoolsMaterial.cDefaultAlphaFunc + self.mTextureBorderColor = RawVirtoolsMaterial.cDefaultTextureBorderColor.clone() + self.mAlphaRef = RawVirtoolsMaterial.cDefaultAlphaRef - def from_blender_prop(self): - pass + def regulate(self): + # regulate colors + self.mDiffuse.regulate() + self.mAmbient.regulate() + self.mSpecular.regulate() + self.mEmissive.regulate() + self.mTextureBorderColor.regulate() + # only diffuse and texture border color can have alpha component + self.mAmbient.a = 1.0 + self.mSpecular.a = 1.0 + self.mEmissive.a = 1.0 + + # alpha ref limit + self.mAlphaRef = UTIL_functions.clamp_int(self.mAlphaRef, 0, 255) + + # specular power + self.mSpecularPower = UTIL_functions.clamp_float(self.mSpecularPower, 0.0, 100.0) + +class BBP_PG_virtools_material(bpy.types.PropertyGroup): + ambient: bpy.props.FloatVectorProperty( + name = "Ambient", + description = "Ambient color of the material", + subtype = 'COLOR', + min = 0.0, + max = 1.0, + size = 3, + default = RawVirtoolsMaterial.cDefaultAmbient.to_tuple_rgb() + ) + diffuse: bpy.props.FloatVectorProperty( + name = "Diffuse", + description = "Diffuse color of the material", + subtype = 'COLOR_GAMMA', + min = 0.0, + max = 1.0, + size = 4, + default = RawVirtoolsMaterial.cDefaultDiffuse.to_tuple_rgba() + ) - def to_blender_prop(self): - pass + specular: bpy.props.FloatVectorProperty( + name = "Specular", + description = "Specular color of the material", + subtype = 'COLOR', + min = 0.0, + max = 1.0, + size = 3, + default = RawVirtoolsMaterial.cDefaultSpecular.to_tuple_rgb() + ) + + emissive: bpy.props.FloatVectorProperty( + name = "Emissive", + description = "Emissive color of the material", + subtype = 'COLOR', + min = 0.0, + max = 1.0, + size = 3, + default = RawVirtoolsMaterial.cDefaultEmissive.to_tuple_rgb() + ) + + specular_power: bpy.props.FloatProperty( + name = "Power", + description = "Specular highlight power", + min = 0.0, + max = 100.0, + default = RawVirtoolsMaterial.cDefaultSpecularPower + ) + + texture: bpy.props.PointerProperty( + type = bpy.types.Image, + name = "Texture", + description = "Texture of the material" + ) + + texture_border_color: bpy.props.FloatVectorProperty( + name = "Border Color", + description = "The border color is used when the texture address mode is VXTEXTURE_ADDRESSBORDER.", + subtype = 'COLOR_GAMMA', + min = 0.0, + max = 1.0, + size = 4, + default = RawVirtoolsMaterial.cDefaultTextureBorderColor.to_tuple_rgba() + ) + + texture_blend_mode: bpy.props.EnumProperty( + name = "Texture Blend", + description = "Texture blend mode", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXTEXTURE_BLENDMODE, + UTIL_virtools_types.g_Annotation_VXTEXTURE_BLENDMODE + ), + default = RawVirtoolsMaterial.cDefaultTextureBlendMode.value + ) + + texture_min_mode: bpy.props.EnumProperty( + name = "Filter Min", + description = "Texture filter mode when the texture is minified", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXTEXTURE_FILTERMODE, + UTIL_virtools_types.g_Annotation_VXTEXTURE_FILTERMODE + ), + default = RawVirtoolsMaterial.cDefaultTextureMinMode.value + ) + + texture_mag_mode: bpy.props.EnumProperty( + name = "Filter Mag", + description = "Texture filter mode when the texture is magnified", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXTEXTURE_FILTERMODE, + UTIL_virtools_types.g_Annotation_VXTEXTURE_FILTERMODE + ), + default = RawVirtoolsMaterial.cDefaultTextureMagMode.value + ) + + texture_address_mode: bpy.props.EnumProperty( + name = "Address Mode", + description = "The address mode controls how the texture coordinates outside the range 0..1", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXTEXTURE_ADDRESSMODE, + UTIL_virtools_types.g_Annotation_VXTEXTURE_ADDRESSMODE + ), + default = RawVirtoolsMaterial.cDefaultTextureAddressMode.value + ) + + source_blend: bpy.props.EnumProperty( + name = "Source Blend", + description = "Source blend factor", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXBLEND_MODE, + UTIL_virtools_types.g_Annotation_VXBLEND_MODE + ), + default = RawVirtoolsMaterial.cDefaultSourceBlend.value + ) + + dest_blend: bpy.props.EnumProperty( + name = "Destination Blend", + description = "Destination blend factor", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXBLEND_MODE, + UTIL_virtools_types.g_Annotation_VXBLEND_MODE + ), + default = RawVirtoolsMaterial.cDefaultDestBlend.value + ) + + fill_mode: bpy.props.EnumProperty( + name = "Fill Mode", + description = "Fill mode", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXFILL_MODE, + UTIL_virtools_types.g_Annotation_VXFILL_MODE + ), + default = RawVirtoolsMaterial.cDefaultFillMode.value + ) + + shade_mode: bpy.props.EnumProperty( + name = "Shade Mode", + description = "Shade mode", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXSHADE_MODE, + UTIL_virtools_types.g_Annotation_VXSHADE_MODE + ), + default = RawVirtoolsMaterial.cDefaultShadeMode.value + ) + + enable_alpha_test: bpy.props.BoolProperty( + name = "Alpha Test", + description = "Whether the alpha test is enabled", + default = RawVirtoolsMaterial.cDefaultEnableAlphaTest + ) + enable_alpha_blend: bpy.props.BoolProperty( + name = "Blend", + description = "Whether alpha blending is enabled or not.", + default = RawVirtoolsMaterial.cDefaultEnableAlphaBlend + ) + enable_perspective_correction: bpy.props.BoolProperty( + name = "Perspective Correction", + description = "Whether texture perspective correction is enabled", + default = RawVirtoolsMaterial.cDefaultEnablePerspectiveCorrection + ) + enable_z_write: bpy.props.BoolProperty( + name = "Z-Buffer Write", + description = "Whether writing in ZBuffer is enabled.", + default = RawVirtoolsMaterial.cDefaultEnableZWrite + ) + enable_two_sided: bpy.props.BoolProperty( + name = "Both Sided", + description = "Whether the material is both sided or not", + default = RawVirtoolsMaterial.cDefaultEnableTwoSided + ) + + alpha_ref: bpy.props.IntProperty( + name = "Alpha Ref Value", + description = "Alpha referential value", + min = 0, + max = 255, + default = RawVirtoolsMaterial.cDefaultAlphaRef + ) + + alpha_func: bpy.props.EnumProperty( + name = "Alpha Test Function", + description = "Alpha comparision function", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXCMPFUNC, + UTIL_virtools_types.g_Annotation_VXCMPFUNC + ), + default = RawVirtoolsMaterial.cDefaultAlphaFunc.value + ) + + z_func: bpy.props.EnumProperty( + name = "Z Compare Function", + description = "Z Comparison function", + items = UTIL_virtools_types.generate_blender_enum_prop_entries( + UTIL_virtools_types.VXCMPFUNC, + UTIL_virtools_types.g_Annotation_VXCMPFUNC + ), + default = RawVirtoolsMaterial.cDefaultZFunc.value + ) + +def get_virtools_material(mtl: bpy.types.Material) -> BBP_PG_virtools_material: + return mtl.virtools_material + +def get_raw_virtools_material(mtl: bpy.types.Material) -> RawVirtoolsMaterial: + props: BBP_PG_virtools_material = get_virtools_material(mtl) + rawdata: RawVirtoolsMaterial = RawVirtoolsMaterial() + + rawdata.mDiffuse.from_tuple_rgba(props.diffuse) + rawdata.mAmbient.from_tuple_rgb(props.ambient) + rawdata.mSpecular.from_tuple_rgb(props.specular) + rawdata.mEmissive.from_tuple_rgb(props.emissive) + rawdata.mSpecularPower = props.specular_power + + rawdata.mTexture = props.texture + rawdata.mTextureBorderColor.from_tuple_rgba(props.texture_border_color) + + rawdata.mTextureBlendMode = int(props.texture_blend_mode) + rawdata.mTextureMinMode = int(props.texture_min_mode) + rawdata.mTextureMagMode = int(props.texture_mag_mode) + rawdata.mTextureAddressMode = int(props.texture_address_mode) + + rawdata.mSourceBlend = int(props.source_blend) + rawdata.mDestBlend = int(props.dest_blend) + rawdata.mFillMode = int(props.fill_mode) + rawdata.mShadeMode = int(props.shade_mode) + + rawdata.mEnableAlphaTest = props.enable_alpha_test + rawdata.mEnableAlphaBlend = props.enable_alpha_blend + rawdata.mEnablePerspectiveCorrection = props.enable_perspective_correction + rawdata.mEnableZWrite = props.enable_z_write + rawdata.mEnableTwoSided = props.enable_two_sided + + rawdata.mAlphaRef = props.alpha_ref + rawdata.mAlphaFunc = int(props.alpha_func) + rawdata.mZFunc = int(props.z_func) + + rawdata.regulate() + return rawdata + +def set_raw_virtools_material(mtl: bpy.types.Material, rawdata: RawVirtoolsMaterial) -> None: + props: BBP_PG_virtools_material = get_virtools_material(mtl) + + props.diffuse = rawdata.mDiffuse.to_tuple_rgba() + props.ambient = rawdata.mAmbient.to_tuple_rgb() + props.specular = rawdata.mSpecular.to_tuple_rgb() + props.emissive = rawdata.mEmissive.to_tuple_rgb() + props.specular_power = rawdata.mSpecularPower + + props.texture = rawdata.mTexture + props.texture_border_color = rawdata.mTextureBorderColor.to_tuple_rgba() + + props.texture_blend_mode = str(rawdata.mTextureBlendMode) + props.texture_min_mode = str(rawdata.mTextureMinMode) + props.texture_mag_mode = str(rawdata.mTextureMagMode) + props.texture_address_mode = str(rawdata.mTextureAddressMode) + + props.source_blend = str(rawdata.mSourceBlend) + props.dest_blend = str(rawdata.mDestBlend) + props.fill_mode = str(rawdata.mFillMode) + props.shade_mode = str(rawdata.mShadeMode) + + props.enable_alpha_test = rawdata.mEnableAlphaTest + props.enable_alpha_blend = rawdata.mEnableAlphaBlend + props.enable_perspective_correction = rawdata.mEnablePerspectiveCorrection + props.enable_z_write = rawdata.mEnableZWrite + props.enable_two_sided = rawdata.mEnableTwoSided + + props.alpha_ref = rawdata.mAlphaRef + props.alpha_func = str(rawdata.mAlphaFunc) + props.z_func = str(rawdata.mZFunc) + +class BBP_PT_virtools_material(bpy.types.Panel): + """Show Virtools Material Properties.""" + bl_label = "Virtools Material" + bl_idname = "BBP_PT_virtools_material" + bl_space_type = 'PROPERTIES' + bl_region_type = 'WINDOW' + bl_context = "material" + + @classmethod + def poll(cls, context): + return context.material is not None + + def draw(self, context): + # get layout and target + layout = self.layout + props: BBP_PG_virtools_material = get_virtools_material(context.material) + + # draw layout + row = layout.row() + row.label(text="Color Parameters") + #row.operator(BALLANCE_OT_preset_virtools_material.bl_idname, text="", icon="PRESET") + layout.prop(props, 'ambient') + layout.prop(props, 'diffuse') + layout.prop(props, 'specular') + layout.prop(props, 'emissive') + layout.prop(props, 'specular_power') + + layout.separator() + layout.label(text="Mode Parameters") + layout.prop(props, 'enable_two_sided') + layout.prop(props, 'fill_mode') + layout.prop(props, 'shade_mode') + + layout.separator() + layout.label(text="Texture Parameters") + layout.prop(props, 'texture', emboss = True) + layout.prop(props, 'texture_blend_mode') + layout.prop(props, 'texture_min_mode') + layout.prop(props, 'texture_mag_mode') + layout.prop(props, 'texture_address_mode') + layout.prop(props, 'enable_perspective_correction') + if (int(props.texture_address_mode) == UTIL_virtools_types.VXTEXTURE_ADDRESSMODE.VXTEXTURE_ADDRESSBORDER.value): + layout.prop(props, 'texture_border_color') + + layout.separator() + layout.label(text="Alpha Test Parameters") + layout.prop(props, 'enable_alpha_test') + if props.enable_alpha_test: + layout.prop(props, 'alpha_func') + layout.prop(props, 'alpha_ref') + + layout.separator() + layout.label(text="Alpha Blend Parameters") + layout.prop(props, 'enable_alpha_blend') + if props.enable_alpha_blend: + layout.prop(props, 'source_blend') + layout.prop(props, 'dest_blend') + + layout.separator() + layout.label(text="Z Write Parameters") + layout.prop(props, 'enable_z_write') + if props.enable_z_write: + layout.prop(props, 'z_func') + + layout.separator() + layout.label(text="Operations") + #layout.operator(BALLANCE_OT_apply_virtools_material.bl_idname, icon="NODETREE") + #layout.operator(BALLANCE_OT_parse_virtools_material.bl_idname, icon="HIDE_OFF") + + +def register_prop(): + bpy.types.Material.virtools_material = bpy.props.PointerProperty(type = BBP_PG_virtools_material) + +def unregister_prop(): + del bpy.types.Material.virtools_material diff --git a/bbp_ng/UTIL_functions.py b/bbp_ng/UTIL_functions.py new file mode 100644 index 0000000..12ac043 --- /dev/null +++ b/bbp_ng/UTIL_functions.py @@ -0,0 +1,38 @@ +import math + +class BBPException(Exception): + """ + The exception thrown by Ballance Blender Plugin + """ + pass + +def clamp_float(v: float, min_val: float, max_val: float) -> float: + """! + @brief Clamp a float value + + @param v[in] The value need to be clamp. + @param min_val[in] The allowed minium value, including self. + @param max_val[in] The allowed maxium value, including self. + @return Clamped value. + """ + if (max_val < min_val): raise BBPException("Invalid range of clamp_float().") + + if (v < min_val): return min_val + elif (v > max_val): return max_val + else: return v + +def clamp_int(v: int, min_val: int, max_val: int) -> int: + """! + @brief Clamp a int value + + @param v[in] The value need to be clamp. + @param min_val[in] The allowed minium value, including self. + @param max_val[in] The allowed maxium value, including self. + @return Clamped value. + """ + if (max_val < min_val): raise BBPException("Invalid range of clamp_int().") + + if (v < min_val): return min_val + elif (v > max_val): return max_val + else: return v + diff --git a/bbp_ng/UTIL_virtools_types.py b/bbp_ng/UTIL_virtools_types.py index d3cf54e..c70a6f0 100644 --- a/bbp_ng/UTIL_virtools_types.py +++ b/bbp_ng/UTIL_virtools_types.py @@ -1,7 +1,45 @@ import typing, enum +from . import UTIL_functions + +class VxColor(): + """ + The Color struct support RGBA. + """ + a: float + r: float + g: float + b: float + def __init__(self, _r: float, _g: float, _b: float, _a: float): + self.a = _a + self.r = _r + self.g = _g + self.b = _b + self.regulate() + + def to_tuple_rgba(self) -> tuple[float, float, float, float]: + return (self.r, self.g, self.b, self.a) + + def to_tuple_rgb(self) -> tuple[float, float, float]: + return (self.r, self.g, self.b) + + def from_tuple_rgba(self, val: tuple[float, float, float, float]) -> None: + (self.r, self.g, self.b, self.a) = val + self.regulate() + + def from_tuple_rgb(self, val: tuple[float, float, float]) -> None: + (self.r, self.g, self.b) = val + self.a = 1.0 + self.regulate() + + def clone(self): + return VxColor(self.r, self.g, self.b, self.a) + + def regulate(self): + self.a = UTIL_functions.clamp_float(self.a, 0.0, 1.0) + self.r = UTIL_functions.clamp_float(self.r, 0.0, 1.0) + self.g = UTIL_functions.clamp_float(self.g, 0.0, 1.0) + self.b = UTIL_functions.clamp_float(self.b, 0.0, 1.0) -##! Color with RGBA order. -VxColor = tuple[float, float, float, float] class VXTEXTURE_BLENDMODE(enum.IntEnum): """! @@ -158,5 +196,5 @@ BlenderEnumPropEntry_t = tuple[str, str, str, str | int, int] def generate_blender_enum_prop_entries(enum_data: InheritingIntEnum_t, anno: dict[int, str]) -> tuple[BlenderEnumPropEntry_t, ...]: # token, display name, descriptions, icon, index return tuple( - (member.name, member.name, anno.get(member.value, ""), "", member.value) for member in enum_data + (str(member.value), member.name, anno.get(member.value, ""), "", member.value) for member in enum_data ) diff --git a/bbp_ng/__init__.py b/bbp_ng/__init__.py index 53b9271..23210a8 100644 --- a/bbp_ng/__init__.py +++ b/bbp_ng/__init__.py @@ -23,6 +23,7 @@ if "bpy" in locals(): #endregion +from . import PROP_virtools_material from . import OP_UV_flatten_uv #region Menu @@ -54,6 +55,9 @@ def menu_drawer_view3d(self, context): g_Classes: tuple[typing.Any, ...] = ( OP_UV_flatten_uv.BBP_OT_flatten_uv, BBP_MT_View3DMenu, + + PROP_virtools_material.BBP_PG_virtools_material, + PROP_virtools_material.BBP_PT_virtools_material, ) class MenuEntry(): @@ -73,6 +77,9 @@ def register() -> None: for cls in g_Classes: bpy.utils.register_class(cls) + # register properties + PROP_virtools_material.register_prop() + # add menu drawer for entry in g_Menus: if entry.mIsAppend: @@ -85,6 +92,9 @@ def unregister() -> None: for entry in g_Menus: entry.mContainerMenu.remove(entry.mMenuDrawer) + # unregister properties + PROP_virtools_material.unregister_prop() + # unregister classes for cls in g_Classes: bpy.utils.unregister_class(cls)