示例#1
0
            public GeometrySource(CL.source src, Dictionary <string, float[]> arrays)
            {
                var acc = src.technique_common.accessor;

                array  = arrays[CheckURI(acc.source)];
                stride = (int)acc.stride;
                offset = (int)acc.offset;
                Count  = (int)acc.count;
            }
示例#2
0
        public static source ArrayToSource(Color[] input, string tag)
        {
            source outputSource = new source();
            outputSource.id = tag;

            const int num_values = 4;

            float_array vectorArray = new float_array();
            vectorArray.Values = new double[input.Length * num_values];
            for (int i = 0; i < input.Length; i++)
            {
                vectorArray.Values[i * num_values + 0] = input[i].r;
                vectorArray.Values[i * num_values + 1] = input[i].g;
                vectorArray.Values[i * num_values + 2] = input[i].b;
                vectorArray.Values[i * num_values + 3] = input[i].a;
            }
            vectorArray.id = tag + "-array";
            vectorArray.count = (ulong)vectorArray.Values.Length;

            outputSource.Item = vectorArray;

            accessor vectorAccessor = new accessor();
            vectorAccessor.source = "#" + vectorArray.id;
            vectorAccessor.count = (ulong)input.Length;
            vectorAccessor.stride = num_values;
            vectorAccessor.param = new param[num_values];
            vectorAccessor.param[0] = new param();
            vectorAccessor.param[0].name = "R";
            vectorAccessor.param[0].type = "float";
            vectorAccessor.param[1] = new param();
            vectorAccessor.param[1].name = "G";
            vectorAccessor.param[1].type = "float";
            vectorAccessor.param[2] = new param();
            vectorAccessor.param[2].name = "B";
            vectorAccessor.param[2].type = "float";
            vectorAccessor.param[3] = new param();
            vectorAccessor.param[3].name = "A";
            vectorAccessor.param[3].type = "float";

            outputSource.technique_common = new sourceTechnique_common();
            outputSource.technique_common.accessor = vectorAccessor;

            return outputSource;
        }
示例#3
0
        public static source ArrayToSource(Vector3[] input, string tag)
        {
            source outputSource = new source();
            outputSource.id = tag;

            float_array vectorArray = new float_array();
            vectorArray.Values = new double[input.Length * 3];
            for (int i = 0; i < input.Length; i++)
            {
                vectorArray.Values[i * 3 + 0] = input[i].x;
                vectorArray.Values[i * 3 + 1] = input[i].y;
                vectorArray.Values[i * 3 + 2] = input[i].z;
            }
            vectorArray.id = tag + "-array";
            vectorArray.count = (ulong)vectorArray.Values.Length;

            outputSource.Item = vectorArray;

            accessor vectorAccessor = new accessor();
            vectorAccessor.source = "#" + tag + "-array";
            vectorAccessor.count = (ulong)input.Length;
            vectorAccessor.stride = 3;
            vectorAccessor.param = new param[3];
            vectorAccessor.param[0] = new param();
            vectorAccessor.param[0].name = "X";
            vectorAccessor.param[0].type = "float";
            vectorAccessor.param[1] = new param();
            vectorAccessor.param[1].name = "Y";
            vectorAccessor.param[1].type = "float";
            vectorAccessor.param[2] = new param();
            vectorAccessor.param[2].name = "Z";
            vectorAccessor.param[2].type = "float";

            outputSource.technique_common = new sourceTechnique_common();
            outputSource.technique_common.accessor = vectorAccessor;

            return outputSource;
        }
示例#4
0
        protected mesh CreatePalletMesh(PalletProperties palletProperties)
        {
            // build pallet object
            Pallet pallet = new Pallet(palletProperties);
            // build list of boxes
            List<Box> listBoxes = pallet.BuildListOfBoxes();
            // build list of vertices / normals / UVs
            ulong vertexCount = 0, normalCount = 0, uvCount = 0, triangleCount = 0, boxCount = 0;
            string triangle_string = string.Empty;

            List<double> doubleArrayPosition = new List<double>(), doubleArrayNormal = new List<double>(), doubleArrayUV = new List<double>();

            foreach (Box box in listBoxes)
            {
                foreach (Vector3D p in box.Points)
                {
                    doubleArrayPosition.Add(p.X); doubleArrayPosition.Add(p.Y); doubleArrayPosition.Add(p.Z);
                    ++vertexCount;
                }
                foreach (Vector3D n in box.Normals)
                {
                    doubleArrayNormal.Add(n.X); doubleArrayNormal.Add(n.Y); doubleArrayNormal.Add(n.Z);
                    ++normalCount;
                }
                foreach (Vector2D uv in box.UVs)
                {
                    doubleArrayUV.Add(uv.X); doubleArrayUV.Add(uv.Y);
                    ++uvCount;
                }
                foreach (TriangleIndices tr in box.Triangles)
                {
                    triangle_string += tr.ConvertToString(boxCount);
                    ++triangleCount;
                }
                ++boxCount;
            }
            mesh palletMesh = new mesh();

            // position source
            source palletPositionSource = new source() { id = "pallet_position", name = "pallet_position" };
            float_array farrayPosition = new float_array { id = "pallet_position_float_array", count = (ulong)doubleArrayPosition.Count, Values = doubleArrayPosition.ToArray() };
            palletPositionSource.technique_common = new sourceTechnique_common()
            {
                accessor = new accessor()
                {
                    stride = 3,
                    count = vertexCount,
                    source = "#pallet_position_float_array",
                    param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } }
                }
            };
            palletPositionSource.Item = farrayPosition;

            // normal source
            source palletPositionNormal = new source() { id = "pallet_normal", name = "pallet_normal" };
            float_array farrayNormal = new float_array { id = "pallet_normal_float_array", count = (ulong)doubleArrayNormal.Count, Values = doubleArrayNormal.ToArray() };
            palletPositionNormal.technique_common = new sourceTechnique_common()
            {
                accessor = new accessor()
                {
                    stride = 3,
                    count = normalCount,
                    source = "#pallet_normal_float_array",
                    param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } }
                }
            };
            palletPositionNormal.Item = farrayNormal;

            // uv source
            source palletPositionUV = new source() { id = "pallet_UV", name = "pallet_UV" };
            float_array farrayUV = new float_array { id = "pallet_UV_float_array", count = (ulong)doubleArrayUV.Count, Values = doubleArrayUV.ToArray() };
            palletPositionUV.technique_common = new sourceTechnique_common()
            {
                accessor = new accessor()
                {
                    stride = 2,
                    count = vertexCount,
                    source = "#pallet_UV_float_array",
                    param = new param[] { new param() { name = "S", type = "float" }, new param() { name = "T", type = "float" } }
                }
            };
            palletPositionUV.Item = farrayUV;
            // insert sources
            palletMesh.source = new source[] { palletPositionSource, palletPositionNormal, palletPositionUV };

            // vertices
            InputLocal verticesInput = new InputLocal() { semantic = "POSITION", source = "#pallet_position" };
            palletMesh.vertices = new vertices() { id = "pallet_vertex", input = new InputLocal[] { verticesInput } };

            triangles trianglesPallet = new triangles() { material = "materialPallet", count = triangleCount };
            trianglesPallet.input = new InputLocalOffset[]
                                    {
                                        new InputLocalOffset() { semantic="VERTEX", source="#pallet_vertex", offset=0}
                                        , new InputLocalOffset() { semantic="NORMAL", source="#pallet_normal", offset=1}
                                        , new InputLocalOffset() { semantic="TEXCOORD", source="#pallet_UV", offset=2, set=0, setSpecified=true }
                                    };

            trianglesPallet.p = triangle_string;
            palletMesh.Items = new object[] { trianglesPallet };

            return palletMesh;
        }
示例#5
0
        protected mesh CreateCaseMesh(BoxProperties caseProperties)
        {
            // build box
            Box box = new Box(0, caseProperties);
            // build list of vertices / normals / UVs
            ulong vertexCount = 0, normalCount = 0, uvCount = 0;
            List<double> doubleArrayPosition = new List<double>(), doubleArrayNormal = new List<double>(), doubleArrayUV = new List<double>();
            foreach (Vector3D p in box.PointsSmallOffset)
            {
                doubleArrayPosition.Add(p.X); doubleArrayPosition.Add(p.Y); doubleArrayPosition.Add(p.Z);
                ++vertexCount;
            }
            foreach (Vector3D n in box.Normals)
            {
                doubleArrayNormal.Add(n.X); doubleArrayNormal.Add(n.Y); doubleArrayNormal.Add(n.Z);
                ++normalCount;
            }
            foreach (Vector2D uv in box.UVs)
            {
                doubleArrayUV.Add(uv.X); doubleArrayUV.Add(uv.Y);
                ++uvCount;
            }

            mesh caseMesh = new mesh();

            // position source
            source casePositionSource = new source() { id = "case_position", name = "case_position" };
            float_array farrayPosition = new float_array { id = "case_position_float_array", count = (ulong)doubleArrayPosition.Count, Values = doubleArrayPosition.ToArray() };
            casePositionSource.technique_common = new sourceTechnique_common()
            {
                accessor = new accessor()
                {
                    stride = 3,
                    count = vertexCount,
                    source = "#case_position_float_array",
                    param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } }
                }
            };
            casePositionSource.Item = farrayPosition;

            // normal source
            source casePositionNormal = new source() { id = "case_normal", name = "case_normal" };
            float_array farrayNormal = new float_array { id = "case_normal_float_array", count = (ulong)doubleArrayNormal.Count, Values = doubleArrayNormal.ToArray() };
            casePositionNormal.technique_common = new sourceTechnique_common()
            {
                accessor = new accessor()
                {
                    stride = 3,
                    count = normalCount,
                    source = "#case_normal_float_array",
                    param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } }
                }
            };
            casePositionNormal.Item = farrayNormal;

            // uv source
            source casePositionUV = new source() { id = "case_UV", name = "pallet_UV" };
            float_array farrayUV = new float_array { id = "case_UV_float_array", count = (ulong)doubleArrayUV.Count, Values = doubleArrayUV.ToArray() };
            casePositionUV.technique_common = new sourceTechnique_common()
            {
                accessor = new accessor()
                {
                    stride = 2,
                    count = vertexCount,
                    source = "#case_UV_float_array",
                    param = new param[] { new param() { name = "S", type = "float" }, new param() { name = "T", type = "float" } }
                }
            };
            casePositionUV.Item = farrayUV;
            // insert sources
            caseMesh.source = new source[] { casePositionSource, casePositionNormal, casePositionUV };

            // vertices
            InputLocal verticesInput = new InputLocal() { semantic = "POSITION", source = "#case_position" };
            caseMesh.vertices = new vertices() { id = "case_vertex", input = new InputLocal[] { verticesInput } };

            List<object> trianglesList = new List<object>();

            // build list of triangles
            foreach (HalfAxis.HAxis axis in HalfAxis.All)
            {
                triangles trianglesCase = new triangles() { material = string.Format("materialCase{0}", (uint)axis), count = 2 };
                trianglesCase.input = new InputLocalOffset[]
                                    {
                                        new InputLocalOffset() { semantic="VERTEX", source="#case_vertex", offset=0}
                                        , new InputLocalOffset() { semantic="NORMAL", source="#case_normal", offset=1}
                                        , new InputLocalOffset() { semantic="TEXCOORD", source="#case_UV", offset=2, set=0, setSpecified=true }
                                    };
                string triangle_string = string.Empty;
                foreach (TriangleIndices tr in box.TrianglesByFace(axis))
                    triangle_string += tr.ConvertToString(0);
                trianglesCase.p = triangle_string;
                trianglesList.Add(trianglesCase);
            }
            // build list of lines
            lines linesCase = new lines()
            {
                material = "materialCaseLines",
                count = 12,
                input = new InputLocalOffset[]
                {
                    new InputLocalOffset() { semantic="VERTEX", source="#case_vertex", offset=0}
                },
                p = "0 1 1 2 2 3 3 0 4 5 5 6 6 7 7 4 0 4 1 5 2 6 3 7"
            };
            trianglesList.Add(linesCase);

            caseMesh.Items = trianglesList.ToArray();
            return caseMesh;
        }
示例#6
0
        protected void CreateChannel(List<object> listAnimationObjects, uint caseIndex, string sData, string sDataType, string sTarget, List<double> lValues)
        {
            string sTime = string.Format("sTime_{0}_ID", caseIndex);
            string sInterp = string.Format("sInterp_{0}_ID", caseIndex);

            // source
            source source_ = new source()
            {
                id = string.Format("sValue_{0}_{1}_ID", caseIndex, sData),
                name = string.Format("sValue_{0}_{1}", caseIndex, sData),
                Item = new float_array() { id = string.Format("fa_{0}_{1}_ID", caseIndex, sData), count = (ulong)lValues.Count, Values = lValues.ToArray() },
                technique_common = new sourceTechnique_common()
                {
                    accessor = new accessor()
                    {
                        source = string.Format("#fa_{0}_{1}_ID", caseIndex, sData),
                        count = (ulong)lValues.Count,
                        stride = 1,
                        param = new param[]
                        {
                            new param() { name=sDataType, type="float" }
                        }
                    }
                }
            };
            listAnimationObjects.Add(source_);

            // sampler
            sampler sampler_ = new sampler()
            {
                id = string.Format("sampler_{0}_{1}_ID", caseIndex, sData),
                input = new InputLocal[]
                {
                    new InputLocal() { semantic="INPUT", source="#" + sTime },
                    new InputLocal() { semantic="OUTPUT", source="#" + source_.id },
                    new InputLocal() { semantic="INTERPOLATION", source="#" + sInterp }
                }
            };
            listAnimationObjects.Add(sampler_);

            // channel
            channel channel_ = new channel()
            {
                source = "#" + sampler_.id,
                target = sTarget
            };
            listAnimationObjects.Add(channel_);
        }
示例#7
0
        public void CreateAnimation(uint caseIndex, uint caseCount, List<object> listAnimationObjects, BoxPosition bPos)
        {
            const int iStep = 5;
            _zOffset = _palletSolution.PalletHeight + 100.0;

            // build list of time
            List<double> listTime = new List<double>();
            List<string> listInterp = new List<string>();
            listTime.Add(0.0);
            listInterp.Add("LINEAR");
            for (int i = 0; i < iStep; ++i)
            {
                listTime.Add(((double)caseIndex + (double)i / (double)(iStep - 1)));
                listInterp.Add("LINEAR");
            }
            listTime.Add(caseCount);
            listInterp.Add("LINEAR");

            BProperties bProperties = _palletSolution.Analysis.BProperties;
            PalletProperties pProperties = _palletSolution.Analysis.PalletProperties;

            double yOffset = 0.5 * (_palletSolution.Analysis.PalletProperties.Width - bProperties.Length);
            BoxPosition bPosFinal = GetFinalBoxPosition(caseIndex);

            List<BoxPosition> listBoxPosition = new List<BoxPosition>();
            // -1.
            listBoxPosition.Add(GetInitialBoxPosition(caseIndex));
            // 0. initialbox position
            listBoxPosition.Add(GetInitialBoxPosition(caseIndex));
            // 1. position at out storing area
            listBoxPosition.Add(new BoxPosition(new Vector3D(_xOffset, yOffset, _zOffset), HalfAxis.HAxis.AXIS_Y_P, HalfAxis.HAxis.AXIS_X_N));
            // 2. position above pallet
            listBoxPosition.Add(new BoxPosition(new Vector3D(pProperties.Length * 0.5, pProperties.Width * 0.5, _zOffset), bPosFinal.DirectionLength, bPosFinal.DirectionWidth));
            // 3. position above final position
            listBoxPosition.Add(new BoxPosition(new Vector3D(bPosFinal.Position.X, bPosFinal.Position.Y, _zOffset), bPosFinal.DirectionLength, bPosFinal.DirectionWidth));
            // 4. final position
            listBoxPosition.Add(bPosFinal);
            // 5.
            listBoxPosition.Add(bPosFinal);

            List<double> listX = new List<double>();
            List<double> listY = new List<double>();
            List<double> listZ = new List<double>();
            List<double> listRX = new List<double>();
            List<double> listRY = new List<double>();
            List<double> listRZ = new List<double>();

            BoxPosition bPprev0 = listBoxPosition[0];
            listX.Add(bPprev0.Position.X);
            listY.Add(bPprev0.Position.Y);
            listZ.Add(bPprev0.Position.Z);
            Vector3D vRotPrev0 = bPprev0.Transformation.Rotations;
            listRX.Add(vRotPrev0.X);
            listRY.Add(vRotPrev0.Y);
            listRZ.Add(vRotPrev0.Z);

            for (int i = 1; i < listBoxPosition.Count; ++i)
            {
                BoxPosition bP = listBoxPosition[i];
                listX.Add(bP.Position.X);
                listY.Add(bP.Position.Y);
                listZ.Add(bP.Position.Z);
                Vector3D vRot = bP.Transformation.Rotations;
                listRX.Add(vRot.X);
                listRY.Add(vRot.Y);
                listRZ.Add(vRot.Z);
            }

            // time
            source sTime = new source()
            {
                id = string.Format("sTime_{0}_ID", caseIndex),
                name = string.Format("sTime_{0}", caseIndex),
                Item = new float_array()
                {
                    id = string.Format("fa_time_{0}_ID", caseIndex),
                    count = (ulong)listTime.Count,
                    Values = listTime.ToArray()
                },
                technique_common = new sourceTechnique_common()
                {
                    accessor = new accessor()
                    {
                        source = string.Format("#fa_time_{0}_ID", caseIndex),
                        count = (ulong)listTime.Count,
                        stride = 1,
                        param = new param[]
                        {
                            new param() { name="TIME", type="float" }
                        }
                    }
                }
            };
            listAnimationObjects.Add(sTime);

            // interp
            source sInterp = new source()
            {
                id = string.Format("sInterp_{0}_ID", caseIndex),
                name = string.Format("sInterp_{0}", caseIndex),
                Item = new Name_array()
                {
                    id = string.Format("na_interp_{0}_ID", caseIndex),
                    count = (ulong)listInterp.Count,
                    Values = listInterp.ToArray()
                },
                technique_common = new sourceTechnique_common()
                {
                    accessor = new accessor()
                    {
                        source = string.Format("#na_interp_{0}_ID", caseIndex),
                        count = (ulong)listInterp.Count,
                        stride = 1,
                        param = new param[]
                        {
                            new param() { name="INTERPOLATION", type="name" }
                        }
                    }
                }
            };
            listAnimationObjects.Add(sInterp);

            CreateChannel(listAnimationObjects, caseIndex, "X", "TIME", string.Format("CaseNode_{0}_ID/t.X", caseIndex), listX);
            CreateChannel(listAnimationObjects, caseIndex, "Y", "TIME", string.Format("CaseNode_{0}_ID/t.Y", caseIndex), listY);
            CreateChannel(listAnimationObjects, caseIndex, "Z", "TIME", string.Format("CaseNode_{0}_ID/t.Z", caseIndex), listZ);
            CreateChannel(listAnimationObjects, caseIndex, "RX", "ANGLE", string.Format("CaseNode_{0}_ID/rx.ANGLE", caseIndex), listRX);
            CreateChannel(listAnimationObjects, caseIndex, "RY", "ANGLE", string.Format("CaseNode_{0}_ID/ry.ANGLE", caseIndex), listRY);
            CreateChannel(listAnimationObjects, caseIndex, "RZ", "ANGLE", string.Format("CaseNode_{0}_ID/rz.ANGLE", caseIndex), listRZ);

        }
示例#8
0
        private float[] GetValueFromFloatArraySource(source src, int index)
        {
            float[] values = new float[0];

            float[] floatArray = new float[0];

            if (src.Item as float_array != null)
            {
                floatArray = Array.ConvertAll<double, float>((src.Item as float_array).Values, Convert.ToSingle);
            }
            if (src.technique_common != null && src.technique_common.accessor != null)
            {
                ulong count = src.technique_common.accessor.count;
                ulong stride = src.technique_common.accessor.stride;

                values = new float[stride];
                Array.Copy(floatArray, (int)((ulong)index * stride), values, 0, (int)stride);
            }

            return values;
        }
示例#9
0
        private static float[][] GetIndividualValuesFromMatrixInput(source src)
        {
            float[][] outs = null;

            if (src.technique_common != null && src.technique_common.accessor != null)
            {
                accessor accs = src.technique_common.accessor;
                double[] vals = (src.Item as float_array).Values;

                outs = new float[16][];

                for (int i = 0; i < 16; i++)
                {
                    outs[i] = new float[accs.count];
                }
                int counter = 0;
                for (int i = 0; i < vals.Length; i += 16, counter++)
                {
                    for (int j = 0; j < 16; j++)
                    {
                        outs[j][counter] = (float)vals[i + j];
                    }
                }
            }

            return outs;
        }
示例#10
0
        private static float[][] GetIndividualParamValuesFromNonMatrixInputFloatArray(source src)
        {
            float[][] outs = null;

            if (src.technique_common != null && src.technique_common.accessor != null)
            {
                accessor accs = src.technique_common.accessor;
                double[] vals = (src.Item as float_array).Values;
                int numParams = accs.param.Length;

                outs = new float[numParams][];

                for (int i = 0; i < numParams; i++)
                {
                    outs[i] = new float[accs.count];
                }
                int counter = 0;
                for (int i = 0; i < vals.Length; i += numParams, counter++)
                {
                    for (int j = 0; j < numParams; j++)
                    {
                        outs[j][counter] = (float)vals[i + j];
                    }
                }
            }

            return outs;
        }
示例#11
0
 protected node AddToCollada(List<material> materials, List<effect> effects, List<geometry> geometries,
     List<string> visitedAttaches, bool hasTextures)
 {
     BasicAttach attach = Attach as BasicAttach;
     if (attach == null || visitedAttaches.Contains(attach.Name))
         goto skipAttach;
     visitedAttaches.Add(attach.Name);
     int m = 0;
     foreach (NJS_MATERIAL item in attach.Material)
     {
         materials.Add(new material
         {
             id = "material_" + attach.Name + "_" + m,
             name = "material_" + attach.Name + "_" + m,
             instance_effect = new instance_effect
             {
                 url = "#" + "material_" + attach.Name + "_" + m + "_eff"
             }
         });
         if (hasTextures & item.UseTexture)
         {
             effects.Add(new effect
             {
                 id = "material_" + attach.Name + "_" + m + "_eff",
                 name = "material_" + attach.Name + "_" + m + "_eff",
                 Items = new effectFx_profile_abstractProfile_COMMON[]
                 {
                     new effectFx_profile_abstractProfile_COMMON
                     {
                         Items = new object[]
                         {
                             new common_newparam_type
                             {
                                 sid = "material_" + attach.Name + "_" + m + "_eff_surface",
                                 /*Item = new Collada141.fx_sampler2D_common()
                                  { instance_image = new Collada141.instance_image() { url = "#image_" + (item.TextureID + 1).ToString(System.Globalization.NumberFormatInfo.InvariantInfo) } },
                                  ItemElementName = Collada141.ItemChoiceType.sampler2D*/
                                 Item = new fx_surface_common
                                 {
                                     type = fx_surface_type_enum.Item2D,
                                     init_from =
                                         new fx_surface_init_from_common[]
                                         {
                                             new fx_surface_init_from_common
                                             {
                                                 Value = "image_" + (item.TextureID + 1).ToString(NumberFormatInfo.InvariantInfo)
                                             }
                                         }
                                 },
                                 ItemElementName = ItemChoiceType.surface
                             }
                         },
                         technique = new effectFx_profile_abstractProfile_COMMONTechnique
                         {
                             sid = "standard",
                             Item = new effectFx_profile_abstractProfile_COMMONTechniquePhong
                             {
                                 ambient = new common_color_or_texture_type
                                 {
                                     Item = new common_color_or_texture_typeTexture
                                     {
                                         texture = "material_" + attach.Name + "_" + m + "_eff_surface",
                                         texcoord = "CHANNEL0"
                                     }
                                 },
                                 diffuse = new common_color_or_texture_type
                                 {
                                     Item = new common_color_or_texture_typeColor
                                     {
                                         Values =
                                             new double[]
                                             {
                                                 item.DiffuseColor.R / 255d, item.DiffuseColor.G / 255d, item.DiffuseColor.B / 255d,
                                                 item.UseAlpha ? item.DiffuseColor.A / 255d : 1
                                             }
                                     }
                                 }
                             }
                         }
                     }
                 }
             });
         }
         else
         {
             effects.Add(new effect
             {
                 id = "material_" + attach.Name + "_" + m + "_eff",
                 name = "material_" + attach.Name + "_" + m + "_eff",
                 Items = new effectFx_profile_abstractProfile_COMMON[]
                 {
                     new effectFx_profile_abstractProfile_COMMON
                     {
                         technique = new effectFx_profile_abstractProfile_COMMONTechnique
                         {
                             sid = "standard",
                             Item = new effectFx_profile_abstractProfile_COMMONTechniquePhong
                             {
                                 diffuse = new common_color_or_texture_type
                                 {
                                     Item = new common_color_or_texture_typeColor
                                     {
                                         Values =
                                             new double[]
                                             {
                                                 item.DiffuseColor.R / 255d, item.DiffuseColor.G / 255d, item.DiffuseColor.B / 255d,
                                                 item.UseAlpha ? item.DiffuseColor.A / 255d : 1
                                             }
                                     }
                                 }
                             }
                         }
                     }
                 }
             });
         }
         m++;
     }
     List<double> verts = new List<double>();
     foreach (Vertex item in attach.Vertex)
     {
         verts.Add(item.X);
         verts.Add(item.Y);
         verts.Add(item.Z);
     }
     source pos = new source
     {
         id = attach.Name + "_position",
         Item = new float_array
         {
             id = attach.Name + "_position_array",
             count = (ulong)verts.Count,
             Values = verts.ToArray()
         },
         technique_common = new sourceTechnique_common
         {
             accessor = new accessor
             {
                 source = "#" + attach.Name + "_position_array",
                 count = (ulong)(verts.Count / 3),
                 stride = 3,
                 param =
                     new param[]
                     {
                         new param { name = "X", type = "float" }, new param { name = "Y", type = "float" },
                         new param { name = "Z", type = "float" }
                     }
             }
         }
     };
     verts = new List<double>();
     foreach (Vertex item in attach.Normal)
     {
         verts.Add(item.X);
         verts.Add(item.Y);
         verts.Add(item.Z);
     }
     source nor = new source
     {
         id = attach.Name + "_normal",
         Item = new float_array
         {
             id = attach.Name + "_normal_array",
             count = (ulong)verts.Count,
             Values = verts.ToArray()
         },
         technique_common = new sourceTechnique_common
         {
             accessor = new accessor
             {
                 source = "#" + attach.Name + "_normal_array",
                 count = (ulong)(verts.Count / 3),
                 stride = 3,
                 param =
                     new param[]
                     {
                         new param { name = "X", type = "float" }, new param { name = "Y", type = "float" },
                         new param { name = "Z", type = "float" }
                     }
             }
         }
     };
     List<source> srcs = new List<source> { pos, nor };
     foreach (NJS_MESHSET mitem in attach.Mesh)
     {
         if (mitem.UV != null)
         {
             verts = new List<double>();
             foreach (UV item in mitem.UV)
             {
                 verts.Add(item.U);
                 verts.Add(-item.V);
             }
             srcs.Add(new source
             {
                 id = mitem.UVName,
                 Item = new float_array
                 {
                     id = mitem.UVName + "_array",
                     count = (ulong)verts.Count,
                     Values = verts.ToArray()
                 },
                 technique_common = new sourceTechnique_common
                 {
                     accessor = new accessor
                     {
                         source = "#" + mitem.UVName + "_array",
                         count = (ulong)(verts.Count / 2),
                         stride = 2,
                         param = new param[] { new param { name = "S", type = "float" }, new param { name = "T", type = "float" } }
                     }
                 }
             });
         }
     }
     List<triangles> tris = new List<triangles>();
     foreach (NJS_MESHSET mesh in attach.Mesh)
     {
         bool hasVColor = mesh.VColor != null;
         bool hasUV = mesh.UV != null;
         uint currentstriptotal = 0;
         foreach (Poly poly in mesh.Poly)
         {
             List<uint> inds = new List<uint>();
             switch (mesh.PolyType)
             {
                 case Basic_PolyType.Triangles:
                     for (uint i = 0; i < 3; i++)
                     {
                         inds.Add(poly.Indexes[i]);
                         if (hasUV)
                             inds.Add(currentstriptotal + i);
                     }
                     currentstriptotal += 3;
                     break;
                 case Basic_PolyType.Quads:
                     for (uint i = 0; i < 3; i++)
                     {
                         inds.Add(poly.Indexes[i]);
                         if (hasUV)
                             inds.Add(currentstriptotal + i);
                     }
                     for (uint i = 1; i < 4; i++)
                     {
                         inds.Add(poly.Indexes[i]);
                         if (hasUV)
                             inds.Add(currentstriptotal + i);
                     }
                     currentstriptotal += 4;
                     break;
                 case Basic_PolyType.NPoly:
                 case Basic_PolyType.Strips:
                     bool flip = !((Strip)poly).Reversed;
                     for (int k = 0; k < poly.Indexes.Length - 2; k++)
                     {
                         flip = !flip;
                         if (!flip)
                         {
                             for (uint i = 0; i < 3; i++)
                             {
                                 inds.Add(poly.Indexes[k + i]);
                                 if (hasUV)
                                     inds.Add(currentstriptotal + i);
                             }
                         }
                         else
                         {
                             inds.Add(poly.Indexes[k + 1]);
                             if (hasUV)
                                 inds.Add(currentstriptotal + 1);
                             inds.Add(poly.Indexes[k]);
                             if (hasUV)
                                 inds.Add(currentstriptotal);
                             inds.Add(poly.Indexes[k + 2]);
                             if (hasUV)
                                 inds.Add(currentstriptotal + 2);
                         }
                         currentstriptotal += 1;
                     }
                     currentstriptotal += 2;
                     break;
             }
             string[] indstr = new string[inds.Count];
             for (int i = 0; i < inds.Count; i++)
                 indstr[i] = inds[i].ToString(NumberFormatInfo.InvariantInfo);
             List<InputLocalOffset> inp = new List<InputLocalOffset>
             {
                 new InputLocalOffset { semantic = "VERTEX", offset = 0, source = "#" + attach.Name + "_vertices" }
             };
             if (hasUV)
             {
                 inp.Add(new InputLocalOffset
                 {
                     semantic = "TEXCOORD",
                     offset = 1,
                     source = "#" + mesh.UVName,
                     setSpecified = true
                 });
             }
             tris.Add(new triangles
             {
                 material = "material_" + attach.Name + "_" + mesh.MaterialID,
                 count = (ulong)(inds.Count / (hasUV ? 6 : 3)),
                 input = inp.ToArray(),
                 p = string.Join(" ", indstr)
             });
         }
     }
     geometries.Add(new geometry
     {
         id = attach.Name,
         name = attach.Name,
         Item = new mesh
         {
             source = srcs.ToArray(),
             vertices = new vertices
             {
                 id = attach.Name + "_vertices",
                 input = new InputLocal[]
                 {
                     new InputLocal
                     {
                         semantic = "POSITION",
                         source = "#" + attach.Name + "_position"
                     },
                     new InputLocal
                     {
                         semantic = "NORMAL",
                         source = "#" + attach.Name + "_normal"
                     }
                 }
             },
             Items = tris.ToArray()
         }
     });
     skipAttach:
     node node = new node
     {
         id = Name,
         name = Name,
         Items = new object[]
         {
             new TargetableFloat3 { sid = "translate", Values = new double[] { Position.X, Position.Y, Position.Z } },
             new rotate { sid = "rotateZ", Values = new double[] { 0, 0, 1, Rotation.ZDeg } },
             new rotate { sid = "rotateX", Values = new double[] { 1, 0, 0, Rotation.XDeg } },
             new rotate { sid = "rotateY", Values = new double[] { 0, 1, 0, Rotation.YDeg } },
             new TargetableFloat3 { sid = "scale", Values = new double[] { Scale.X, Scale.Y, Scale.Z } }
         },
         ItemsElementName = new ItemsChoiceType2[]
         {
             ItemsChoiceType2.translate,
             ItemsChoiceType2.rotate,
             ItemsChoiceType2.rotate,
             ItemsChoiceType2.rotate,
             ItemsChoiceType2.scale
         }
     };
     if (attach != null)
     {
         List<instance_material> mats = new List<instance_material>();
         foreach (NJS_MESHSET item in attach.Mesh)
         {
             mats.Add(new instance_material
             {
                 symbol = "material_" + attach.Name + "_" + item.MaterialID,
                 target = "#" + "material_" + attach.Name + "_" + item.MaterialID
             });
         }
         node.instance_geometry = new instance_geometry[]
         {
             new instance_geometry
             {
                 url = "#" + attach.Name,
                 bind_material = new bind_material { technique_common = mats.ToArray() }
             }
         };
     }
     List<node> childnodes = new List<node>();
     foreach (NJS_OBJECT item in Children)
         childnodes.Add(item.AddToCollada(materials, effects, geometries, visitedAttaches, hasTextures));
     node.node1 = childnodes.ToArray();
     return node;
 }
示例#12
0
        static CL.source CreateSource(string id, Func <int, Vector4> get, int components, int len)
        {
            var src = new CL.source();

            src.id = id;
            var floats = new float[len * components];

            for (int i = 0; i < len; i++)
            {
                var v4 = get(i);
                floats[i * components]     = v4.X;
                floats[i * components + 1] = v4.Y;
                if (components > 2)
                {
                    floats[i * components + 2] = v4.Z;
                }
                if (components > 3)
                {
                    floats[i * components + 3] = v4.W;
                }
            }
            string arrId = id + "-array";

            src.Item = new CL.float_array()
            {
                id   = arrId,
                Text = string.Join(" ", floats.Select((x) => x.ToString(CultureInfo.InvariantCulture)))
            };
            src.technique_common = new CL.sourceTechnique_common();
            var acc = new CL.accessor()
            {
                source = "#" + arrId,
                count  = (ulong)len,
                stride = (ulong)components
            };

            src.technique_common.accessor = acc;
            if (components == 2)
            {
                acc.param = new CL.param[] {
                    new CL.param()
                    {
                        name = "U", type = "float"
                    },
                    new CL.param()
                    {
                        name = "V", type = "float"
                    }
                };
            }
            else if (components == 3)
            {
                acc.param = new CL.param[] {
                    new CL.param()
                    {
                        name = "X", type = "float"
                    },
                    new CL.param()
                    {
                        name = "Y", type = "float"
                    },
                    new CL.param()
                    {
                        name = "Z", type = "float"
                    }
                };
            }
            else if (components == 4)
            {
                acc.param = new CL.param[] {
                    new CL.param()
                    {
                        name = "R", type = "float"
                    },
                    new CL.param()
                    {
                        name = "G", type = "float"
                    },
                    new CL.param()
                    {
                        name = "B", type = "float"
                    },
                    new CL.param()
                    {
                        name = "A", type = "float"
                    }
                };
            }
            return(src);
        }
示例#13
0
        static ExportModel ProcessModel(ModelFile mdl, ResourceManager resources)
        {
            mdl.Path = mdl.Path.Replace(' ', '_');
            var ex = new ExportModel();

            for (int midx = 0; midx < mdl.Levels.Length; midx++)
            {
                var lvl       = mdl.Levels[midx];
                var processed = ProcessRef(lvl, resources);
                var geo       = new CL.geometry();
                geo.name = geo.id = mdl.Path + "-level" + midx;
                var mesh = new CL.mesh();
                geo.Item = mesh;
                CL.source positions;
                CL.source normals = null;
                CL.source colors  = null;
                CL.source tex1    = null;
                CL.source tex2    = null;
                int       idxC    = 1;
                positions = CreateSource(
                    geo.name + "-positions",
                    (k) => new Vector4(processed.Vertices[k].Position, 0),
                    3, processed.Vertices.Length);
                mesh.vertices = new CL.vertices()
                {
                    id    = geo.name + "-vertices",
                    input = new CL.InputLocal[] { new CL.InputLocal()
                                                  {
                                                      semantic = "POSITION", source = "#" + positions.id
                                                  } }
                };
                var sources = new List <CL.source>()
                {
                    positions
                };
                if ((processed.FVF & D3DFVF.NORMAL) == D3DFVF.NORMAL)
                {
                    normals = CreateSource(
                        geo.name + "-normals",
                        (k) => new Vector4(processed.Vertices[k].Normal, 0),
                        3, processed.Vertices.Length);
                    sources.Add(normals);
                    idxC++;
                }
                if ((processed.FVF & D3DFVF.DIFFUSE) == D3DFVF.DIFFUSE)
                {
                    colors = CreateSource(
                        geo.name + "-color",
                        (k) =>
                    {
                        var c = Color4.FromRgba(processed.Vertices[k].Diffuse);
                        return(new Vector4(c.R, c.G, c.B, c.A));
                    }, 4, processed.Vertices.Length);
                    sources.Add(colors);
                    idxC++;
                }
                bool doTex1, doTex2 = false;
                if ((processed.FVF & D3DFVF.TEX2) == D3DFVF.TEX2)
                {
                    doTex1 = doTex2 = true;
                }
                else if ((processed.FVF & D3DFVF.TEX1) == D3DFVF.TEX1)
                {
                    doTex1 = true;
                }
                else
                {
                    doTex1 = doTex2 = false;
                }
                if (doTex1)
                {
                    tex1 = CreateSource(
                        geo.name + "-tex1",
                        (k) => new Vector4(processed.Vertices[k].TextureCoordinate, 0, 0),
                        2, processed.Vertices.Length);
                    sources.Add(tex1);
                    idxC++;
                }
                if (doTex2)
                {
                    tex2 = CreateSource(
                        geo.name + "-tex2",
                        (k) => new Vector4(processed.Vertices[k].TextureCoordinateTwo, 0, 0),
                        2, processed.Vertices.Length);
                    sources.Add(tex2);
                    idxC++;
                }
                mesh.source = sources.ToArray();
                var items = new List <object>();
                foreach (var dc in processed.Drawcalls)
                {
                    if (!ex.Materials.Any((x) => x.Name == dc.Material.Name))
                    {
                        ex.Materials.Add(dc.Material);
                    }
                    var trs = new CL.triangles();
                    trs.count    = (ulong)(dc.Indices.Length / 3);
                    trs.material = dc.Material.Name + "-material";
                    List <int> pRefs = new List <int>(dc.Indices.Length * idxC);
                    List <CL.InputLocalOffset> inputs = new List <CL.InputLocalOffset>()
                    {
                        new CL.InputLocalOffset()
                        {
                            semantic = "VERTEX", source = "#" + geo.id + "-vertices", offset = 0
                        }
                    };
                    ulong off = 1;
                    if (normals != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "NORMAL",
                            source   = "#" + normals.id,
                            offset   = off++
                        });
                    }
                    if (colors != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "COLOR",
                            source   = "#" + colors.id,
                            offset   = off++
                        });
                    }
                    if (tex1 != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "TEXCOORD",
                            source   = "#" + tex1.id,
                            offset   = off++
                        });
                    }
                    if (tex2 != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "TEXCOORD",
                            source   = "#" + tex2.id,
                            offset   = off++
                        });
                    }
                    trs.input = inputs.ToArray();
                    for (int i = 0; i < dc.Indices.Length; i++)
                    {
                        for (int j = 0; j < idxC; j++)
                        {
                            pRefs.Add(dc.Indices[i]);
                        }
                    }
                    trs.p = string.Join(" ", pRefs.ToArray());
                    items.Add(trs);
                }
                mesh.Items = items.ToArray();
                ex.Geometries.Add(geo);
            }
            return(ex);
        }