From 34deeba87c3959b5e4a43d0ad0b78f6cc67b82a9 Mon Sep 17 00:00:00 2001 From: xLinka Date: Thu, 27 Jun 2024 00:07:24 +0100 Subject: [PATCH] fix hmac and normals for planet mesh generation --- ProjectObsidian/Components/Mesh/Planet.cs | 36 +++++++++---------- .../ProtoFlux/Math/FibonacciNode.cs | 4 +-- ProjectObsidian/ProtoFlux/Strings/HMAC.cs | 13 +++---- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/ProjectObsidian/Components/Mesh/Planet.cs b/ProjectObsidian/Components/Mesh/Planet.cs index b9e0562..77ecb45 100644 --- a/ProjectObsidian/Components/Mesh/Planet.cs +++ b/ProjectObsidian/Components/Mesh/Planet.cs @@ -39,11 +39,12 @@ private void GeneratePlanet(MeshX mesh, int subdivisions, float radius, float no // Apply noise to vertices ApplyPerlinNoise(vertices, radius, noiseScale, noiseStrength); - // Assign vertices and triangles to the mesh + // Assign vertices, normals, and triangles to the mesh mesh.SetVertexCount(vertices.Count); for (int i = 0; i < vertices.Count; i++) { mesh.SetVertex(i, vertices[i]); + mesh.SetNormal(i, vertices[i].Normalized); // Set normals directly } for (int i = 0; i < triangles.Count; i += 3) @@ -51,27 +52,26 @@ private void GeneratePlanet(MeshX mesh, int subdivisions, float radius, float no mesh.AddTriangle(triangles[i], triangles[i + 1], triangles[i + 2]); } - // mesh.RecalculateNormals(AllTriangles); } private void CreateIcosahedron(List vertices, List triangles) { float t = (1.0f + MathX.Sqrt(5.0f)) / 2.0f; - vertices.Add(new float3(-1, t, 0)); - vertices.Add(new float3(1, t, 0)); - vertices.Add(new float3(-1, -t, 0)); - vertices.Add(new float3(1, -t, 0)); + vertices.Add(new float3(-1, t, 0).Normalized); + vertices.Add(new float3(1, t, 0).Normalized); + vertices.Add(new float3(-1, -t, 0).Normalized); + vertices.Add(new float3(1, -t, 0).Normalized); - vertices.Add(new float3(0, -1, t)); - vertices.Add(new float3(0, 1, t)); - vertices.Add(new float3(0, -1, -t)); - vertices.Add(new float3(0, 1, -t)); + vertices.Add(new float3(0, -1, t).Normalized); + vertices.Add(new float3(0, 1, t).Normalized); + vertices.Add(new float3(0, -1, -t).Normalized); + vertices.Add(new float3(0, 1, -t).Normalized); - vertices.Add(new float3(t, 0, -1)); - vertices.Add(new float3(t, 0, 1)); - vertices.Add(new float3(-t, 0, -1)); - vertices.Add(new float3(-t, 0, 1)); + vertices.Add(new float3(t, 0, -1).Normalized); + vertices.Add(new float3(t, 0, 1).Normalized); + vertices.Add(new float3(-t, 0, -1).Normalized); + vertices.Add(new float3(-t, 0, 1).Normalized); triangles.AddRange(new int[] { 0, 11, 5, @@ -136,14 +136,10 @@ private int GetMidpoint(Dictionary midpointCache, List vertic float3 p1 = vertices[v1]; float3 p2 = vertices[v2]; - float3 middle = new float3( - (p1.x + p2.x) / 2.0f, - (p1.y + p2.y) / 2.0f, - (p1.z + p2.z) / 2.0f - ); + float3 middle = (p1 + p2) / 2.0f; midpoint = vertices.Count; - vertices.Add(middle); + vertices.Add(middle.Normalized); midpointCache[key] = midpoint; return midpoint; diff --git a/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs b/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs index 457c97c..f52d32d 100644 --- a/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs +++ b/ProjectObsidian/ProtoFlux/Math/FibonacciNode.cs @@ -22,8 +22,8 @@ private int Fibonacci(int n) { if (n < 0) { - UniLog.Log("Negative numbers are not allowed."); - return -1; // Special error value + //UniLog.Log("Negative numbers are not allowed."); + return -1; } if (n == 0) return 0; diff --git a/ProjectObsidian/ProtoFlux/Strings/HMAC.cs b/ProjectObsidian/ProtoFlux/Strings/HMAC.cs index 398ac08..55f1200 100644 --- a/ProjectObsidian/ProtoFlux/Strings/HMAC.cs +++ b/ProjectObsidian/ProtoFlux/Strings/HMAC.cs @@ -1,6 +1,7 @@ using System; using System.Security.Cryptography; using System.Text; +using Elements.Core; using FrooxEngine; using FrooxEngine.ProtoFlux; using ProtoFlux.Core; @@ -20,18 +21,18 @@ public enum HashFunction [NodeCategory("Obsidian/String")] public class HMACNode : ObjectFunctionNode { - public readonly ObjectInput Message; - public readonly ObjectInput Key; - public readonly ObjectInput HashAlgorithm; + public ObjectInput Message; + public ObjectInput Key; + public ValueInput HashAlgorithm; protected override string Compute(FrooxEngineContext context) { - string message = Message.Evaluate(context); - string key = Key.Evaluate(context); + string message = Message.Evaluate(context) ?? string.Empty; + string key = Key.Evaluate(context) ?? string.Empty; HashFunction hashFunction = HashAlgorithm.Evaluate(context); if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(key)) - return ""; + return string.Empty; byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] messageBytes = Encoding.UTF8.GetBytes(message);