private float[] CalculateSurfaceAreas(out float totalSurfaceArea)
        {
            int idx = 0;

            totalSurfaceArea = 0.0f;
            float[] surfaceAreas = new float[triangles.Length / 3];
            for (int triangleIndex = 0; triangleIndex < triangles.Length; triangleIndex += 3)
            {
                Vector3 p1 = vertices[triangles[triangleIndex]];
                Vector3 p2 = vertices[triangles[triangleIndex + 1]];
                Vector3 p3 = vertices[triangles[triangleIndex + 2]];

                // http://www.wikihow.com/Sample/Area-of-a-Triangle-Side-Length
                float a = (p1 - p2).sqrMagnitude;
                float b = (p1 - p3).sqrMagnitude;
                float c = (p2 - p3).sqrMagnitude;

                // faster with only 1 square root: http://www.iquilezles.org/blog/?p=1579
                // A² = (2ab + 2bc + 2ca – a² – b² – c²)/16
                float areaSquared = ((2.0f * a * b) + (2.0f * b * c) + (2.0f * c * a) - (a * a) - (b * b) - (c * c)) / 16.0f;
                float area        = PathGenerator.SquareRoot(areaSquared);
                surfaceAreas[idx++] = area;
                totalSurfaceArea   += area;
            }

            return(surfaceAreas);
        }
Exemple #2
0
        public Vector3 RandomDirection3D(System.Random r)
        {
            float   z      = (2.0f * (float)r.NextDouble()) - 1.0f; // z is in the range [-1,1]
            Vector2 planar = RandomDirection2D(r) * PathGenerator.SquareRoot(1.0f - (z * z));

            return(new Vector3(planar.x, planar.y, z));
        }
Exemple #3
0
        public void GenerateLightningBoltPath(LightningBolt bolt, Vector3 start, Vector3 end, LightningBoltParameters p)
        {
            if (p.Points.Count < 2)
            {
                Debug.LogError("Lightning path should have at least two points");
                return;
            }

            int     generation = p.Generations;
            int     totalGenerations = generation;
            float   offsetAmount, d;
            int     smoothingFactor = p.SmoothingFactor - 1;
            Vector3 distance, randomVector;
            LightningBoltSegmentGroup group = bolt.AddGroup();

            group.LineWidth          = p.TrunkWidth;
            group.Generation         = generation--;
            group.EndWidthMultiplier = p.EndWidthMultiplier;
            group.Color = Color.white;

            p.Start = p.Points[0] + start;
            p.End   = p.Points[p.Points.Count - 1] + end;
            end     = p.Start;

            for (int i = 1; i < p.Points.Count; i++)
            {
                start    = end;
                end      = p.Points[i];
                distance = (end - start);
                d        = PathGenerator.SquareRoot(distance.sqrMagnitude);
                if (p.ChaosFactor > 0.0f)
                {
                    if (bolt.CameraMode == CameraMode.Perspective)
                    {
                        end += (d * p.ChaosFactor * RandomDirection3D(p.Random));
                    }
                    else if (bolt.CameraMode == CameraMode.OrthographicXY)
                    {
                        end += (d * p.ChaosFactor * RandomDirection2D(p.Random));
                    }
                    else
                    {
                        end += (d * p.ChaosFactor * RandomDirection2DXZ(p.Random));
                    }
                    distance = (end - start);
                }
                group.Segments.Add(new LightningBoltSegment {
                    Start = start, End = end
                });

                offsetAmount = d * p.ChaosFactor;
                RandomVector(bolt, ref start, ref end, offsetAmount, p.Random, out randomVector);

                if (ShouldCreateFork(p, generation, totalGenerations))
                {
                    Vector3 branchVector = distance * p.ForkMultiplier() * smoothingFactor * 0.5f;
                    Vector3 forkEnd      = end + branchVector + randomVector;
                    GenerateLightningBoltStandard(bolt, start, forkEnd, generation, totalGenerations, 0.0f, p);
                }

                if (--smoothingFactor == 0)
                {
                    smoothingFactor = p.SmoothingFactor - 1;
                }
            }
        }