예제 #1
0
        public void Cultures_That_Are_Not_Invariant_Write_Invariant_Culture_Imml()
        {
            if (Thread.CurrentThread.CurrentCulture.IsReadOnly)
            {
                Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
            }

            Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ",";
            Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator = ".";

            try
            {
                var immlDocument = new ImmlDocument();
                immlDocument.Gravity = new Vector3(1.2345f, 2.3456f, 3.4567f);
                immlDocument.SoundSpeed = 10.34566f;

                var immlSerialiser = new ImmlSerialiser();
                var immlString = immlSerialiser.Write(immlDocument);

                var textReader = new StringReader(immlString);
                var xDoc = XDocument.Load(textReader);

                Assert.Equal("1.2345,2.3456,3.4567", xDoc.Root.Attribute(XName.Get("Gravity", string.Empty)).Value);
                Assert.Equal("10.34566", xDoc.Root.Attribute(XName.Get("SoundSpeed", string.Empty)).Value);
            }
            finally
            {
                //restore it
                Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator;
                Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator = CultureInfo.InvariantCulture.NumberFormat.NumberGroupSeparator;
            }
        }
 public void Initialise(IImmlElement context, DeviceManager deviceManager)
 {
     _Context = context as ImmlDocument;
 }
 public ImmlDocumentViewModel(ImmlDocument document)
 {
     this.Document = document;
 }
예제 #4
0
        private async void _NewFile()
        {
            var immlDocument = new ImmlDocument();
            App.ViewModel.SelectedDocument = new ImmlDocumentViewModel(immlDocument);

            _ContextRenderer.Clear();

            var node = new ImmlDocumentSceneNode();
            node.Initialise(immlDocument, _DeviceManager);
            
            _ContextRenderer.Add(node);
        }
예제 #5
0
        public void Serialiser_Does_Not_Write_Attributes_That_Are_Set_To_Their_Default_Values()
        {
            var immlDocument = new ImmlDocument();
            var immlSerialiser = new ImmlSerialiser();
            var immlString = immlSerialiser.Write(immlDocument);

            //test for some optional values
            Assert.DoesNotContain("Gravity=\"0,-9.8,0\"", immlString);
            Assert.DoesNotContain("GlobalIllumination=\"#4c4c4c\"", immlString);
            Assert.DoesNotContain("AnimationSpeed=\"1\"", immlString);
            Assert.DoesNotContain("PhysicsSpeed=\"1\"", immlString);
            Assert.DoesNotContain("SoundSpeed=\"1\"", immlString);
        }
예제 #6
0
        public void Serialiser_Does_Not_Write_Optional_Attributes_That_Do_Not_Have_A_Value()
        {
            var immlDocument = new ImmlDocument();
            var immlSerialiser = new ImmlSerialiser();
            var immlString = immlSerialiser.Write(immlDocument);

            //test for some optional values
            Assert.DoesNotContain("Author", immlString);
            Assert.DoesNotContain("Description", immlString);
        }
예제 #7
0
        public void Serialiser_Can_Write_A_Simple_Nested_Hierarchy_To_A_String()
        {
            var immlDocument = new ImmlDocument();
            immlDocument.Author = Guid.NewGuid().ToString();

            var model = new Model();
            model.Name = Guid.NewGuid().ToString();
            model.Source = string.Format("http://{0}", Guid.NewGuid().ToString());

            var primitive = new Primitive();
            primitive.Name = Guid.NewGuid().ToString();

            var camera = new Camera();
            camera.Name = Guid.NewGuid().ToString();

            primitive.Add(camera);
            model.Add(primitive);
            immlDocument.Add(model);

            var immlSerialiser = new ImmlSerialiser();
            var immlString = immlSerialiser.Write(immlDocument);

            var textReader = new StringReader(immlString);
            var xDoc = XDocument.Load(textReader);

            //verify the model is in the correct place
            var xElementModel = xDoc.Descendants(XName.Get("Model", immlSerialiser.Namespace)).Where(e => e.Attribute(XName.Get("Name")).Value == model.Name).FirstOrDefault();
            Assert.NotNull(xElementModel);
            Assert.Equal(immlDocument.Name, xElementModel.Parent.Attribute(XName.Get("Name")).Value);

            //verify the primitive is in the correct place
            var xElementPrimitive = xDoc.Descendants(XName.Get("Primitive", immlSerialiser.Namespace)).Where(e => e.Attribute(XName.Get("Name")).Value == primitive.Name).FirstOrDefault();
            Assert.NotNull(xElementPrimitive);
            Assert.Equal(model.Name, xElementPrimitive.Parent.Attribute(XName.Get("Name")).Value);

            //verify the camera is in the correct place
            var xElementCamera = xDoc.Descendants(XName.Get("Camera", immlSerialiser.Namespace)).Where(e => e.Attribute(XName.Get("Name")).Value == camera.Name).FirstOrDefault();
            Assert.NotNull(xElementCamera);
            Assert.Equal(primitive.Name, xElementCamera.Parent.Attribute(XName.Get("Name")).Value);
        }
예제 #8
0
        public void Serialiser_Can_Write_A_File_With_A_Flat_Hierarchy_Of_Ten_Thousand_Elements_In_Under_Five_Seconds()
        {
            var tmpFile = Path.GetTempFileName();

            try
            {
                //10k elements produces ~2mb of IMML
                var elementsToGenerate = 10000;
                var elementTypeArray = new string[] { "Primitive", "Model", "Camera" };
                var random = new Random();
                var immlContext = new ImmlDocument();

                for (int i = 0; i < elementsToGenerate; i++)
                {
                    var index = random.Next(0, elementTypeArray.Length);
                    var typeToCreate = elementTypeArray[index];

                    var element = ElementFactory.Create(typeToCreate);
                    immlContext.Add(element);
                }

                using (var fs = new FileStream(tmpFile, FileMode.Create))
                {
                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    new ImmlSerialiser().Write(immlContext, fs);
                    stopWatch.Stop();

                    Console.WriteLine(fs.Length);
                    Assert.NotInRange(stopWatch.Elapsed.TotalSeconds, 5, int.MaxValue);
                }
            }
            finally
            {
                System.IO.File.Delete(tmpFile);
            }
        }
예제 #9
0
        public void Serialiser_Can_Write_An_ImmlDocument_With_Attributes_To_A_String()
        {
            var immlDocument = new ImmlDocument();
            immlDocument.Author = Guid.NewGuid().ToString();
            immlDocument.AnimationSpeed = (float)new Random().NextDouble();
            immlDocument.SoundSpeed = (float)new Random().NextDouble();
            immlDocument.PhysicsSpeed = (float)new Random().NextDouble();
            immlDocument.Background = Guid.NewGuid().ToString();
            immlDocument.Behaviours.Add(Guid.NewGuid().ToString());
            immlDocument.Behaviours.Add(Guid.NewGuid().ToString());
            immlDocument.Camera = Guid.NewGuid().ToString();
            immlDocument.Context = Guid.NewGuid().ToString();
            immlDocument.Description = Guid.NewGuid().ToString();
            immlDocument.GlobalIllumination = Color3.Red;
            immlDocument.Gravity = Vector3.UnitZ;
            immlDocument.HostUri = new Uri("http://www.vastpark.com");
            immlDocument.Name = Guid.NewGuid().ToString();
            immlDocument.Tags.Add("tag1");
            immlDocument.Tags.Add("tag2");

            var immlSerialiser = new ImmlSerialiser();
            var immlString = immlSerialiser.Write(immlDocument);

            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Author", immlDocument.Author)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "AnimationSpeed", immlDocument.AnimationSpeed)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "SoundSpeed", immlDocument.SoundSpeed)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "PhysicsSpeed", immlDocument.PhysicsSpeed)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Background", immlDocument.Background)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Camera", immlDocument.Camera)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Description", immlDocument.Description)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "GlobalIllumination", immlDocument.GlobalIllumination)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "HostUri", immlDocument.HostUri)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Name", immlDocument.Name)));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Behaviours", TypeConvert.Parse(immlDocument.Behaviours))));
            Assert.True(immlString.Contains(string.Format("{0}=\"{1}\"", "Tags", TypeConvert.Parse(immlDocument.Tags))));
        }
예제 #10
0
    private void _LoadLights(ImmlDocument document)
    {
        foreach (var immlLight in document.Elements.OfType<Imml.Scene.Controls.Light>())
        {
            var lightGameObject = new GameObject(immlLight.Name);
            lightGameObject.AddComponent<UnityEngine.Light>();
            lightGameObject.light.range = immlLight.Range;
            lightGameObject.light.color = immlLight.Diffuse.ToUnityColor(1);

            if (immlLight.CastShadows)
            {
                lightGameObject.light.shadows = LightShadows.Soft;
            }

            var rotation = immlLight.WorldRotation.Unify().ToUnityVector();

            switch (immlLight.Type)
            {
                case ImmlSpec.LightType.Directional:
                    {
                        lightGameObject.light.type = UnityEngine.LightType.Directional;
                        lightGameObject.transform.position = immlLight.WorldPosition.ToUnityVector();
                        lightGameObject.transform.rotation = UnityEngine.Quaternion.Euler(rotation);
                        break;
                    }
                case ImmlSpec.LightType.Point:
                    {
                        lightGameObject.light.type = UnityEngine.LightType.Point;
                        lightGameObject.transform.position = immlLight.WorldPosition.ToUnityVector();
                        lightGameObject.transform.rotation = UnityEngine.Quaternion.Euler(rotation);
                        break;
                    }
                case ImmlSpec.LightType.Spot:
                    {
                        lightGameObject.light.type = UnityEngine.LightType.Spot;
                        lightGameObject.transform.position = immlLight.WorldPosition.ToUnityVector();
                        lightGameObject.transform.rotation = UnityEngine.Quaternion.Euler(rotation);

                        if (immlLight.OuterCone > 0)
                        {
                            lightGameObject.light.spotAngle = immlLight.OuterCone;
                        }
                        break;
                    }
            }
        }
    }
예제 #11
0
    private void _LoadPrimitives(ImmlDocument document)
    {
        foreach (var immlPrimitive in document.Elements.OfType<Primitive>())
        {
            var primType = _GetType(immlPrimitive.Type);

            var prim = UnityEngine.GameObject.CreatePrimitive(primType);
            prim.transform.position = immlPrimitive.WorldPosition.ToUnityVector();
            prim.transform.rotation = UnityEngine.Quaternion.Euler(immlPrimitive.WorldRotation.Unify().ToUnityVector());

            switch (immlPrimitive.Type)
            {
                case Imml.PrimitiveType.Plane:
                    {
                        //unity has an inconsistent starting scale for plane primitives
                        prim.transform.localScale = immlPrimitive.Size.ToUnityVector() / 10;
                        prim.transform.position = new Vector3(immlPrimitive.WorldPosition.X, immlPrimitive.WorldPosition.Y - 0.5f, immlPrimitive.WorldPosition.Z);
                        break;
                    }
                case ImmlSpec.PrimitiveType.Cylinder:
                case ImmlSpec.PrimitiveType.Cone:
                    {
                        var scaled = new Vector3(immlPrimitive.WorldSize.X, immlPrimitive.WorldSize.Y / 2, immlPrimitive.WorldSize.Z);
                        prim.transform.localScale = scaled;
                        break;
                    }
                default:
                    {
                        prim.transform.localScale = immlPrimitive.Size.ToUnityVector();
                        break;
                    }
            }

            //materials
            var material = immlPrimitive.GetMaterialGroup(-1).GetMaterial();
            var alpha = material.Opacity;

            prim.renderer.material.shader = UnityEngine.Shader.Find("VertexLit");

            prim.renderer.material.SetColor("_Color", material.Diffuse.ToUnityColor(alpha));
            prim.renderer.material.SetColor("_SpecColor", material.Specular.ToUnityColor(alpha));
            prim.renderer.material.SetColor("_Emission", material.Emissive.ToUnityColor(alpha));
            prim.renderer.material.SetColor("_ReflectColor", material.Ambient.ToUnityColor(alpha));
            prim.renderer.castShadows = immlPrimitive.CastShadows;
            prim.renderer.receiveShadows = true;

            //physics
            var immlPhysics = immlPrimitive.GetPhysics();

            if (immlPhysics != null && immlPhysics.Enabled)
            {
                prim.AddComponent<Rigidbody>();

                if (immlPhysics.Movable)
                {
                    prim.rigidbody.mass = immlPhysics.Weight;
                }
                else
                {
                    prim.rigidbody.isKinematic = true;
                }

                prim.rigidbody.centerOfMass = immlPhysics.Centre.ToUnityVector();

                switch(immlPhysics.Bounding)
                {
                    case BoundingType.Box:
                        {
                            prim.AddComponent<BoxCollider>();
                            break;
                        }
                    case BoundingType.ConvexHull:
                        {
                            prim.AddComponent<MeshCollider>();
                            break;
                        }
                    case BoundingType.Cylinder:
                        {
                            prim.AddComponent<CapsuleCollider>();
                            break;
                        }
                    case BoundingType.Sphere:
                        {
                            prim.AddComponent<SphereCollider>();
                            break;
                        }

                }
            }
        }
    }