Ejemplo n.º 1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Color  color     = Color.White;
            double intensity = 0.5;
            string name      = "";

            // Reference the inputs
            DA.GetData(0, ref color);
            DA.GetData(1, ref intensity);
            DA.GetData(2, ref name);

            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "AmbientLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color     = Convert.ToInt32(color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2"), 16);;
            lightObject.Intensity = intensity;
            lightObject.Matrix    = new Matrix(new Point3d(0, 0, 0)).Array;

            // Create object3d
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetData(0, object3d);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Object3d object3d = null;
            bool     minify   = false;
            bool     run      = true;

            // Reference the inputs
            DA.GetData(0, ref object3d);
            DA.GetData(1, ref minify);
            DA.GetData(2, ref run);

            // If minify is true, formatting should be none
            Formatting formatting;

            if (minify)
            {
                formatting = Formatting.None;
            }
            else
            {
                formatting = Formatting.Indented;
            }

            string jsonString = "";

            // If set to run, run the serializer
            if (run)
            {
                // Create JSON string
                jsonString = JsonConvert.SerializeObject(object3d, formatting, new JsonSerializerSettings
                {
                    ContractResolver  = new CamelCasePropertyNamesContractResolver(),
                    NullValueHandling = NullValueHandling.Ignore
                });
            }
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Set run to true to serialize the object");
            }

            // Set the outputs
            DA.SetData(0, jsonString);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Color  color       = Color.LightSkyBlue;
            Color  groundColor = Color.Tan;
            double intensity   = 0.5;
            string name        = "";

            // Reference the inputs
            DA.GetData(0, ref color);
            DA.GetData(1, ref groundColor);
            DA.GetData(2, ref intensity);
            DA.GetData(3, ref name);



            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "HemisphereLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color       = new DecimalColor(color).Color;
            lightObject.GroundColor = new DecimalColor(groundColor).Color;
            lightObject.Intensity   = intensity;
            lightObject.Matrix      = new Matrix(new Point3d(0, 0, 1)).Array;

            // Create object3d
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetData(0, object3d);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Rhino.Geometry.Mesh mesh = null;
            string   name            = "";
            Material material        = null;
            int      decimalAccuracy = 3;

            // Reference the inputs
            DA.GetData(0, ref mesh);
            DA.GetData(1, ref name);
            DA.GetData(2, ref material);
            DA.GetData(3, ref decimalAccuracy);

            // If the meshes have quads, triangulate them
            if (!mesh.Faces.ConvertQuadsToTriangles())
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Error triangulating quad meshes. Try triangulating quads before feeding into this component.");
            }
            ;

            // Get the center of the object so the bufferGeometry can be given a local coordinates
            Point3d center = mesh.GetBoundingBox(true).Center;

            /// Add the bufferGeometry
            BufferGeometry geometry = new BufferGeometry(mesh, center, decimalAccuracy, true);

            /// Add the child
            dynamic meshObject = new ExpandoObject();

            meshObject.Uuid = Guid.NewGuid();
            if (name.Length > 0)
            {
                meshObject.name = name;
            }
            meshObject.Type          = "Mesh";
            meshObject.Geometry      = geometry.Uuid;
            meshObject.Matrix        = new Matrix(center).Array;
            meshObject.CastShadow    = true;
            meshObject.ReceiveShadow = true;

            // Create line object
            Object3d object3d = new Object3d(meshObject);

            object3d.AddGeometry(geometry);

            // If there is a material, add the material, textures, and images
            if (material != null)
            {
                meshObject.Material = material.Data.Uuid;

                // Set the material to use vertex colors
                material.Data.vertexColors = 2;
                material.Data.color        = "0xffffff";

                object3d.AddMaterial(material.Data);
                if (material.Textures != null)
                {
                    foreach (dynamic texture in material.Textures)
                    {
                        object3d.AddTexture(texture);
                    }
                }
                if (material.Images != null)
                {
                    foreach (dynamic image in material.Images)
                    {
                        object3d.AddImage(image);
                    }
                }
            }
            else
            {
                Guid uuid = Guid.NewGuid();
                meshObject.Material = uuid;

                // Build the material object
                dynamic meshBasicMaterial = new ExpandoObject();
                meshBasicMaterial.Uuid         = uuid;
                meshBasicMaterial.Type         = "MeshBasicMaterial";
                meshBasicMaterial.vertexColors = 2;
                object3d.AddMaterial(meshBasicMaterial);
            }

            // Set output references
            DA.SetData(0, object3d);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Point3d position   = new Point3d(0, 10, 0);
            Color   color      = Color.White;
            double  intensity  = 1;
            double  angle      = 0.35;
            double  distance   = 0;
            double  decay      = 1;
            Boolean castShadow = false;
            string  name       = "";

            // Reference the inputs
            DA.GetData(0, ref position);
            DA.GetData(1, ref color);
            DA.GetData(2, ref intensity);
            DA.GetData(3, ref angle);
            DA.GetData(4, ref distance);
            DA.GetData(5, ref decay);
            DA.GetData(6, ref castShadow);
            DA.GetData(7, ref name);

            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "SpotLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color      = Convert.ToInt32(color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2"), 16);;
            lightObject.Intensity  = intensity;
            lightObject.Angle      = (Math.PI / 180) * angle;
            lightObject.CastShadow = castShadow;
            lightObject.Distance   = distance;
            lightObject.Decay      = decay;
            lightObject.Matrix     = new Matrix(position).Array;

            // Create a helper to aid in visualizing the light's behavior
            List <Curve> helper = new List <Curve>();

            var line = new Rhino.Geometry.Line(position, new Point3d(0, 0, 0));

            helper.Add(line.ToNurbsCurve());

            Vector3d vector = new Vector3d(new Point3d(0, 0, 0) - position);
            Plane    plane  = new Plane(position, vector);
            double   radius = vector.Length * Math.Tan((Math.PI / 180) * angle);
            Cone     cone   = new Cone(plane, vector.Length, radius);

            Brep brep = cone.ToBrep(false);

            for (int i = 0; i < brep.Edges.Count; i++)
            {
                helper.Add(brep.Edges[i].EdgeCurve);
            }

            // Create object3d
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetDataList(0, helper);
            DA.SetData(1, object3d);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Mesh     mesh            = null;
            string   name            = "";
            Material material        = null;
            int      decimalAccuracy = 3;
            UserData userData        = null;

            // Reference the inputs
            DA.GetData(0, ref mesh);
            DA.GetData(1, ref name);
            DA.GetData(2, ref material);
            DA.GetData(3, ref decimalAccuracy);
            DA.GetData(4, ref userData);

            // If the meshes have quads, triangulate them
            if (!mesh.Faces.ConvertQuadsToTriangles())
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Error triangulating quad meshes. Try triangulating quads before feeding into this component.");
            }
            ;

            // Get the center of the object so the bufferGeometry can be given a local coordinates
            Point3d center = mesh.GetBoundingBox(true).Center;

            // Create bufferGeometry
            var geometry = new BufferGeometry(mesh, center, decimalAccuracy);

            //Create a mesh object
            dynamic meshObject = new ExpandoObject();

            meshObject.Uuid          = Guid.NewGuid();
            meshObject.Name          = name;
            meshObject.Type          = "Mesh";
            meshObject.Geometry      = geometry.Uuid;
            meshObject.Matrix        = new Matrix(center).Array;
            meshObject.CastShadow    = true;
            meshObject.ReceiveShadow = true;

            // If there is userData, add it to the object
            if (userData != null)
            {
                meshObject.UserData = userData.properties;
            }

            // Create mesh object
            Object3d object3d = new Object3d(meshObject);

            object3d.AddGeometry(geometry);

            // If there is a material, add the material, textures, and images
            if (material != null)
            {
                meshObject.Material = material.Data.Uuid;

                object3d.AddMaterial(material.Data);
                if (material.Textures != null)
                {
                    foreach (dynamic texture in material.Textures)
                    {
                        object3d.AddTexture(texture);
                    }
                }
                if (material.Images != null)
                {
                    foreach (dynamic image in material.Images)
                    {
                        object3d.AddImage(image);
                    }
                }
            }



            // Set outputs
            DA.SetData(0, object3d);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declate variables
            Curve        curve           = null;
            string       name            = "";
            Material     material        = null;
            List <Color> colors          = new List <Color>();
            int          decimalAccuracy = 3;

            // Reference the inputs
            DA.GetData(0, ref curve);
            DA.GetData(1, ref name);
            DA.GetData(2, ref material);
            if (!DA.GetDataList(3, colors))
            {
                return;
            }
            DA.GetData(4, ref decimalAccuracy);

            // Throw error message if curve cannot be converted to a polyline
            if (!curve.IsPolyline())
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input must be polyine, curves are not allowed.");
                return;
            }

            // Convert curve to polyline
            Polyline polyline;

            curve.TryGetPolyline(out polyline);

            // Get the center of the object so the bufferGeometry can be given local coordinates
            Point3d center = polyline.BoundingBox.Center;

            // Produce a buffer geometry from the polyline
            dynamic geometry = new BufferGeometry(polyline, center, decimalAccuracy);

            geometry.AddVertexColors(colors);

            // If dashed material is used add the line distance to the buffer geometry
            if (material != null && string.Equals(material.Data.Type, "LineDashedMaterial"))
            {
                geometry.AddLineDistance();
            }

            //Create a threejs object definition
            dynamic lineObject = new ExpandoObject();

            lineObject.Uuid     = Guid.NewGuid();
            lineObject.Name     = name;
            lineObject.Type     = "Line";
            lineObject.Geometry = geometry.Uuid;
            lineObject.Matrix   = new Matrix(center).Array;

            // Create file object
            Object3d object3d = new Object3d(lineObject);

            object3d.AddGeometry(geometry);

            // If there is a material, add the material, textures, and images
            if (material != null)
            {
                lineObject.Material = material.Data.Uuid;

                material.Data.vertexColors = 2;
                object3d.AddMaterial(material.Data);

                if (material.Textures != null)
                {
                    foreach (dynamic texture in material.Textures)
                    {
                        object3d.AddTexture(texture);
                    }
                }
                if (material.Images != null)
                {
                    foreach (dynamic image in material.Images)
                    {
                        object3d.AddImage(image);
                    }
                }
            }
            else
            {
                Guid uuid = Guid.NewGuid();
                lineObject.Material = uuid;

                // Build the material object
                dynamic lineBasicMaterial = new ExpandoObject();
                lineBasicMaterial.Uuid         = uuid;
                lineBasicMaterial.Type         = "LineBasicMaterial";
                lineBasicMaterial.vertexColors = 2;
                object3d.AddMaterial(lineBasicMaterial);
            }

            // Set outputs
            DA.SetData(0, object3d);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Point3d position   = new Point3d(0, 10, 0);
            Color   color      = Color.White;
            double  intensity  = 1;
            double  distance   = 0;
            double  decay      = 1;
            Boolean castShadow = false;
            string  name       = "";

            // Reference the inputs
            DA.GetData(0, ref position);
            DA.GetData(1, ref color);
            DA.GetData(2, ref intensity);
            DA.GetData(3, ref distance);
            DA.GetData(4, ref decay);
            DA.GetData(5, ref castShadow);
            DA.GetData(6, ref name);

            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "PointLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color      = Convert.ToInt32(color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2"), 16);;
            lightObject.Intensity  = intensity;
            lightObject.CastShadow = castShadow;
            lightObject.Distance   = distance;
            lightObject.Decay      = decay;
            lightObject.Matrix     = new Matrix(position).Array;

            // Create a helper to aid in visualizing the light's behavior
            List <Curve> helper  = new List <Curve>();
            Circle       circle1 = new Circle(Plane.WorldXY, position, 0.5);
            Circle       circle2 = new Circle(Plane.WorldYZ, position, 0.5);
            Circle       circle3 = new Circle(Plane.WorldZX, position, 0.5);

            helper.Add(circle1.ToNurbsCurve());
            helper.Add(circle2.ToNurbsCurve());
            helper.Add(circle3.ToNurbsCurve());

            var lineX0 = new Rhino.Geometry.Line(position, position + new Point3d(1, 0, 0));
            var lineX1 = new Rhino.Geometry.Line(position, position + new Point3d(-1, 0, 0));
            var lineY0 = new Rhino.Geometry.Line(position, position + new Point3d(0, 1, 0));
            var lineY1 = new Rhino.Geometry.Line(position, position + new Point3d(0, -1, 0));
            var lineZ0 = new Rhino.Geometry.Line(position, position + new Point3d(0, 0, 1));
            var lineZ1 = new Rhino.Geometry.Line(position, position + new Point3d(0, 0, -1));

            helper.Add(lineX0.ToNurbsCurve());
            helper.Add(lineX1.ToNurbsCurve());
            helper.Add(lineY0.ToNurbsCurve());
            helper.Add(lineY1.ToNurbsCurve());
            helper.Add(lineZ0.ToNurbsCurve());
            helper.Add(lineZ1.ToNurbsCurve());

            // Create light
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetDataList(0, helper);
            DA.SetData(1, object3d);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            List <Object3d> object3ds       = new List <Object3d>();
            String          name            = "";
            Color           backgroundColor = Color.LightGray;
            Texture         background      = null;
            Texture         environment     = null;
            Fog             fog             = null;

            // Reference the inputs
            DA.GetDataList(0, object3ds);
            DA.GetData(1, ref name);
            DA.GetData(2, ref backgroundColor);
            DA.GetData(3, ref background);
            DA.GetData(4, ref environment);
            DA.GetData(5, ref fog);

            /// Scene
            dynamic scene = new ExpandoObject();

            scene.Uuid     = Guid.NewGuid();
            scene.Type     = "Scene";
            scene.Name     = name;
            scene.Matrix   = new Matrix(Point3d.Origin).Array;
            scene.Children = new List <dynamic>();

            Object3d sceneObject = new Object3d(scene);

            // Create lists for ids to make sure there are no duplicates
            List <string> geometriesIds = new List <string>();
            List <string> materialsIds  = new List <string>();
            List <string> texturesIds   = new List <string>();
            List <string> imagesIds     = new List <string>();

            // If there is a background, add it to the scene
            if (background != null)
            {
                scene.Background = background.Data.Uuid;

                if (!texturesIds.Contains(background.Data.Uuid.ToString()))
                {
                    sceneObject.AddTexture(background.Data);
                    texturesIds.Add(background.Data.Uuid.ToString());
                }

                if (!imagesIds.Contains(background.Image.Uuid.ToString()))
                {
                    sceneObject.AddImage(background.Image);
                    imagesIds.Add(background.Image.Uuid.ToString());
                }
            }
            else
            {
                scene.Background = new DecimalColor(backgroundColor).Color;
            }

            // If there is an environment, add it to the scene
            if (environment != null)
            {
                scene.Environment = environment.Data.Uuid;

                if (!texturesIds.Contains(environment.Data.Uuid.ToString()))
                {
                    sceneObject.AddTexture(environment.Data);
                    texturesIds.Add(environment.Data.Uuid.ToString());
                }

                if (!imagesIds.Contains(environment.Image.Uuid.ToString()))
                {
                    sceneObject.AddImage(environment.Image);
                    imagesIds.Add(environment.Image.Uuid.ToString());
                }
            }

            // If there is an fog input, add it to the scene
            if (fog != null)
            {
                scene.Fog = fog;
            }

            // Loop over all the threejs objects and add them to the scene
            foreach (Object3d object3d in object3ds)
            {
                // Add the objects to the scene as child objects
                scene.Children.Add(object3d.Object);

                // If Threejs object includes geometries list, add geometry objects to the scene
                if (object3d.Geometries != null)
                {
                    foreach (dynamic geometry in object3d.Geometries)
                    {
                        if (!geometriesIds.Contains(geometry.Uuid.ToString()))
                        {
                            sceneObject.AddGeometry(geometry);
                            geometriesIds.Add(geometry.Uuid.ToString());
                        }
                    }
                }

                // If Threejs object includes materials list, add material obects to the scene
                if (object3d.Materials != null)
                {
                    foreach (dynamic material in object3d.Materials)
                    {
                        if (!materialsIds.Contains(material.Uuid.ToString()))
                        {
                            sceneObject.AddMaterial(material);
                            materialsIds.Add(material.Uuid.ToString());
                        }
                    }
                }

                // If Threejs object includes textures list, add the textures to the scene
                if (object3d.Textures != null)
                {
                    foreach (dynamic texture in object3d.Textures)
                    {
                        if (!texturesIds.Contains(texture.Uuid.ToString()))
                        {
                            sceneObject.AddTexture(texture);
                            texturesIds.Add(texture.Uuid.ToString());
                        }
                    }
                }

                // If Threejs object includes images list, add the images to the scene
                if (object3d.Images != null)
                {
                    foreach (dynamic image in object3d.Images)
                    {
                        if (!imagesIds.Contains(image.Uuid.ToString()))
                        {
                            sceneObject.AddImage(image);
                            imagesIds.Add(image.Uuid.ToString());
                        }
                    }
                }
            }

            // Set the outputs
            DA.SetData(0, sceneObject);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            List <Point3d> points          = new List <Point3d>();
            string         name            = "";
            Material       material        = null;
            List <Color>   colors          = new List <Color>();
            int            decimalAccuracy = 3;

            // Reference the inputs
            if (!DA.GetDataList(0, points))
            {
                return;
            }
            DA.GetData(1, ref name);
            DA.GetData(2, ref material);
            if (!DA.GetDataList(3, colors))
            {
                return;
            }
            DA.GetData(4, ref decimalAccuracy);

            // Get the center of the bounding box to create local coordinates
            PointCloud pointCloud = new PointCloud(points);
            Point3d    center     = pointCloud.GetBoundingBox(true).Center;

            // Build the customColor objet
            dynamic customColor = new ExpandoObject();

            customColor.ItemSize   = 3;
            customColor.Type       = "Float32Array";
            customColor.Array      = new List <double>();
            customColor.Normalized = false;

            // Build the size object
            dynamic size = new ExpandoObject();

            size.Itemsize   = 1;
            size.Type       = "Float32Array";
            size.Array      = new List <double>();
            size.Normalized = false;

            // Build the attributes object
            //dynamic attributes = new ExpandoObject();
            //attributes.position = position;
            //attributes.customColor = customColor;
            //attributes.size = size;

            // Create the buffer geometry
            BufferGeometry geometry = new BufferGeometry(points, center, decimalAccuracy);

            geometry.AddVertexColors(colors);

            // Build the child object
            dynamic pointsObject = new ExpandoObject();

            pointsObject.Uuid     = Guid.NewGuid();
            pointsObject.Type     = "Points";
            pointsObject.Layers   = 1;
            pointsObject.Matrix   = new Matrix(center).Array;
            pointsObject.Geometry = geometry.Uuid;

            // Create line object
            Object3d object3d = new Object3d(pointsObject);

            object3d.AddGeometry(geometry);

            // If there is a material, add the material, textures, and images
            if (material != null)
            {
                pointsObject.Material = material.Data.Uuid;

                material.Data.vertexColors = 2;
                object3d.AddMaterial(material.Data);

                if (material.Textures != null)
                {
                    foreach (dynamic texture in material.Textures)
                    {
                        object3d.AddTexture(texture);
                    }
                }
                if (material.Images != null)
                {
                    foreach (dynamic image in material.Images)
                    {
                        object3d.AddImage(image);
                    }
                }
            }
            else
            {
                Guid uuid = Guid.NewGuid();
                pointsObject.Material = uuid;

                // Build the material object
                dynamic pointsMaterial = new ExpandoObject();
                pointsMaterial.Uuid         = uuid;
                pointsMaterial.Type         = "PointsMaterial";
                pointsMaterial.vertexColors = 2;
                object3d.AddMaterial(pointsMaterial);
            }

            // Set output references
            DA.SetData(0, object3d);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Rectangle3d rectangle = new Rectangle3d();
            Color       color     = Color.White;
            double      intensity = 1;
            string      name      = "";

            // Reference the inputs
            DA.GetData(0, ref rectangle);
            DA.GetData(1, ref color);
            DA.GetData(2, ref intensity);
            DA.GetData(3, ref name);

            // Get matrix transform between starting plane and rectangle
            Point3d position = rectangle.Center;

            Vector3d vectorX = new Vector3d(-1, 0, 0);
            Vector3d vectorZ = new Vector3d(0, 0, 1);

            Plane zxPlane = new Plane(Point3d.Origin, vectorX, vectorZ);

            var plane     = rectangle.Plane;
            var transform = Transform.PlaneToPlane(zxPlane, plane);

            float[]       matrixArray = transform.ToFloatArray(false);
            List <double> l           = new List <double>();

            foreach (float value in matrixArray)
            {
                decimal dec = new decimal(value);
                double  dub = (double)dec;
                l.Add(Math.Round(dub, 6));
            }

            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "RectAreaLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color     = Convert.ToInt32(color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2"), 16);;
            lightObject.Intensity = intensity;
            lightObject.Width     = rectangle.Width;
            lightObject.Height    = rectangle.Height;
            lightObject.Matrix    = new List <double> {
                l[0] * -1, l[2], l[1], l[3], l[8] * -1, l[10], l[9], l[11], l[4] * -1, l[6], l[5], l[7], l[12] * -1, l[14], l[13], l[15]
            };

            // Create a helper to aid in visualizing the light's behavior
            List <Curve> helper = new List <Curve>();

            helper.Add(rectangle.ToNurbsCurve());

            Point3d start         = plane.Origin;
            Point3d end           = start + plane.Normal;
            Line    directionLine = new Line(start, end);

            helper.Add(directionLine.ToNurbsCurve());

            // Create object3d
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetDataList(0, helper);
            DA.SetData(1, object3d);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Point3d position     = new Point3d(0, 10, 10);
            Color   color        = Color.White;
            double  intensity    = 0.5;
            string  name         = "";
            Boolean castShadow   = false;
            double  shadowTop    = 10;
            double  shadowBottom = -10;
            double  shadowRight  = 10;
            double  shadowLeft   = -10;
            double  shadowNear   = 0;
            double  shadowFar    = 100;

            // Reference the inputs
            DA.GetData(0, ref position);
            DA.GetData(1, ref color);
            DA.GetData(2, ref intensity);
            DA.GetData(3, ref name);
            DA.GetData(4, ref castShadow);
            DA.GetData(5, ref shadowTop);
            DA.GetData(6, ref shadowBottom);
            DA.GetData(7, ref shadowRight);
            DA.GetData(8, ref shadowLeft);
            DA.GetData(9, ref shadowNear);
            DA.GetData(10, ref shadowFar);

            // Build the shadow camera
            dynamic camera = new ExpandoObject();

            camera.Uuid   = Guid.NewGuid();
            camera.Type   = "OrthographicCamera";
            camera.Top    = shadowTop;
            camera.Bottom = shadowBottom;
            camera.Right  = shadowRight;
            camera.Left   = shadowLeft;
            camera.Near   = shadowNear;
            camera.Far    = shadowFar;

            dynamic shadow = new ExpandoObject();

            shadow.camera = camera;

            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "DirectionalLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color      = new DecimalColor(color).Color;
            lightObject.Intensity  = intensity;
            lightObject.Matrix     = new Matrix(position).Array;
            lightObject.CastShadow = castShadow;
            lightObject.Shadow     = shadow;

            // Create a helper to aid in visualizing the light's behavior
            List <Curve> helper = new List <Curve>();

            Circle circle1 = new Circle(Plane.WorldXY, position, 0.5);
            Circle circle2 = new Circle(Plane.WorldYZ, position, 0.5);
            Circle circle3 = new Circle(Plane.WorldZX, position, 0.5);

            helper.Add(circle1.ToNurbsCurve());
            helper.Add(circle2.ToNurbsCurve());
            helper.Add(circle3.ToNurbsCurve());

            var line = new Rhino.Geometry.Line(position, new Point3d(0, 0, 0));

            helper.Add(line.ToNurbsCurve());

            // If it casts a shadow, create a shadow helper
            if (castShadow)
            {
                // Create the plane's x vector
                Vector3d vectorX = new Vector3d(-position);
                vectorX.Rotate(90 * (Math.PI / 180), Vector3d.ZAxis);
                vectorX.Z = 0;

                // Create the plane's y vector
                Vector3d vectorY = new Vector3d(-position);
                vectorY.Rotate(-90 * (Math.PI / 180), vectorX);

                Plane    plane     = new Plane(position, vectorX, vectorY);
                Interval xSize     = new Interval(-shadowRight, -shadowLeft);
                Interval ySize     = new Interval(shadowBottom, shadowTop);
                Interval zSize     = new Interval(shadowNear, shadowFar);
                Box      shadowBox = new Box(plane, xSize, ySize, zSize);
                Brep     brep      = shadowBox.ToBrep();

                for (int i = 0; i < brep.Edges.Count; i++)
                {
                    helper.Add(brep.Edges[i].EdgeCurve);
                }
            }

            // Create object3d
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetDataList(0, helper);
            DA.SetData(1, object3d);
        }