コード例 #1
0
ファイル: InputData.cs プロジェクト: sbojceska/Unify
        private void GetLights()
        {
            LightTable        allLights = doc.Lights;
            List <UnifyLight> lightList = new List <UnifyLight>();

            foreach (LightObject light in allLights)
            {
                UnifyLight unifyObj = new UnifyLight();

                unifyObj.ObjType    = "LightObject";
                unifyObj.LightType  = light.LightGeometry.LightStyle.ToString();
                unifyObj.Guid       = light.Id;
                unifyObj.Diffuse    = Utility.ColorToString(light.LightGeometry.Diffuse);
                unifyObj.UniqueName = light.LightGeometry.LightStyle.ToString() + "-" + light.Id.ToString();
                unifyObj.Intensity  = light.LightGeometry.Intensity;

                if (light.LightGeometry.LightStyle == Rhino.Geometry.LightStyle.WorldRectangular)
                {
                    // adjust location, rhino reports location at lower left corner
                    // unity reports location at center of the area/rectangle
                    Rhino.Geometry.Point3d loc = new Rhino.Geometry.Point3d(
                        light.LightGeometry.Location.X + (light.LightGeometry.Length.Length * 0.5),
                        light.LightGeometry.Location.Y + (light.LightGeometry.Width.Length * 0.5),
                        light.LightGeometry.Location.Z);
                    unifyObj.Location = loc.ToString();

                    // create target from location + direction
                    Rhino.Geometry.Transform xf     = Rhino.Geometry.Transform.Translation(light.LightGeometry.Direction);
                    Rhino.Geometry.Point3d   target = loc;
                    loc.Transform(xf);
                    unifyObj.Target = target.ToString();
                }
                else
                {
                    // create target from location + direction
                    Rhino.Geometry.Transform xf     = Rhino.Geometry.Transform.Translation(light.LightGeometry.Direction);
                    Rhino.Geometry.Point3d   target = light.LightGeometry.Location;
                    target.Transform(xf);
                    unifyObj.Target = target.ToString();

                    unifyObj.Location = light.LightGeometry.Location.ToString();
                }

                unifyObj.Range           = light.LightGeometry.Direction.Length;
                unifyObj.SpotAngle       = light.LightGeometry.SpotAngleRadians;
                unifyObj.ShadowIntensity = light.LightGeometry.SpotLightShadowIntensity;
                unifyObj.Width           = light.LightGeometry.Width.Length;
                unifyObj.Length          = light.LightGeometry.Length.Length;

                if (light.IsDeleted)
                {
                    unifyObj.Deleted = true;
                }

                lightList.Add(unifyObj);
            }

            this.Lights = lightList;
        }
コード例 #2
0
    private static void CreateLight(UnifyLight obj, GameObject parent)
    {
        GameObject lightGameObj;
        Light      lightComp;

        if (GameObject.Find(obj.LightType + "_" + obj.Guid) == null)
        {
            lightGameObj = new GameObject(obj.LightType + "_" + obj.Guid);
            lightComp    = lightGameObj.AddComponent <Light>();
        }
        else
        {
            lightGameObj = GameObject.Find(obj.LightType + "_" + obj.Guid);
            lightComp    = lightGameObj.GetComponent <Light>();
        }

        // set light type
        switch (obj.LightType)
        {
        case "WorldSpot":
        {
            lightComp.type = LightType.Spot;

            // set range
            lightComp.range = Convert.ToSingle(obj.Range);

            // set angle
            lightComp.spotAngle = Utilities.ConvertRange(0, 90, 0, 180, Utilities.RadiansToDegrees(obj.SpotAngle));
            break;
        }

        case "WorldDirectional":
        {
            lightComp.type = LightType.Directional;
            break;
        }

        case "WorldPoint":
        {
            lightComp.type = LightType.Point;

            // set range
            // range settings doesn't exist in Rhino
            lightComp.range = 5.0f;
            break;
        }

        case "WorldRectangular":
        {
            lightComp.type = LightType.Area;

            // set width and height
            lightComp.areaSize = new Vector2(Convert.ToSingle(obj.Length), Convert.ToSingle(obj.Width));
            break;
        }

        default:
        {
            Debug.Log("No Light Type found.");
            break;
        }
        }

        // set color
        lightComp.color = Utilities.ConvertToUnityColor(obj.Diffuse);

        // set location
        lightGameObj.transform.position = Utilities.TranslateFromRhinoCS(obj.Location);

        // set target
        GameObject tempObj = new GameObject("Temp");

        tempObj.transform.position = Utilities.TranslateFromRhinoCS(obj.Target);
        lightGameObj.transform.LookAt(tempObj.transform.position);
        DestroyImmediate(tempObj);

        // set intensity
        lightComp.intensity = Utilities.ConvertRange(0, 1, 0, 8, Convert.ToSingle(obj.Intensity));

        // set shadows
        lightComp.shadows        = LightShadows.Soft;
        lightComp.shadowStrength = Convert.ToSingle(obj.ShadowIntensity);

        // create prefab
        string assetName = "Assets/Resources/Lights/" + obj.UniqueName + ".prefab";

        PrefabUtility.CreatePrefab(assetName, lightGameObj, ReplacePrefabOptions.ReplaceNameBased);

        // add light to parent
        lightGameObj.transform.parent = parent.transform;
    }