예제 #1
0
        private int GetElementCount(int time, bool transparent)
        {
            List <DebugRenderTriangleIndexMarker> triangleMarkers = transparent ? transparentTriangleMarkers : opaqueTriangleMarkers;

            int indexElementCount = 0;
            DebugRenderTriangleIndexMarker searchItem = new DebugRenderTriangleIndexMarker(0, time);
            int index = triangleMarkers.BinarySearch(searchItem);

            if (index < 0)
            {
                index = ~index;
            }

            index = triangleMarkers.Count > 0 ? SMath.Clamp(index, 0, triangleMarkers.Count - 1) : index;

            if (index < triangleMarkers.Count)
            {
                while (index > 0 && triangleMarkers[index].time > time)
                {
                    // Not exact match but the next one -> Decrease index by one to have only the range that counts up for the requested time
                    index--;
                }

                if (index < triangleMarkers.Count && triangleMarkers[index].time <= time)
                {
                    indexElementCount = triangleMarkers[index].index + 1;
                }
            }
            return(indexElementCount);
        }
예제 #2
0
        public BBox ShrinkAbsoluteBorder(Vector2d amount)
        {
            var dx = SMath.Clamp(amount.X, 0, 0.5 * Dx);
            var dy = SMath.Clamp(amount.Y, 0.0, 0.5 * Dy);

            return(new BBox(X0 + dx, Y0 + dy, X1 + dx, Y1 + dy));
        }
예제 #3
0
        // OPT: This could prolly be more efficient.
        // But a core i5 handles 50k particles without much sweat, so, no sweat.
        public void Update(double dt, ParticleGroup group)
        {
            var fdt       = (float)dt;
            var particles = group.Particles;

            // We iterate downwards through the list so if we remove a particle we don't
            // reorder anything we haven't already updated.
            for (int i = particles.Count - 1; i > -1; i--)
            {
                var p = particles[i];

                if (scaleWithTime)
                {
                    p.Scale += (deltaScale * fdt);
                    p.Scale  = (float)SMath.Clamp(p.Scale, 0.1, 5);
                }
                if (changeColorWithTime)
                {
                    doColorFade(ref p, group.colorFader, fdt);
                    //group.colorFader.setColor(ref p);
                }

                doMovement(ref p, dt);
                particles[i] = p;
                if (p.Life < 0)
                {
                    group.Remove(i);
                }
            }
        }
예제 #4
0
        private void CreateMesh()
        {
            double height = 0.5;
            double radius = 0.5;

            Segments = SMath.Clamp(Segments, 3, 256);

            VertexList = new VertexNormalColor[Segments * (CreateCaps ? 2 : 1) + 1];
            IndexList  = new uint[Segments * 3 + (CreateCaps ? (Segments - 2) * 3 : 0)];

            // Create vertices
            VertexList[0].Position = new Vector3(0, 0, height);
            for (int i = 0; i < Segments; i++)
            {
                double  angle            = ((double)i / (double)Segments) * Math.PI * 2.0;
                Vector2 planarCoordinate = Vector2.FromAngle(angle, radius);

                VertexList[i + 1].Position = new Vector3(planarCoordinate.X, planarCoordinate.Y, -height);

                if (CreateCaps)
                {
                    // Top (for cap)
                    VertexList[i + Segments + 1].Position = new Vector3(planarCoordinate.X, planarCoordinate.Y, -height);
                }
            }

            // Create normals
            Vector3 capNormal = CreateCaps ? GeometryMath.GetTriangleNormal(VertexList[1].Position, VertexList[2].Position, VertexList[3].Position) : new Vector3(0, 0, 0);

            for (int i = 0; i < Segments; i++)
            {
                Vector3 manifoldNormal = GeometryMath.GetTriangleNormal(VertexList[0].Position, VertexList[(i + 1) % Segments + 1].Position, VertexList[i + 1].Position);
                VertexList[i + 1].Normal = manifoldNormal;
                if (CreateCaps)
                {
                    VertexList[Segments + i + 1].Normal = capNormal;
                }
            }

            // Create triangles
            int index = 0;

            for (int i = 0; i < Segments; i++)
            {
                // Manifold
                IndexList[index++] = 0;
                IndexList[index++] = (uint)((i + 1) % Segments + 1);
                IndexList[index++] = (uint)(i + 1);

                if (CreateCaps && i < Segments - 2)
                {
                    // Cap
                    IndexList[index++] = (uint)(Segments + 1);
                    IndexList[index++] = (uint)(Segments + i + 2);
                    IndexList[index++] = (uint)(Segments + i + 3);
                }
            }
        }
예제 #5
0
		protected void UpdateUVs (Vector3[] vertices) {
			Vector2[] uvs = new Vector2[vertices.Length];

			for (int i=0; i<uvs.Length; i++) {
				float u = vertices[i].x / GetMaxLightLength() / 2 + 0.5f;
				float v = vertices[i].y / GetMaxLightLength() / 2 + 0.5f;
				
				u = SMath.Clamp(0, u, 1);
				v = SMath.Clamp(0, v, 1);

				uvs[i] = new Vector2(u, v);
			}

			_mesh.uv = uvs;
		}
예제 #6
0
        public void OnViewportMouseMove(object sender, MouseEventArgs args)
        {
            Point   mousePosition         = new Point(args.X, args.Y);
            Vector2 winformsMousePosition = new Vector2(args.X, args.Y);

            if (PreviousMousePosition == null)
            {
                PreviousMousePosition = new Vector2(winformsMousePosition);
            }
            Vector2 mouseDelta = winformsMousePosition - PreviousMousePosition;

            PreviousMousePosition = winformsMousePosition;

            if (args.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (IsPerspective)
                {
                    // Take yaw/pitch from mouse movement
                    double yawDelta   = SMath.Clamp(mouseDelta.X * CameraAimSpeed, -15.0, 15.0);
                    double pitchDelta = SMath.Clamp(mouseDelta.Y * CameraAimSpeed, -15.0, 15.0);

                    Matrix4x4 yawRotMat = new Matrix4x4(new Quaternion(PerspectiveCamera.UpDirection, SMath.DEG2RAD * yawDelta));
                    PerspectiveCamera.Direction = (yawRotMat * PerspectiveCamera.Direction).GetNormalized();

                    Matrix4x4 pitchRotMat = new Matrix4x4(new Quaternion(PerspectiveCamera.SideDirection, SMath.DEG2RAD * pitchDelta));
                    PerspectiveCamera.Direction = (pitchRotMat * PerspectiveCamera.Direction).GetNormalized();
                }
                else
                {
                    OrthoCamera.Angle += SMath.DEG2RAD * SMath.Clamp(-mouseDelta.X * CameraAimSpeed, -15.0, 15.0);
                }

                if (winformsMousePosition.X > Width || winformsMousePosition.Y > Height || winformsMousePosition.X < 0 || winformsMousePosition.Y < 0)
                {
                    PreviousMousePosition = new Vector2(Location.X + Width / 2, Location.Y + Height / 2);
                    Cursor.Position       = this.PointToScreen(PreviousMousePosition.ToPoint());
                }
            }
        }
예제 #7
0
 public Vector2d ClampVector(Vector2d vec)
 {
     return(new Vector2d(SMath.Clamp(vec.X, Left, Right), SMath.Clamp(vec.Y, Bottom, Top)));
 }
예제 #8
0
        private void CreateMesh()
        {
            double height       = 0.5;
            double TopRadius    = 0.5;
            double BottomRadius = TopRadius * BottomRadiusScale;

            Segments = SMath.Clamp(Segments, 3, 256);

            VertexList = new VertexNormalColor[Segments * (CreateCaps ? 4 : 2)];
            IndexList  = new uint[Segments * 6 + (CreateCaps ? (Segments - 1) * 6 : 0)];

            // Create vertices
            for (int i = 0; i < Segments; i++)
            {
                double  angle            = ((double)i / (double)Segments) * Math.PI * 2.0;
                Vector2 planarCoordinate = Vector2.FromAngle(angle, 1.0);
                // Top
                VertexList[i + Segments * 0].Position = new Vector3(planarCoordinate.X * TopRadius, planarCoordinate.Y * TopRadius, height);
                // Bottom
                VertexList[i + Segments * 1].Position = new Vector3(planarCoordinate.X * BottomRadius, planarCoordinate.Y * BottomRadius, -height);

                if (CreateCaps)
                {
                    // Top (for cap)
                    VertexList[i + Segments * 2].Position = new Vector3(planarCoordinate.X * TopRadius, planarCoordinate.Y * TopRadius, height);
                    // Bottom (for cap)
                    VertexList[i + Segments * 3].Position = new Vector3(planarCoordinate.X * BottomRadius, planarCoordinate.Y * BottomRadius, -height);
                }
            }

            // Create normals
            Vector3 topNormal    = CreateCaps ? GeometryMath.GetTriangleNormal(VertexList[Segments * 2].Position, VertexList[Segments * 2 + 2].Position, VertexList[Segments * 2 + 1].Position) : new Vector3(0, 0, 0);
            Vector3 bottomNormal = CreateCaps ? GeometryMath.GetTriangleNormal(VertexList[Segments * 3].Position, VertexList[Segments * 3 + 1].Position, VertexList[Segments * 3 + 2].Position) : new Vector3(0, 0, 0);

            for (int i = 0; i < Segments; i++)
            {
                Vector3 manifoldNormal = GeometryMath.GetTriangleNormal(VertexList[i].Position, VertexList[(i + 1) % Segments].Position, VertexList[i + Segments].Position);
                VertexList[Segments * 0 + i].Normal = manifoldNormal;
                VertexList[Segments * 1 + i].Normal = manifoldNormal;
                if (CreateCaps)
                {
                    VertexList[Segments * 2 + i].Normal = topNormal;
                    VertexList[Segments * 3 + i].Normal = bottomNormal;
                }
            }

            // Create triangles
            int index = 0;

            for (int i = 0; i < Segments; i++)
            {
                // Manifold
                IndexList[index++] = (uint)i;
                IndexList[index++] = (uint)((i + 1) % Segments);
                IndexList[index++] = (uint)(i + Segments);

                IndexList[index++] = (uint)((i + 1) % Segments);
                IndexList[index++] = (uint)((i + 1) % Segments + Segments);
                IndexList[index++] = (uint)(i + Segments);

                if (CreateCaps && i < Segments - 1)
                {
                    // Top cap
                    IndexList[index++] = (uint)(Segments * 2);
                    IndexList[index++] = (uint)(Segments * 2 + i + 1);
                    IndexList[index++] = (uint)(Segments * 2 + i);

                    // Bottom cap
                    IndexList[index++] = (uint)(Segments * 3);
                    IndexList[index++] = (uint)(Segments * 3 + i);
                    IndexList[index++] = (uint)(Segments * 3 + i + 1);
                }
            }
        }