Example #1
0
    void Check()
    {
        if (tex == null)
        {
            return;
        }
        for (int i = 0; i < sceneItem.items.Count; i++)
        {
            if (itemState[i])
            {
                ItemInfoJson itemInfo = sceneItem.items[i];
                BaseItem     baseItem = getBaseItemByID(itemInfo.itemID);
                for (int j = 0; j < itemInfo.itemPos.Count; j++)
                {
                    ItemPosInfoJson posInfo = itemInfo.itemPos[j];
                    Object          obj     = AssetDatabase.LoadAssetAtPath("Assets/" + baseItem.model + ".prefab", typeof(Object));
                    GameObject      gameObj = Instantiate(obj) as GameObject;
                    gameObj.transform.position    = new Vector3(posInfo.pos.x, posInfo.pos.y, posInfo.pos.z);
                    gameObj.transform.eulerAngles = new Vector3(posInfo.rotate.x, posInfo.rotate.y, posInfo.rotate.z);
                    gameObj.name = itemInfo.itemName;

                    ObjLightMapData mapdata = objLightData.groups[i].objLightDatas[j];
                    MeshRenderer    render  = gameObj.GetComponent <MeshRenderer>();
                    render.lightmapIndex       = (int)mapdata.lightIndex;
                    render.lightmapScaleOffset = new Vector4(mapdata.tilingX, mapdata.tilingY, mapdata.offsetX, mapdata.offsetY);
                    //MColor[] mc = mapdata.color;
                    //creatNewLightMap(mapdata,tex);
                }
            }
        }
    }
    /// <summary>
    /// 生成单类物体的光照信息  多个位置
    /// </summary>
    /// <param name="objs"></param>
    /// <param name="id"></param>
    static ObjLightMapDataGroup createObjLightData(EditorSceneObj objs, long id, Texture2D lightTex)
    {
        ObjLightMapDataGroup dataGroup = new ObjLightMapDataGroup();

        dataGroup.id = id;
        List <ObjLightMapData> objPosMapData = new List <ObjLightMapData>();

        for (int i = 0; i < objs.itemObj.Count; i++)
        {
            GameObject      posObj  = objs.itemObj[i];
            ObjLightMapData mapdata = getObjLightMapData(posObj, lightTex);
            objPosMapData.Add(mapdata);
        }
        dataGroup.objLightDatas = objPosMapData.ToArray();
        return(dataGroup);
    }
    static List <MColor> CreateObjLighting(ObjLightMapData light, Texture2D lightTex)
    {
        if (light == null)
        {
            return(null);
        }
        if (lightTex == null)
        {
            return(null);
        }
        int lightWidth = lightTex.width;
        int lightHeigh = lightTex.height;

        int startX = (int)(Mathf.Abs(light.offsetX) * lightWidth);
        int startY = (int)(Mathf.Abs(light.offsetY) * lightHeigh);
        int scaleX = (int)(light.tilingX * lightWidth);
        int scaleY = (int)(light.tilingY * lightHeigh);

        List <MColor> colors = new List <MColor>();
        int           width  = scaleX;
        int           heigh  = scaleY;
        Texture2D     tex    = new Texture2D(width, heigh);

        for (int i = startX; i < startX + scaleX; i++)
        {
            for (int j = startY; j < startY + scaleY; j++)
            {
                Color c = lightTex.GetPixel(i, j);
                tex.SetPixel(i - startX, j - startY, c);
                //colors.Add(new MColor(c));
            }
        }
        tex.Apply();
        byte[]       texByte = tex.EncodeToPNG();
        FileStream   file    = File.Open(Application.dataPath + "/testLight.png", FileMode.Create);
        BinaryWriter writer  = new BinaryWriter(file);

        writer.Write(texByte);
        file.Close();
        writer.Close();
        return(colors);
    }
Example #4
0
    void creatNewLightMap(ObjLightMapData mapdata, Texture2D tex)
    {
        int lightWidth = tex.width;
        int lightHeigh = tex.height;
        int startX     = (int)(Mathf.Abs(mapdata.offsetX) * lightWidth);
        int startY     = (int)(Mathf.Abs(mapdata.offsetY) * lightHeigh);
        int scaleX     = (int)(mapdata.tilingX * lightWidth);
        int scaleY     = (int)(mapdata.tilingY * lightHeigh);

        for (int i = startX; i < startX + scaleX; i++)
        {
            for (int j = startY; j < startY + scaleY; j++)
            {
                int    index = (i - startX) * scaleY + (j - startY);
                MColor mc    = mapdata.color[index];
                tex.SetPixel(i, j, new Color(mc.r, mc.g, mc.b));
            }
        }
        tex.Apply();
    }
    private static ObjLightMapData getObjLightMapData(GameObject obj, Texture2D lightTex)
    {
        MeshRenderer render = obj.GetComponent <MeshRenderer>();

        if (render == null)
        {
            return(null);
        }
        ObjLightMapData objdata = new ObjLightMapData();

        objdata.lightIndex = render.lightmapIndex;
        objdata.tilingX    = render.lightmapScaleOffset.x;
        objdata.tilingY    = render.lightmapScaleOffset.y;
        objdata.offsetX    = render.lightmapScaleOffset.z;
        objdata.offsetY    = render.lightmapScaleOffset.w;
        List <MColor> sceneColors = CreateObjLighting(objdata, lightTex);

        if (sceneColors == null)
        {
            return(objdata);
        }
        objdata.color = sceneColors.ToArray();
        return(objdata);
    }