Browse Source

Added Galaxy Point Cloud example.

Vicente Penades 6 years ago
parent
commit
6f44a9a1df

+ 8 - 1
SharpGLTF.sln

@@ -23,10 +23,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{D7D51F42
 EndProject
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpGLTF.Toolkit", "src\SharpGLTF.Toolkit\SharpGLTF.Toolkit.csproj", "{41690879-1F91-4555-A40A-F67B01868D7E}"
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpGLTF.Toolkit", "src\SharpGLTF.Toolkit\SharpGLTF.Toolkit.csproj", "{41690879-1F91-4555-A40A-F67B01868D7E}"
 EndProject
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example1", "examples\Example1\Example1.csproj", "{68662AA0-8523-4B9E-9230-DE79F2B07EAB}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example1", "examples\Example1\Example1.csproj", "{68662AA0-8523-4B9E-9230-DE79F2B07EAB}"
 EndProject
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{83E7E49D-8A28-45E8-9DBD-1F3AEDEF3E42}"
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{83E7E49D-8A28-45E8-9DBD-1F3AEDEF3E42}"
 EndProject
 EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PointCloudGalaxy", "examples\PointCloudGalaxy\PointCloudGalaxy.csproj", "{53B7933A-DD1B-4E75-90EC-94E46101C6CC}"
+EndProject
 Global
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		Debug|Any CPU = Debug|Any CPU
@@ -53,6 +55,10 @@ Global
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB}.Release|Any CPU.Build.0 = Release|Any CPU
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB}.Release|Any CPU.Build.0 = Release|Any CPU
+		{53B7933A-DD1B-4E75-90EC-94E46101C6CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{53B7933A-DD1B-4E75-90EC-94E46101C6CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{53B7933A-DD1B-4E75-90EC-94E46101C6CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{53B7933A-DD1B-4E75-90EC-94E46101C6CC}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 		HideSolutionNode = FALSE
@@ -63,6 +69,7 @@ Global
 		{4FCBB910-67D4-4628-9B2B-F5F2C8D92257} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
 		{4FCBB910-67D4-4628-9B2B-F5F2C8D92257} = {0CBF510D-D836-40BA-95EC-E93FDBB90632}
 		{41690879-1F91-4555-A40A-F67B01868D7E} = {072B725F-773F-4751-9616-E9778897C1D2}
 		{41690879-1F91-4555-A40A-F67B01868D7E} = {072B725F-773F-4751-9616-E9778897C1D2}
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB} = {83E7E49D-8A28-45E8-9DBD-1F3AEDEF3E42}
 		{68662AA0-8523-4B9E-9230-DE79F2B07EAB} = {83E7E49D-8A28-45E8-9DBD-1F3AEDEF3E42}
+		{53B7933A-DD1B-4E75-90EC-94E46101C6CC} = {83E7E49D-8A28-45E8-9DBD-1F3AEDEF3E42}
 	EndGlobalSection
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {1D7BBAD9-834C-4981-AC96-0AA5226FC43F}
 		SolutionGuid = {1D7BBAD9-834C-4981-AC96-0AA5226FC43F}

+ 72 - 0
examples/PointCloudGalaxy/Galaxy.cs

@@ -0,0 +1,72 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace PointCloudGalaxy
+{
+    // based on: https://itinerantgames.tumblr.com/post/78592276402/a-2d-procedural-galaxy-with-c
+
+    class Galaxy
+    {
+        public int numArms = 5;
+
+        public float armSeparationDistance => 2 * (float)Math.PI / numArms;
+
+        public float armOffsetMax = 0.5f;
+        public float rotationFactor = 5;
+        public float randomJitter = 0.02f;
+
+        public float scaleOverPlane = 0.15f;
+
+        public IEnumerable<System.Numerics.Vector3> CreateStarts(int count)
+        {
+            var randomizer = new Random();
+
+            float RndFloat(double min, double max) { return (float)(randomizer.NextDouble() * (max-min) + min); }
+
+            for (int i = 0; i < count; i++)
+            {
+                var p = CreateRandomStar(RndFloat);
+
+                yield return p;
+            }
+        }
+
+        private System.Numerics.Vector3 CreateRandomStar(Func<double, double, float> rndFloat)
+        {
+            // Choose a distance from the center of the galaxy.
+            float distance = rndFloat(0,1);
+            distance = (float)Math.Pow(distance, 2);
+
+            // Choose an angle between 0 and 2 * PI.
+            float angle = rndFloat(0, 2 * Math.PI);
+            float armOffset = rndFloat(0, armOffsetMax);
+            armOffset = armOffset - armOffsetMax / 2;
+            armOffset = armOffset * (1 / distance);
+
+            float squaredArmOffset = (float)Math.Pow(armOffset, 2);
+            if (armOffset < 0) squaredArmOffset = squaredArmOffset * -1;
+
+            armOffset = squaredArmOffset;
+
+            float rotation = distance * rotationFactor;
+
+            angle = (int)(angle / armSeparationDistance) * armSeparationDistance + armOffset + rotation;
+
+            // Convert polar coordinates to 2D cartesian coordinates.
+            var starX = (float)Math.Cos(angle) * distance;
+            var starY = (float)Math.Sin(angle) * distance;                        
+
+            // calculate height over galaxy plane
+            var starH = rndFloat(-1,1);
+            starH *= 1.0f - distance;
+            starH *= scaleOverPlane;
+
+            starX += rndFloat(-1, 1) * 0.5f * randomJitter;
+            starY += rndFloat(-1, 1) * 0.5f * randomJitter;
+            starH += rndFloat(-1, 1) * 0.5f * randomJitter;
+
+            return new System.Numerics.Vector3(starX, starH, starY);
+        }
+    }
+}

+ 12 - 0
examples/PointCloudGalaxy/PointCloudGalaxy.csproj

@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.2</TargetFramework>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\..\src\SharpGLTF.Toolkit\SharpGLTF.Toolkit.csproj" />
+  </ItemGroup>  
+
+</Project>

+ 57 - 0
examples/PointCloudGalaxy/Program.cs

@@ -0,0 +1,57 @@
+using System;
+using System.Numerics;
+
+using SharpGLTF.Geometry;
+using SharpGLTF.Materials;
+using SharpGLTF.Schema2;
+
+namespace PointCloudGalaxy
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var material = new MaterialBuilder("material1").WithUnlitShader();
+
+            var mesh = new MeshBuilder<SharpGLTF.Geometry.VertexTypes.VertexPosition, SharpGLTF.Geometry.VertexTypes.VertexColor1>("points");
+
+            // create a point cloud primitive
+            var pointCloud = mesh.UsePrimitive(material, 1);
+
+            var galaxy = new Galaxy();
+
+            galaxy.scaleOverPlane = 0.05f;
+            galaxy.randomJitter = 0.01f;
+            foreach(var startPoint in galaxy.CreateStarts(50000))
+            {
+                pointCloud.AddPoint((startPoint, Vector4.One));
+            }
+
+            galaxy.scaleOverPlane = 0.15f;
+            galaxy.randomJitter = 0.02f;
+            foreach (var startPoint in galaxy.CreateStarts(50000))
+            {
+                pointCloud.AddPoint((startPoint, new Vector4(0.4f, 0.8f, 0.7f, 1)));
+            }
+            
+            galaxy.randomJitter = 0.07f;
+            foreach (var startPoint in galaxy.CreateStarts(10000))
+            {
+                pointCloud.AddPoint((startPoint, new Vector4(0.2f, 0.6f, 0.5f, 1)));
+            }
+
+            // create a new gltf model
+            var model = ModelRoot.CreateModel();
+
+            // add all meshes (just one in this case) to the model
+            model.CreateMeshes(mesh);
+
+            // create a scene, a node, and assign the first mesh (the terrain)
+            model.UseScene("Default")
+                .CreateNode().WithMesh(model.LogicalMeshes[0]);
+
+            // save the model as GLB
+            model.SaveGLB("Galaxy.glb");
+        }
+    }
+}