예제 #1
0
        private void WriteGeometry(JsonWriter writer)
        {
            XbimMatrix3D globalTrans = GetGlobalModelTransform();
            //write out the material nodes and then the instances for each material
            Dictionary <int, XbimTexture> styles = _context.SurfaceStyles().ToDictionary(s => s.DefinedObjectId);

            foreach (var instanceGroup in _context.ShapeInstancesGroupByStyle())
            {
                if (!instanceGroup.Any())
                {
                    continue;                       //skip emmpty instances;
                }
                int         styleId = instanceGroup.Key;
                XbimTexture style   = styles[styleId];
                writer.WriteStartObject(); //Material node
                {
                    writer.WritePropertyName("type"); writer.WriteValue("material");
                    writer.WritePropertyName("id"); writer.WriteValue("M" + styleId);
                    writer.WritePropertyName("color");
                    writer.WriteStartObject(); //begin color
                    {
                        writer.WritePropertyName("r"); writer.WriteValue(style.ColourMap[0].Red);
                        writer.WritePropertyName("g"); writer.WriteValue(style.ColourMap[0].Green);
                        writer.WritePropertyName("b"); writer.WriteValue(style.ColourMap[0].Blue);
                    } writer.WriteEndObject(); //end color
                    writer.WritePropertyName("alpha"); writer.WriteValue(style.ColourMap[0].Alpha);
                    //all instances of this style
                    writer.WritePropertyName("nodes"); //beginning of the instance nodes
                    writer.WriteStartArray();
                    foreach (var shapeInstance in instanceGroup)
                    {
                        writer.WriteStartObject();                                //start of instance transform
                        {
                            if (_maps.Contains(shapeInstance.ShapeGeometryLabel)) //it is a reference
                            {
                                //write the transform
                                writer.WritePropertyName("type"); //geometry node
                                writer.WriteValue("matrix");
                                writer.WritePropertyName("id");
                                writer.WriteValue("I" + shapeInstance.InstanceLabel);
                                writer.WritePropertyName("elements");
                                XbimMatrix3D m = XbimMatrix3D.Multiply(shapeInstance.Transformation, globalTrans);
                                WriteMatrix(writer, m);
                                writer.WritePropertyName("nodes"); //beginning of the instance nodes
                                writer.WriteStartArray();
                                //write the map
                                writer.WriteStartObject();            //start of map
                                {
                                    writer.WritePropertyName("type"); //geometry node
                                    writer.WriteValue("geometry");
                                    writer.WritePropertyName("coreId");
                                    writer.WriteValue("L" + shapeInstance.ShapeGeometryLabel);
                                } writer.WriteEndObject(); //end of map
                                writer.WriteEndArray();    //end of instance tranform nodes
                            }
                            else //write the actual geometry
                            {
                                writer.WritePropertyName("type");      //geometry node
                                writer.WriteValue("geometry");
                                writer.WritePropertyName("primitive"); //primitive: "
                                writer.WriteValue("triangles");
                                writer.WritePropertyName("id");
                                writer.WriteValue("I" + shapeInstance.InstanceLabel);
                                WriteShapeGeometry(writer, _context.ShapeGeometry(shapeInstance), XbimMatrix3D.Multiply(shapeInstance.Transformation, globalTrans));
                            }
                        } writer.WriteEndObject(); //end of instance transform
                    }
                    writer.WriteEndArray();        //end of instance transform nodes
                } writer.WriteEndObject();         // end of the material node
            }
        }