예제 #1
0
        private MaterialComponent GetMaterial(TextureTag texTag)
        {
            Material material = texTag.GetMaterial() as Material;

            if (material == null)
            {
                return(null);
            }

            long materialUid = material.RefUID();

            MaterialComponent mcRet;

            if (_materialCache.TryGetValue(materialUid, out mcRet))
            {
                return(mcRet);
            }

            using (BaseContainer materialData = material.GetData())
            {
                mcRet = new MaterialComponent();
                // Just for debugging purposes
                for (int i = 0, id = 0; -1 != (id = materialData.GetIndexId(i)); i++)
                {
                    if (materialData.GetTypeC4D(id) == C4dApi.DA_LONG)
                    {
                        int iii = materialData.GetInt32(id);
                    }
                    if (materialData.GetTypeC4D(id) == C4dApi.DA_REAL)
                    {
                        double d = materialData.GetFloat(id);
                    }
                    else if (materialData.GetTypeC4D(id) == C4dApi.DA_VECTOR)
                    {
                        double3 v = materialData.GetVector(id);
                    }
                }

                if (material.GetChannelState(C4dApi.CHANNEL_COLOR))
                {
                    BaseChannel diffuseChannel = material.GetChannel(C4dApi.CHANNEL_COLOR);
                    if (diffuseChannel != null)
                    {
                        mcRet.Diffuse = new MatChannelContainer();

                        using (BaseContainer data = diffuseChannel.GetData())
                        {
                            mcRet.Diffuse.Color   = (float3)(data.GetVector(C4dApi.BASECHANNEL_COLOR_EX) * data.GetFloat(C4dApi.BASECHANNEL_BRIGHTNESS_EX));
                            mcRet.Diffuse.Mix     = (float)data.GetFloat(C4dApi.BASECHANNEL_MIXSTRENGTH_EX);
                            mcRet.Diffuse.Texture = GetTexture(data, diffuseChannel);
                        }
                    }
                }

                if (material.GetChannelState(C4dApi.CHANNEL_SPECULARCOLOR))
                {
                    BaseChannel specularChannel = material.GetChannel(C4dApi.CHANNEL_SPECULARCOLOR);
                    if (specularChannel != null)
                    {
                        mcRet.Specular = new SpecularChannelContainer();

                        using (BaseContainer data = specularChannel.GetData())
                        {
                            mcRet.Specular.Color     = (float3)(data.GetVector(C4dApi.BASECHANNEL_COLOR_EX) * data.GetFloat(C4dApi.BASECHANNEL_BRIGHTNESS_EX));
                            mcRet.Specular.Mix       = (float)data.GetFloat(C4dApi.BASECHANNEL_MIXSTRENGTH_EX);
                            mcRet.Specular.Texture   = GetTexture(data, specularChannel);
                            mcRet.Specular.Shininess =
                                CalculateShininess((float)materialData.GetFloat(C4dApi.MATERIAL_SPECULAR_WIDTH));
                            mcRet.Specular.Intensity = (float)(1.5 * materialData.GetFloat(C4dApi.MATERIAL_SPECULAR_HEIGHT));
                        }
                    }
                }

                if (material.GetChannelState(C4dApi.CHANNEL_LUMINANCE))
                {
                    BaseChannel emissiveChannel = material.GetChannel(C4dApi.CHANNEL_LUMINANCE);
                    if (emissiveChannel != null)
                    {
                        mcRet.Emissive = new MatChannelContainer();

                        using (BaseContainer data = emissiveChannel.GetData())
                        {
                            mcRet.Emissive.Color   = (float3)(data.GetVector(C4dApi.BASECHANNEL_COLOR_EX) * data.GetFloat(C4dApi.BASECHANNEL_BRIGHTNESS_EX));
                            mcRet.Emissive.Mix     = (float)data.GetFloat(C4dApi.BASECHANNEL_MIXSTRENGTH_EX);
                            mcRet.Emissive.Texture = GetTexture(data, emissiveChannel);
                        }
                    }
                }

                if (material.GetChannelState(C4dApi.CHANNEL_NORMAL))
                {
                    BaseChannel bumpChannel = material.GetChannel(C4dApi.CHANNEL_NORMAL);
                    if (bumpChannel != null)
                    {
                        mcRet.Bump = new BumpChannelContainer();

                        using (BaseContainer data = bumpChannel.GetData())
                        {
                            mcRet.Bump.Intensity = (float)materialData.GetFloat(C4dApi.MATERIAL_NORMAL_STRENGTH);
                            mcRet.Bump.Texture   = GetTexture(data, bumpChannel);
                        }
                    }
                }
            }

            /*
             * // Just for debugging purposes
             * BaseChannel specularChannel = material.GetChannel(C4dApi.CHANNEL_SPECULARCOLOR);
             * if (specularChannel != null)
             * {
             *  mcRet.HasSpecular = true;
             *  BaseContainer data = specularChannel.GetData();
             *  for (int i=0, id=0; -1 != (id = data.GetIndexId(i)); i++)
             *  {
             *      if (data.GetType(id) == C4dApi.DA_REAL)
             *      {
             *          double d = data.GetReal(id);
             *      }
             *      else if (data.GetType(id) == C4dApi.DA_VECTOR)
             *      {
             *          double3 v = data.GetVector(id);
             *      }
             *
             *
             *  }
             *
             * }
             */
            _materialCache[materialUid] = mcRet;
            return(mcRet);
        }
예제 #2
0
        /// <summary>

        /// This method tries to make the best out of C4Ds seldom relationship between objects with
        /// multiple materials which can or can not be restricted to polygon selections and one or more UV sets.
        /// But there are unhandled cases:
        /// Multple UV tags are not supported. Overlapping polygon selections are probably handled differently.
        /// The awkward side effects when changing the tags' order in C4D are not reproduced.
        /// </summary>
        /// <param name="ob"></param>
        /// <param name="snc"></param>


        private void VisitObject(BaseObject ob, SceneNodeContainer snc)
        {
            Collection <TextureTag>           textureTags   = new Collection <TextureTag>();
            Dictionary <string, SelectionTag> selectionTags = new Dictionary <string, SelectionTag>();
            UVWTag      uvwTag    = null;
            CAWeightTag weightTag = null;

            // SCRATCH
            // var targetComponent = new TargetComponent {ExtraInfo = aStr, Radius = anInt};
            // snc.AddComponent(targetComponent);

            // Iterate over the object's tags
            for (BaseTag tag = ob.GetFirstTag(); tag != null; tag = tag.GetNext())
            {
                // GeneralTag
                if (1036156 == tag.GetTypeC4D())
                {
                    var    di    = tag.GetDataInstance();
                    int    anInt = di.GetInt32(10000);
                    string aStr  = di.GetString(10001);
                    Logger.Debug("Found a GeneralTag with TheInt=" + anInt + " and TheString = \"" + aStr + "\"");
                    // var targetComponent = new TargetComponent {ExtraInfo = aStr, Radius = anInt};
                    // snc.AddComponent(targetComponent);
                }

                // CAWeightTag - Save data to create the weight list later
                CAWeightTag wTag = tag as CAWeightTag;
                if (wTag != null)
                {
                    weightTag = wTag;
                    continue;
                }

                // TextureTag (Material - there might be more than one)
                TextureTag tex = tag as TextureTag;
                if (tex != null)
                {
                    textureTags.Add(tex);
                    continue;
                }
                // UVWTag - the texutre coordinates. We handle only one
                UVWTag uvw = tag as UVWTag;
                if (uvw != null)
                {
                    if (uvwTag == null)
                    {
                        uvwTag = uvw;
                    }
                    else
                    {
                        Logger.Error("Object " + ob.GetName() + " contains more than one uv-coordinates-tag. Cannot handle this. Only the first texture tag will be recognized.");
                    }
                    continue;
                }
                // Selection tag. Only recognize the polygon selections as they might be referenced in a TextureTag
                SelectionTag selection  = tag as SelectionTag;
                string       selTagName = tag.GetName();
                if (selection != null && selection.GetTypeC4D() == C4dApi.Tpolygonselection && !string.IsNullOrEmpty(selTagName))    // One Type and three TypeIDs - You C4D programmer guys really suck
                {
                    selectionTags[selTagName] = selection;
                }
                // XPresso Tags - TBD
                XPressoTag xPresso = tag as XPressoTag;
                if (xPresso != null)
                {
                    // Handle XPresso tag
                    continue;
                }
            }

            TextureTag lastUnselectedTag = null;
            Collection <KeyValuePair <SelectionTag, TextureTag> > texSelList = new Collection <KeyValuePair <SelectionTag, TextureTag> >(); // Abused KeyValuePair. Should have been Pair...

            // Now iterate over the textureTags
            foreach (TextureTag texture in textureTags)
            {
                string selRef = "";
                using (BaseContainer texData = texture.GetData())
                {
                    selRef = texData.GetString(C4dApi.TEXTURETAG_RESTRICTION);
                }
                if (string.IsNullOrEmpty(selRef))
                {
                    // This material is not restricted to any polygon selection
                    lastUnselectedTag = texture;
                }
                else
                {
                    SelectionTag sel;
                    if (selectionTags.TryGetValue(selRef, out sel))
                    {
                        texSelList.Add(new KeyValuePair <SelectionTag, TextureTag>(sel, texture));
                    }
                }
            }

            // At this point we have the last texture tag not restricted to a seletion. This will become the Material of this FuseeObjectContainer
            // no matter if this object contains geometry or not
            if (lastUnselectedTag != null)
            {
                AddComponent(snc, GetMaterial(lastUnselectedTag));
            }

            // Further processing only needs to take place if the object contains any geometry at all.
            PolygonObject polyOb = ob as PolygonObject;

            // Check whether the object contains an unpolygonized mesh
            if (polyOb == null)
            {
                polyOb = ob.GetCache(null) as PolygonObject;
            }

            if (polyOb != null)
            {
                float3[] normalOb = polyOb.CreatePhongNormals();

                // Initialize the polygon index set
                int           nPolys   = polyOb.GetPolygonCount();
                HashSet <int> polyInxs = new HashSet <int>();
                for (int i = 0; i < nPolys; i++)
                {
                    polyInxs.Add(i);
                }

                foreach (KeyValuePair <SelectionTag, TextureTag> texSelItem in texSelList)
                {
                    HashSet <int> polyInxsSubset = new HashSet <int>();
                    BaseSelect    bs             = texSelItem.Key.GetBaseSelect();
                    int           nSegments      = bs.GetSegments();
                    for (int iSeg = 0; iSeg < nSegments; iSeg++)
                    {
                        int from = bs.GetRangeA(iSeg);
                        int to   = bs.GetRangeB(iSeg);
                        for (int iSel = from; iSel <= to; iSel++)
                        {
                            polyInxs.Remove(iSel);
                            polyInxsSubset.Add(iSel);
                        }
                    }

                    // Now generate Polygons for this subset
                    if (polyInxsSubset.Count > 0)
                    {
                        if (snc.Children == null)
                        {
                            snc.Children = new List <SceneNodeContainer>();
                        }

                        SceneNodeContainer subSnc = new SceneNodeContainer();

                        AddComponent(subSnc, new TransformComponent()
                        {
                            Translation = new float3(0, 0, 0),
                            Rotation    = new float3(0, 0, 0),
                            Scale       = new float3(1, 1, 1)
                        });

                        AddComponent(subSnc, GetMaterial(texSelItem.Value));
                        subSnc.Name = snc.Name + "_" + texSelItem.Key.GetName();
                        AddComponent(subSnc, GetMesh(polyOb, normalOb, uvwTag, polyInxsSubset));
                        _weightManager.AddWeightData(subSnc, polyOb, weightTag, polyInxsSubset);

                        snc.Children.Add(subSnc);
                    }
                }

                // The remaining polygons directly go into the original mesh

                AddComponent(snc, GetMesh(polyOb, normalOb, uvwTag, polyInxs));
                _weightManager.AddWeightData(snc, polyOb, weightTag, polyInxs);
            }
            else if (ob.GetTypeC4D() == C4dApi.Olight)
            {
                using (BaseContainer lightData = ob.GetData())
                    // Just for debugging purposes
                    for (int i = 0, id = 0; -1 != (id = lightData.GetIndexId(i)); i++)
                    {
                        if (lightData.GetTypeC4D(id) == C4dApi.DA_LONG)
                        {
                            int iii = lightData.GetInt32(id);
                        }
                        if (lightData.GetTypeC4D(id) == C4dApi.DA_REAL)
                        {
                            double d = lightData.GetFloat(id);
                        }
                        else if (lightData.GetTypeC4D(id) == C4dApi.DA_VECTOR)
                        {
                            double3 v = lightData.GetVector(id);
                        }
                    }
                ;
            }
        }