Skip to content
Snippets Groups Projects
shading.py 59.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Maurice Raybaud's avatar
    Maurice Raybaud committed
    
        if texturesDif == "" and mater.pov.replacement_text == "":
            if texturesAlpha != "":
                # POV-Ray "scale" is not a number of repetitions factor, but its inverse,
                # a standard scale factor.
                # Offset seems needed relatively to scale so probably center of the scale
                # is not the same in blender and POV
                # Strange that the translation factor for scale is not the same as for
                # translate.
                # TODO: verify both matches with blender internal.
                mappingAlpha = imgMapTransforms(t_alpha)
                # mappingAlpha = " translate <%.4g,%.4g,%.4g> scale <%.4g,%.4g,%.4g>\n" % \
                               # (-t_alpha.offset.x, -t_alpha.offset.y, t_alpha.offset.z,
                                # 1.0 / t_alpha.scale.x, 1.0 / t_alpha.scale.y,
                                # 1.0 / t_alpha.scale.z)
                if texturesAlpha and texturesAlpha.startswith("PAT_"):
                    tabWrite("function{f%s(x,y,z).transmit}\n" %texturesAlpha) 
                else:
                    tabWrite("pigment {pigment_pattern {uv_mapping image_map" \
                             "{%s \"%s\" %s}%s}\n" % \
                             (imageFormat(texturesAlpha), texturesAlpha, imgMap(t_alpha),
                              mappingAlpha))
                tabWrite("pigment_map {\n")
                tabWrite("[0 color rgbft<0,0,0,1,1>]\n")
                tabWrite("[1 color rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>]\n" % \
                         (col[0], col[1], col[2], povFilter, trans))
                tabWrite("}\n")
                tabWrite("}\n")
    
            else:
                tabWrite("pigment {rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>}\n" % \
                         (col[0], col[1], col[2], povFilter, trans))
                         
                                        
            if texturesSpec != "":
                # Level 3 is full specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=3)))
                
            elif colored_specular_found:
                # Level 1 is no specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=1)))
    
            else:
                # Level 2 is translated specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=2)))
    
        elif mater.pov.replacement_text == "":
            mappingDif = imgMapTransforms(t_dif)
            # mappingDif = ("scale <%.4g,%.4g,%.4g> translate <%.4g,%.4g,%.4g>" % \
                          # ( 1.0 / t_dif.scale.x, 
                          # 1.0 / t_dif.scale.y,
                          # 1.0 / t_dif.scale.z, 
                          # 0.5-(0.5/t_dif.scale.x) + t_dif.offset.x,
                          # 0.5-(0.5/t_dif.scale.y) + t_dif.offset.y,
                          # 0.5-(0.5/t_dif.scale.z) + t_dif.offset.z))
            if texturesAlpha != "":
                # Strange that the translation factor for scale is not the same as for
                # translate.
                # TODO: verify both matches with blender internal.
                mappingAlpha = imgMapTransforms(t_alpha)
                # mappingAlpha = "translate <%.4g,%.4g,%.4g> scale <%.4g,%.4g,%.4g>" % \
                               # (-t_alpha.offset.x, -t_alpha.offset.y, t_alpha.offset.z,
                                # 1.0 / t_alpha.scale.x, 1.0 / t_alpha.scale.y,
                                # 1.0 / t_alpha.scale.z)
                if texturesAlpha and texturesAlpha.startswith("PAT_"):
                    tabWrite("pigment{pigment_pattern {function{f%s(x,y,z).transmit}}\n" %texturesAlpha)
                else:
                    tabWrite("pigment {pigment_pattern {uv_mapping image_map" \
                             "{%s \"%s\" %s}%s}\n" % \
                             (imageFormat(texturesAlpha), texturesAlpha, imgMap(t_alpha),
                              mappingAlpha))
                tabWrite("pigment_map {\n")
                tabWrite("[0 color rgbft<0,0,0,1,1>]\n")
                if texturesAlpha and texturesAlpha.startswith("PAT_"):
                    tabWrite("[1 function{f%s(x,y,z).transmit}]\n" %texturesAlpha) 
                elif texturesDif and not texturesDif.startswith("PAT_"):                                       
                    tabWrite("[1 uv_mapping image_map {%s \"%s\" %s} %s]\n" % \
                             (imageFormat(texturesDif), texturesDif,
                              (imgMap(t_dif) + imgGamma), mappingDif))
                elif texturesDif and texturesDif.startswith("PAT_"):
                    tabWrite("[1 %s]\n" %texturesDif)
                tabWrite("}\n")
                tabWrite("}\n")
    
            else:
                if texturesDif and texturesDif.startswith("PAT_"):
                    tabWrite("pigment{%s}\n" %texturesDif) 
                else:                                    
                    tabWrite("pigment {\n")
                    tabWrite("uv_mapping image_map {\n")
                    #tabWrite("%s \"%s\" %s}%s\n" % \
                    #         (imageFormat(texturesDif), texturesDif,
                    #         (imgGamma + imgMap(t_dif)),mappingDif))
                    tabWrite("%s \"%s\" \n" % (imageFormat(texturesDif), texturesDif))
                    tabWrite("%s\n" % (imgGamma + imgMap(t_dif)))
                    tabWrite("}\n")
                    tabWrite("%s\n" % mappingDif)
                    tabWrite("}\n")
                  
            if texturesSpec != "":
                # Level 3 is full specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=3)))                  
            else:
                # Level 2 is translated specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=2)))
    
            ## scale 1 rotate y*0
            #imageMap = ("{image_map {%s \"%s\" %s }" % \
            #            (imageFormat(textures), textures,imgMap(t_dif)))
    
            #tabWrite("\n\t\t\tuv_mapping pigment %s} %s finish {%s}" % \
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            #           (imageMap, mapping, safety(material_finish)))
    
            #tabWrite("\n\t\t\tpigment {uv_mapping image_map " \
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            #           "{%s \"%s\" %s}%s} finish {%s}" % \
            #           (imageFormat(texturesDif), texturesDif,imgMap(t_dif),
            #            mappingDif, safety(material_finish)))
        if texturesNorm != "" and mater.pov.replacement_text == "":
            ## scale 1 rotate y*0
            # POV-Ray "scale" is not a number of repetitions factor, but its inverse,
            # a standard scale factor.
            # Offset seems needed relatively to scale so probably center of the scale is
            # not the same in blender and POV
            mappingNor =imgMapTransforms(t_nor)
            # mappingNor = (" translate <%.4g,%.4g,%.4g> scale <%.4g,%.4g,%.4g>" % \
                          # (-t_nor.offset.x, -t_nor.offset.y, t_nor.offset.z,
                           # 1.0 / t_nor.scale.x, 1.0 / t_nor.scale.y, 1.0 / t_nor.scale.z))
            #imageMapNor = ("{bump_map {%s \"%s\" %s mapping}" % \
            #               (imageFormat(texturesNorm),texturesNorm,imgMap(t_nor)))
            #We were not using the above maybe we should?
            if texturesNorm and texturesNorm.startswith("PAT_"):
                tabWrite("normal{function{f%s(x,y,z).grey} bump_size %.4g}\n" %(texturesNorm, t_nor.normal_factor * 10))
            else:                                    
                tabWrite("normal {uv_mapping bump_map {%s \"%s\" %s  bump_size %.4g }%s}\n" % \
                         (imageFormat(texturesNorm), texturesNorm, imgMap(t_nor),
                          t_nor.normal_factor * 10.0, mappingNor))
        if texturesSpec != "" and mater.pov.replacement_text == "":
            tabWrite("]\n")
    
            tabWrite("}\n")
    
        #End of slope/ior texture_map
        if mater.diffuse_shader == 'MINNAERT' and mater.pov.replacement_text == "":
            tabWrite("]\n")
            tabWrite("}\n")
        if mater.diffuse_shader == 'FRESNEL' and mater.pov.replacement_text == "":
            c = 1
            while (c <= lampCount):
                tabWrite("]\n")
                tabWrite("}\n")
                c += 1
    
          
                
        # Close first layer of POV "texture" (Blender material)
        tabWrite("}\n")
        
        if (mater.specular_color.s > 0.0):
            colored_specular_found = True
        else:
            colored_specular_found = False
            
        # Write another layered texture using invisible diffuse and metallic trick 
        # to emulate colored specular highlights
        special_texture_found = False
        for t in mater.texture_slots:
            if(t and t.use and ((t.texture.type == 'IMAGE' and t.texture.image) or t.texture.type != 'IMAGE') and
               (t.use_map_specular or t.use_map_raymir)):
                # Specular mapped textures would conflict with colored specular
                # because POV can't layer over or under pigment patterned textures
                special_texture_found = True
        
        if colored_specular_found and not special_texture_found:
            if comments:
    
                tabWrite("  // colored highlights with a stransparent metallic layer\n")
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            else:
                tabWrite("\n")
        
            tabWrite("texture {\n")
            tabWrite("pigment {rgbft<%.3g, %.3g, %.3g, 0, 1>}\n" % \
                             (mater.specular_color[0], mater.specular_color[1], mater.specular_color[2]))
            tabWrite("finish {%s}\n" % (safety(material_finish, Level=2))) # Level 2 is translated spec
    
            texturesNorm = ""
            for t in mater.texture_slots:
    
                if t and t.texture.pov.tex_pattern_type != 'emulator':
                    proceduralFlag=True
                    image_filename = string_strip_hyphen(bpy.path.clean_name(t.texture.name))
                if (t and t.texture.type == 'IMAGE' and
                        t.use and t.texture.image and
                        t.texture.pov.tex_pattern_type == 'emulator'):
                    proceduralFlag=False 
                    image_filename = path_image(t.texture.image)
                    imgGamma = ""
                    if image_filename:
                        if t.use_map_normal:
                            texturesNorm = image_filename
                            # colvalue = t.normal_factor * 10.0  # UNUSED
                            #textNormName=t.texture.image.name + ".normal"
                            #was the above used? --MR
                            t_nor = t
                            if proceduralFlag:
                                tabWrite("normal{function" \
                                         "{f%s(x,y,z).grey} bump_size %.4g}\n" % \
                                         (texturesNorm,
                                         t_nor.normal_factor * 10))
                            else:
                                tabWrite("normal {uv_mapping bump_map " \
                                         "{%s \"%s\" %s  bump_size %.4g }%s}\n" % \
                                         (imageFormat(texturesNorm),
                                         texturesNorm, imgMap(t_nor),
                                         t_nor.normal_factor * 10,
                                         mappingNor))
                                          
            tabWrite("}\n") # THEN IT CAN CLOSE LAST LAYER OF TEXTURE