Exemplo n.º 1
0
def getStateFromMaterial(prim_material, texture_cache, col_inst=None):
    state = RenderState.makeEmpty()
    
    mat = Material()
    texattr = TextureAttrib.makeAllOff()
    
    hasDiffuse = False
    if prim_material and prim_material.effect:
        
        diffuse = getattr(prim_material.effect, 'diffuse', None)
        transparent = getattr(prim_material.effect, 'transparent', None)
        if isinstance(diffuse, collada.material.Map) and isinstance(transparent, collada.material.Map):
            if diffuse.sampler.surface.image == transparent.sampler.surface.image:
                #some exporters put the same map in the diffuse channel
                # and the transparent channel when they don't really mean to
                transparent = None
        
        if isinstance(diffuse, collada.material.Map) or isinstance(transparent, collada.material.Map):
            diffuseMap = None
            transparentMap = None
            diffuseInitColor = None
            if isinstance(diffuse, collada.material.Map):
                diffuseMap = diffuse
            else:
                diffuseInitColor = v4fromtuple(diffuse)
            if isinstance(transparent, collada.material.Map):
                transparentMap = transparent
            if diffuseMap == transparentMap:
                transparentMap = None
                
            diffuseTexture = getTexture(color=diffuseMap, alpha=transparentMap, texture_cache=texture_cache, diffuseinit=diffuseInitColor)
            texattr = addTextureStage('tsDiff', TextureStage.MModulate, texattr, diffuseTexture)
            hasDiffuse = True

        if type(diffuse) is tuple:
            mat.setDiffuse(v4fromtuple(diffuse))
        
        # hack to look for sketchup version < 8 where transparency was exported flipped
        # also ColladaMaya v2.03b had this same issue
        flip_alpha = False
        if col_inst and col_inst.assetInfo:
            for contributor in col_inst.assetInfo.contributors:
                tool_name = contributor.authoring_tool
                if tool_name is None:
                    continue
                split = tool_name.split()
                if len(split) == 3 and \
                      split[0].strip().lower() == 'google' and \
                      split[1].strip().lower() == 'sketchup':
                    version = split[2].strip().split('.')
                    try:
                        major_version = int(version[0])
                        if major_version < 8:
                            flip_alpha = True
                    except (ValueError, TypeError):
                        continue
                    
                try:
                    collada_maya_idx = split.index('ColladaMaya')
                    if split[collada_maya_idx + 1] == 'v2.03b':
                        flip_alpha = True
                except (ValueError, IndexError):
                    continue
        
        if type(transparent) is tuple:
            trR, trG, trB = transparent[0], transparent[1], transparent[2]
            trA = transparent[3] if len(transparent) > 3 else 1.0
        else:
            trR, trG, trB = 1.0, 1.0, 1.0
            trA = 1.0
        
        transparency = getattr(prim_material.effect, 'transparency', 1.0)
        if transparency is None:
            transparency = 1.0
        a_one = prim_material.effect.opaque_mode == collada.material.OPAQUE_MODE.A_ONE
        if a_one:
            alphaR = alphaG = alphaB = alphaA = transparency * trA
        else:
            alphaR = transparency * trR
            alphaG = transparency * trG
            alphaB = transparency * trB
            alphaA = luminance([trR, trG, trB])
            flip_alpha = not flip_alpha
        
        if flip_alpha:
            alphaR = 1.0 - alphaR
            alphaG = 1.0 - alphaG
            alphaB = 1.0 - alphaB
            alphaA = 1.0 - alphaA
        
        if alphaA < 1.0:
            state = state.addAttrib(ColorScaleAttrib.make(VBase4(alphaR, alphaG, alphaB, alphaA)))
        
        emission = getattr(prim_material.effect, 'emission', None)
        if isinstance(emission, collada.material.Map):
            emissionTexture = getTexture(alpha=emission, texture_cache=texture_cache)
            texattr = addTextureStage('tsEmiss', TextureStage.MGlow, texattr, emissionTexture)
        elif type(emission) is tuple:
            mat.setEmission(v4fromtuple(emission))
        
        ambient = getattr(prim_material.effect, 'ambient', None)
        if type(ambient) is tuple:
            mat.setAmbient(v4fromtuple(ambient))
        
        specular = getattr(prim_material.effect, 'specular', None)
        if isinstance(specular, collada.material.Map):
            specularTexture = getTexture(color=specular, texture_cache=texture_cache)
            texattr = addTextureStage('tsSpec', TextureStage.MGloss, texattr, specularTexture)
            mat.setSpecular(VBase4(0.1, 0.1, 0.1, 1.0))
        elif type(specular) is tuple:
            mat.setSpecular(v4fromtuple(specular))

        shininess = getattr(prim_material.effect, 'shininess', None)
        #this sets a sane value for blinn shading
        if shininess <= 1.0:
            if shininess < 0.01:
                shininess = 1.0
            shininess = shininess * 128.0
        mat.setShininess(shininess)

        bumpmap = getattr(prim_material.effect, 'bumpmap', None)
        if isinstance(bumpmap, collada.material.Map):
            bumpTexture = getTexture(color=bumpmap, texture_cache=texture_cache)
            texattr = addTextureStage('tsBump', TextureStage.MNormal, texattr, bumpTexture)

        if prim_material.effect.double_sided:
            state = state.addAttrib(CullFaceAttrib.make(CullFaceAttrib.MCullNone))

    if hasDiffuse:
        state = state.addAttrib(DepthOffsetAttrib.make(1))
    
    state = state.addAttrib(MaterialAttrib.make(mat))
    state = state.addAttrib(texattr)
    
    return state
Exemplo n.º 2
0
def getStateFromMaterial(prim_material, texture_cache, col_inst=None):
    state = RenderState.makeEmpty()

    mat = Material()
    texattr = TextureAttrib.makeAllOff()

    hasDiffuse = False
    if prim_material and prim_material.effect:

        diffuse = getattr(prim_material.effect, 'diffuse', None)
        transparent = getattr(prim_material.effect, 'transparent', None)
        if isinstance(diffuse, collada.material.Map) and isinstance(
                transparent, collada.material.Map):
            if diffuse.sampler.surface.image == transparent.sampler.surface.image:
                #some exporters put the same map in the diffuse channel
                # and the transparent channel when they don't really mean to
                transparent = None

        if isinstance(diffuse, collada.material.Map) or isinstance(
                transparent, collada.material.Map):
            diffuseMap = None
            transparentMap = None
            diffuseInitColor = None
            if isinstance(diffuse, collada.material.Map):
                diffuseMap = diffuse
            else:
                diffuseInitColor = v4fromtuple(diffuse)
            if isinstance(transparent, collada.material.Map):
                transparentMap = transparent
            if diffuseMap == transparentMap:
                transparentMap = None

            diffuseTexture = getTexture(color=diffuseMap,
                                        alpha=transparentMap,
                                        texture_cache=texture_cache,
                                        diffuseinit=diffuseInitColor)
            texattr = addTextureStage('tsDiff', TextureStage.MModulate,
                                      texattr, diffuseTexture)
            hasDiffuse = True

        if type(diffuse) is tuple:
            mat.setDiffuse(v4fromtuple(diffuse))

        # hack to look for sketchup version < 8 where transparency was exported flipped
        # also ColladaMaya v2.03b had this same issue
        flip_alpha = False
        if col_inst and col_inst.assetInfo:
            for contributor in col_inst.assetInfo.contributors:
                tool_name = contributor.authoring_tool
                if tool_name is None:
                    continue
                split = tool_name.split()
                if len(split) == 3 and \
                      split[0].strip().lower() == 'google' and \
                      split[1].strip().lower() == 'sketchup':
                    version = split[2].strip().split('.')
                    try:
                        major_version = int(version[0])
                        if major_version < 8:
                            flip_alpha = True
                    except (ValueError, TypeError):
                        continue

                try:
                    collada_maya_idx = split.index('ColladaMaya')
                    if split[collada_maya_idx + 1] == 'v2.03b':
                        flip_alpha = True
                except (ValueError, IndexError):
                    continue

        if type(transparent) is tuple:
            trR, trG, trB = transparent[0], transparent[1], transparent[2]
            trA = transparent[3] if len(transparent) > 3 else 1.0
        else:
            trR, trG, trB = 1.0, 1.0, 1.0
            trA = 1.0

        transparency = getattr(prim_material.effect, 'transparency', 1.0)
        if transparency is None:
            transparency = 1.0
        a_one = prim_material.effect.opaque_mode == collada.material.OPAQUE_MODE.A_ONE
        if a_one:
            alphaR = alphaG = alphaB = alphaA = transparency * trA
        else:
            alphaR = transparency * trR
            alphaG = transparency * trG
            alphaB = transparency * trB
            alphaA = luminance([trR, trG, trB])
            flip_alpha = not flip_alpha

        if flip_alpha:
            alphaR = 1.0 - alphaR
            alphaG = 1.0 - alphaG
            alphaB = 1.0 - alphaB
            alphaA = 1.0 - alphaA

        if alphaA < 1.0:
            state = state.addAttrib(
                ColorScaleAttrib.make(VBase4(alphaR, alphaG, alphaB, alphaA)))

        emission = getattr(prim_material.effect, 'emission', None)
        if isinstance(emission, collada.material.Map):
            emissionTexture = getTexture(alpha=emission,
                                         texture_cache=texture_cache)
            texattr = addTextureStage('tsEmiss', TextureStage.MGlow, texattr,
                                      emissionTexture)
        elif type(emission) is tuple:
            mat.setEmission(v4fromtuple(emission))

        ambient = getattr(prim_material.effect, 'ambient', None)
        if type(ambient) is tuple:
            mat.setAmbient(v4fromtuple(ambient))

        specular = getattr(prim_material.effect, 'specular', None)
        if isinstance(specular, collada.material.Map):
            specularTexture = getTexture(color=specular,
                                         texture_cache=texture_cache)
            texattr = addTextureStage('tsSpec', TextureStage.MGloss, texattr,
                                      specularTexture)
            mat.setSpecular(VBase4(0.1, 0.1, 0.1, 1.0))
        elif type(specular) is tuple:
            mat.setSpecular(v4fromtuple(specular))

        shininess = getattr(prim_material.effect, 'shininess', None)
        #this sets a sane value for blinn shading
        if shininess <= 1.0:
            if shininess < 0.01:
                shininess = 1.0
            shininess = shininess * 128.0
        mat.setShininess(shininess)

        bumpmap = getattr(prim_material.effect, 'bumpmap', None)
        if isinstance(bumpmap, collada.material.Map):
            bumpTexture = getTexture(color=bumpmap,
                                     texture_cache=texture_cache)
            texattr = addTextureStage('tsBump', TextureStage.MNormal, texattr,
                                      bumpTexture)

        if prim_material.effect.double_sided:
            state = state.addAttrib(
                CullFaceAttrib.make(CullFaceAttrib.MCullNone))

    if hasDiffuse:
        state = state.addAttrib(DepthOffsetAttrib.make(1))

    state = state.addAttrib(MaterialAttrib.make(mat))
    state = state.addAttrib(texattr)

    return state
Exemplo n.º 3
0
def getStateFromMaterial(prim_material):
    state = RenderState.makeFullDefault()
    
    emission = None
    ambient = None
    diffuse = None
    specular = None
    shininess = None
    reflection = None
    reflectivity = None
    
    if prim_material:
        for prop in prim_material.supported:
            value = getattr(prim_material, prop)
            
            if value is None:
                continue
            
            if type(value) is tuple:
                val4 = value[3] if len(value) > 3 else 1.0
                value = VBase4(value[0], value[1], value[2], val4)
            
            if isinstance(value, collada.material.Map):
                texture_file = value.sampler.surface.image.path
                if not texture_file is None:
                    (root, leaf) = os.path.split(sys.argv[1])
                    tex_absolute = os.path.join(root, texture_file)
                    myImage = PNMImage()
                    myImage.read(Filename(tex_absolute))
                    myTexture = Texture(texture_file)
                    myTexture.load(myImage)
                    state = state.addAttrib(TextureAttrib.make(myTexture))
            elif prop == 'emission':
                emission = value
            elif prop == 'ambient':
                ambient = value
            elif prop == 'diffuse':
                diffuse = value
            elif prop == 'specular':
                specular = value
            elif prop == 'shininess':
                shininess = value
            elif prop == 'reflective':
                reflective = value
            elif prop == 'reflectivity':
                reflectivity = value
            elif prop == 'transparent':
                pass
            elif prop == 'transparency':
                pass
            else:
                raise
    
    mat = Material()
    
    if not emission is None:
        mat.setEmission(emission)
    if not ambient is None:
        mat.setAmbient(ambient)
    if not diffuse is None:
        mat.setDiffuse(diffuse)
    if not specular is None:
        mat.setSpecular(specular)
    if not shininess is None:
        mat.setShininess(shininess)
        
    state = state.addAttrib(MaterialAttrib.make(mat))
    return state