XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

CustomContent processor and Splines...

Last post 1/26/2009 8:59 PM by Pixelstudio. 6 replies.
  • 1/20/2009 9:55 PM

    CustomContent processor and Splines...

    Hello people.

    I'm working on a idea to get an easy workflow between 3dsmax<>XNA. first i have to create something that can import splines.
    The idea is a follow: in 3dsmax i'll export all vertex points of a spline to a "splines.spl".
    In XNA i would need a custom content reader/writer/processor thingy.

    Well, i looked at a couple of tutorials, one of those is found here: http://msdn.microsoft.com/en-us/library/bb447754.aspx
    So i created a new project called Splines and added the Curve3D class that was included with a scripedCamera sample.

    first a class to hold the input from the file
    SplineSource:
    namespace Splines 
        class SplineSource 
        { 
            public SplineSource(string sourceCode) 
        { 
            this.sourceCode = sourceCode; 
        } 
     
        private string sourceCode; 
        public string SourceCode { get { return sourceCode; } } 
        } 

    Then a importer :
    SplineImporter

    namespace Splines 
        [ContentImporter(".spl", CacheImportedData=true, DisplayName = "Spline Importer", DefaultProcessor = "SplineProcessor")] 
        class SplineImporter : ContentImporter<SplineSource> 
        { 
            public override SplineSource Import(string filename, ContentImporterContext context) 
            { 
                string sourceCode = System.IO.File.ReadAllText(filename); 
                return new SplineSource(sourceCode); 
            } 
        } 

    And a processor to process all the data. (this gives an error if i use the input data e.g uncomment the line)

    namespace Splines 
        [ContentProcessor(DisplayName = "Spline Processor")] 
        class SplineProcessor : ContentProcessor<SplineSource, SplineCurve> 
        { 
            public override SplineCurve Process(SplineSource input, ContentProcessorContext context) 
            { 
                string[] elements = input.SourceCode.Split(','); 
                Curve3D spCurve = new Curve3D(); 
     
                // spCurve.AddPoint(new Vector3(Convert.ToSingle(elements[0]), Convert.ToSingle(elements[1]), Convert.ToSingle(elements[2])), 0); 
                spCurve.AddPoint(new Vector3(1,1,1), 0); 
     
                return new SplineCurve(spCurve); 
                //throw new NotImplementedException(); 
            } 
        } 

    and a splinecurve class:
    namespace Splines 
        public class SplineCurve 
        { 
            public SplineCurve(Curve3D splineCurve3D)  
            { this.splineCurve3D = new Curve3D();} 
             
            private Curve3D splineCurve3D; 
            public Curve3D SplineCurve3D 
            { 
                get 
                {return (Curve3D) splineCurve3D;  } 
            } 
     
        } 


    But for testing thingies i just a 1,1,1 to a curve3D point....
    So next a writer to write stuff (as a xnb file i think) ?
    namespace Splines 
        [ContentTypeWriter] 
        public class SplineWriter : ContentTypeWriter<SplineCurve> 
        { 
            protected override void Write(ContentWriter output, SplineCurve value) 
            { 
                output.Write(value.SplineCurve3D.curveX.ToString()); 
                output.Write(value.SplineCurve3D.curveY.ToString()); 
                output.Write(value.SplineCurve3D.curveZ.ToString()); 
            } 
     
            public override string GetRuntimeReader(TargetPlatform targetPlatform) 
            { 
                return "Splines.SplineReader, Splines, version=1.0.0.0, Culture=neutral"
                 
                //return typeof(SplineCurve).AssemblyQualifiedName; 
            } 
        } 


    And Finally a reader to read it back out "sjees but well" :)
    namespace Splines 
        public class SplineReader :  ContentTypeReader<SplineCurve> 
        { 
            protected override SplineCurve Read(ContentReader input, SplineCurve existingInstance) 
            { 
                Curve3D myCurve = new Curve3D(); 
                Vector3 point = new Vector3(input.ReadSingle(), 
                                            input.ReadSingle(), 
                                            input.ReadSingle()); 
                myCurve.AddPoint(point,1); 
                return new SplineCurve(myCurve); 
            } 
        } 



    And as you can guess when i add the references to my project, add a .spl file (that contains "1,1,1") and try to load it... it does not work. i get weird numbers and not 1,1,1.....
    You can also see that the reading/writing and such is far from complete. cause it should read all the lines, write all the lines, etc.
    But that i'll do when the basic thing works.

    So perhpas you guys can have a look at this and help me out.
    The maxscript is very easy for me and when i got it all working i'll upload it somewhere so maybe more people can use it.... :)

    Cheers!

    Martijn




  • 1/20/2009 11:40 PM In reply to

    Re: CustomContent processor and Splines...

    Sounds like a cool project!

    Your problem is that the ContentTypeWriter is saving out strings (it starts with a 32 bit float, but then calls ToString before saving it out), while the ContentTypeReader is reading 32 bit floating point values. These need to match: either write and read strings in both places, or (better) write and read singles in both places.
    XNA Framework Developer - blog - homepage
  • 1/21/2009 9:56 PM In reply to

    Re: CustomContent processor and Splines...

    Ah well you are right about that!. to bad i only have time in the evening to do some xna :) not so awake then hehe.


    But well, again i'm trying further.
    And i do get the the idea right.

    The processor should create the Curve3D and add all the point that are read in.
    Next the writer should write the thing to a file and after that the reader would read it back in. (hope this process is correct?)

    Well what i dont get right is the writer/reader part.
    I thought, i have a splineCurve filled with keys, why can't i write this and read this back.
    if i try output.WriteObject<SplineCurve>(value); in the writer i get some weird invalidoperationexecption...

    if i do output.WriteObject<Curve3D>(value.SplineCurve3D); i get some like "cannot find a contenttypewriter implementation..

    So both don't work....
    if i create a other new vector3 and write that as a object it works. but well how do i get all the vector3's back....
    Hmm kinda getting lost.


    Hope some1 can help me out!
    If the curve3D class is needed i can past that also....

    thx in advance!


  • 1/21/2009 10:39 PM In reply to

    Re: CustomContent processor and Splines...

    What exactly do you mean by "weird invalidoperationexecption"? What does the exception message say? What is the callstack?

    Likewise for the second exception. I'm thinking the actual message has more information than you posted here?
    XNA Framework Developer - blog - homepage
  • 1/22/2009 9:16 AM In reply to

    Re: CustomContent processor and Splines...

    Ah okey here is some more info about the errors i get:

    when i do output.writeObject<SplineCurve>(value);
    InvalidOperationException: Cyclic reference found while serializing Splines.SpliceCurve. you may be missing a ContentSerializer attribute.SharedResource flag.

    When i do output.writeObject<Curve3D>(value.SplineCurve3D);
    Unsupported type. Cannot find a ContentTypeWriter implementaion for Splines.Curve3D.


  • 1/22/2009 7:41 PM In reply to

    Re: CustomContent processor and Splines...

    Ok.

    The key here is to understand how WriteObject works: it just looks up the ContentTypeWriter for whatever type you asked it to write, then calls into that write function.

    So, in your first version the ContentTypeWriter for SplineCurve objects is being passed a SplineCurve value to write out. It does this by calling WriteObject on that exact same value, which looks up the ContentTypeWriter for SplineCurve objects, and calls into it, which looks up the ContentTypeWriter for SplineCurve objects, and calls into it, which looks up the ContentTypeWriter for SplineCurve objects, and calls into it...

    Your Write function has to actually write out some data, not just call back into itself!

    The second version is closer to what you want, as it is actually writing out the individual pieces of data from inside the object. This comes to grief because the type you are trying to write out is a Curve3D object, and you haven't provided a ContentTypeWriter for Curve3D. The solution is to do provide one: you need a separate ContentTypeWriter for every custom type you want to serialize.
    XNA Framework Developer - blog - homepage
  • 1/26/2009 8:59 PM In reply to

    Re: CustomContent processor and Splines...

    Indeed you are correct!

    After looking at some more examples and tutorials i finally got how this thing works.
    So i recreated the writer and reader and it works now jaj :)

    i also worked on the processor and got this working now.
    For now i will only support one spline object. maybe i can add some more functionality later on.


    Now i'm writing the export script for 3dsmax.
    i now export all the vertex points from a spline to a file.
    But the curve3D requires a time value that is linked to the position. so somethign like 1,2,3,1000 will say get to point 1,2,3 on time 1000.
    Now how do i link this to 3dsmax.

    i was strated out something like:

          totalFrameCount = animationRange.end 
        -- we assume 30 fps... 
        totalAnimationTime = totalFrameCount / 30 
        aniTime = totalAnimationTime * 1000 
        step = aniTime / numKnots $ 

    Step would result in the time number that is exported allong with the splines.
    But i'm wondering if this is correct, or if there is a better way to do this.

    now the max frame number is divided by the vertex count....
    When i got this finishes i can zip my spline thingy for more people :)

    ps:Shawn thx for you fast and nice feedback!
Page 1 of 1 (7 items) Previous Next