Search Unity

how to assign a normal map to a material and SetTextureScale with AssetPostprocessor

Discussion in 'Scripting' started by Monil, May 6, 2012.

  1. Monil

    Monil

    Joined:
    Apr 24, 2012
    Posts:
    102
    I have to create a script that assigns the normal map during the 'import FBX files. In 3ds max I have the object with custom attributes, such as $ Box01.CA.NormalMapName = "rock_NM", $ Box01.CA.Shader = "DiffuseMapNormal", when importing the FBX file in unity custom attributes are recognized, but I can not to add a normal map to the material and I can not change SetTextureScale to a normal map. My value of TextureScale is similar to 0.008196721.

    Code (csharp):
    1. import System;
    2. import System.IO;
    3.  
    4. class MaxToUnityPostprocessor extends AssetPostprocessor {
    5.  
    6.     var shaderDiffuseMapNormal : Shader = Shader.Find("Kreative3D/DiffuseMapNormal");
    7.  
    8.     function OnPostprocessGameObjectWithUserProperties (go : GameObject, propNames : String[], values : System.Object[]){
    9.  
    10.         for (var i : int = 0; i < propNames.Length; i++) {
    11.  
    12.             var propName : String = propNames[i];
    13.  
    14.             var value : Object = values[i];
    15.  
    16.             //Debug.Log("Propname: " + propName + " value: " +values[i]);
    17.  
    18.                if(propName == (go.name + ".ParametriOggetto.Shader")  value.GetType() == String){
    19.  
    20.               var sShader : String = value;
    21.  
    22.                  if(go.GetComponent(MeshRenderer)){
    23.  
    24.                    if(sShader == "DiffuseMapNormal"){
    25.  
    26.                     go.renderer.sharedMaterial.shader = shaderDiffuseMapNormal;
    27.  
    28.                     }
    29.                    }
    30.               }
    31.  
    32.           if(propName == (go.name + ".ParametriOggetto.NormalMapName")  value.GetType() == String){
    33.  
    34.               var sNormalMapName : String = value;
    35.  
    36.               //Debug.Log( go.name + ".NormalMapName = " + sNormalMapName);
    37.  
    38.                  if(sNormalMapName != ""){
    39.  
    40.                    if(go.GetComponent(MeshRenderer)){
    41.  
    42.                     if(go.renderer.sharedMaterial.HasProperty("_BumpMap")){
    43.  
    44.                         var NormalMapTextureOffset : Vector2;
    45.  
    46.                         if(go.renderer.sharedMaterial.GetTexture("_BumpMap") != null){
    47.  
    48.                            Debug.Log( go.name + " have a normal map" );
    49.  
    50.                            } else {
    51.  
    52.                              Debug.Log( go.name + " does not have a normal map");
    53.  
    54.                              var myfloat1 : float = go.renderer.sharedMaterial.GetTextureScale("_MainTex")[0];
    55.                              var myfloat2 : float = go.renderer.sharedMaterial.GetTextureScale("_MainTex")[1];
    56.  
    57.                              Debug.Log(myfloat1 + " " + myfloat2);
    58.  
    59.                              NormalMapTextureOffset.Set(myfloat1,myfloat2);
    60.  
    61.                              Debug.Log(NormalMapTextureOffset);
    62.  
    63.                              go.renderer.sharedMaterial.SetTextureScale("_BumpMap",NormalMapTextureOffset);
    64.  
    65.                              Debug.Log( go.name + " mainTextureScale : " + NormalMapTextureOffset);
    66.                              Debug.Log( go.name + " mainTextureScaleX : " + NormalMapTextureOffset[0]);
    67.                              Debug.Log( go.name + " mainTextureScaleY : " + NormalMapTextureOffset[1]);
    68.  
    69.                              Debug.Log( go.name + " GetTextureScale : " + go.renderer.sharedMaterial.GetTextureScale("_BumpMap"));
    70.                              Debug.Log( go.name + " GetTextureScaleX : " + go.renderer.sharedMaterial.GetTextureScale("_BumpMap")[0]);
    71.                              Debug.Log( go.name + " GetTextureScaleY : " + go.renderer.sharedMaterial.GetTextureScale("_BumpMap")[1]);
    72.  
    73.                              var MaterialePrincipale : Material = go.renderer.sharedMaterial;
    74.                              var shaderMaterialePrincipale : String = MaterialePrincipale.shader.name;
    75.                              var materialTemp = new Material (Shader.Find(shaderMaterialePrincipale));
    76.                              var IndirizzoTexturePrincipale = AssetDatabase.GetAssetPath(MaterialePrincipale.mainTexture);
    77.                              var IndirizzoTextureNormal  = Path.GetDirectoryName (IndirizzoTexturePrincipale);
    78.                              IndirizzoTextureNormal += "/" + sNormalMapName;
    79.                              var LoadTextureNormal : Texture2D =  AssetDatabase.LoadAssetAtPath(IndirizzoTextureNormal, typeof(Texture));                    
    80.                              materialTemp.mainTextureScale = MaterialePrincipale.mainTextureScale;
    81.                              materialTemp.mainTexture = MaterialePrincipale.mainTexture;
    82.                              materialTemp.SetColor("_Color", MaterialePrincipale.GetColor("_Color"));
    83.                              materialTemp.SetTexture("_MainTex", MaterialePrincipale.GetTexture("_MainTex"));
    84.                              materialTemp.SetTexture("_BumpMap", LoadTextureNormal);
    85.                              materialTemp.SetTextureOffset("_MainTex", MaterialePrincipale.GetTextureOffset("_MainTex"));
    86.                              materialTemp.SetTextureOffset("_BumpMap", MaterialePrincipale.GetTextureOffset("_MainTex"));
    87.                              materialTemp.SetFloat("_SelfIllumination", MaterialePrincipale.GetFloat("_SelfIllumination"));
    88.                                AssetDatabase.CreateAsset(materialTemp, "Assets/MaxToUnity/MaterialsTemp/" + MaterialePrincipale.name + "_MaterialTemp.mat");
    89.  
    90.                            }
    91.                     }
    92.                    }
    93.                  }
    94.           }    
    95.           }
    96.     }
    97.  
    98. }
     
    Last edited: May 6, 2012