示例#1
0
        override public void UpdateGeometry()
        {
            if (sphere == null)
            {
                return;
            }

            sphereMesh.GetComponent <MeshFilter>().sharedMesh = UnityUtil.GetPrimitiveMesh(PrimitiveType.Sphere);
            sphereMesh.transform.localScale = new Vector3(2 * radius, 2 * radius, 2 * radius);

            // if we want to scale away/towards bottom of sphere, then we need to
            //  translate along axis as well...
            Frame3f f      = GetLocalFrame(CoordSpace.ObjectCoords);
            float   fScale = sphere.transform.localScale[1];

            f.Origin -= f.FromFrameV(fScale * centerShift);
            update_shift();
            f.Origin += f.FromFrameV(fScale * centerShift);
            SetLocalFrame(f, CoordSpace.ObjectCoords);

            // apparently this is expensive?
            if (DeferRebuild == false)
            {
                sphereMesh.GetComponent <MeshCollider>().sharedMesh = sphereMesh.GetComponent <MeshFilter>().sharedMesh;
            }

            increment_timestamp();
        }
示例#2
0
        /// <summary>
        /// input ray is in Object (local) coords of so, apply all intermediate
        /// transforms to get it to Scene coords
        /// </summary>
        public static Ray3f ObjectToScene(SceneObject so, Ray3f objectRay)
        {
            Ray3f       sceneRay = objectRay;
            SceneObject curSO    = so;

            while (curSO != null)
            {
                Frame3f  curF  = curSO.GetLocalFrame(CoordSpace.ObjectCoords);
                Vector3f scale = curSO.GetLocalScale();
                Vector3f new_o = curF.FromFrameP(sceneRay.Origin * scale);
                Vector3f new_d = curF.FromFrameV(sceneRay.Direction * scale);
                sceneRay = new Ray3f(new_o, new_d.Normalized);
                SOParent parent = curSO.Parent;
                if (parent is FScene)
                {
                    return(sceneRay);
                }
                curSO = (parent as SceneObject);
            }
            if (curSO == null)
            {
                DebugUtil.Error("SceneTransforms.ObjectToScene: found null parent SO!");
            }
            return(sceneRay);
        }
示例#3
0
        /// <summary>
        /// Set the position of the object frame for this SO without moving the mesh in the scene.
        /// The input frame is the new object frame. So, this is a frame "in scene coordinates" unless
        /// this object has a parent SceneObject (ie is not a child of Scene directly). In that case
        /// the frame needs to be specified relative to that SO. An easy way to do this is to via
        ///   obj_pivot = GetLocalFrame(Object).FromFrame( SceneTransforms.SceneToObject(pivot_in_scene) )
        /// TODO: specify frame coordpace as input argument?
        /// </summary>
        public void RepositionPivot(Frame3f objFrame)
        {
            //if (Parent is FScene == false)
            //    throw new NotSupportedException("DMeshSO.RepositionMeshFrame: have not tested this!");

            Frame3f curFrame = this.GetLocalFrame(CoordSpace.ObjectCoords);
            bool    bNormals = mesh.HasVertexNormals;

            // map vertices to new frame
            foreach (int vid in mesh.VertexIndices())
            {
                Vector3f v = (Vector3f)mesh.GetVertex(vid);
                v = curFrame.FromFrameP(ref v);
                v = objFrame.ToFrameP(ref v);
                mesh.SetVertex(vid, v);

                if (bNormals)
                {
                    Vector3f n = mesh.GetVertexNormal(vid);
                    n = curFrame.FromFrameV(ref n);
                    n = objFrame.ToFrameV(ref n);
                    mesh.SetVertexNormal(vid, n);
                }
            }

            // set new object frame
            SetLocalFrame(objFrame, CoordSpace.ObjectCoords);

            fast_mesh_update(bNormals, false);
            post_mesh_modified();
        }
示例#4
0
 public void GetCurrentPositions_SingleVectorBarycentric(Vector3d[] NewPositions)
 {
     gParallel.ForEach <int>(DisplaceMesh.VertexIndices(), (vid) => {
         Frame3f triFrame = BaseMesh.GetTriFrame(BaryFaceDisplacements[vid].tID);
         Vector3f offsetV = triFrame.FromFrameV(BaryFaceDisplacements[vid].dv);
         Vector3d triPt   = BaseMesh.GetTriBaryPoint(BaryFaceDisplacements[vid].tID,
                                                     BaryFaceDisplacements[vid].a, BaryFaceDisplacements[vid].b, BaryFaceDisplacements[vid].c);
         NewPositions[vid] = triPt + offsetV;
         //NewPositions[vid] = triPt;
     });
 }
示例#5
0
        public static void AppendMeshSO(DMeshSO appendTo, DMeshSO append)
        {
            FScene scene = appendTo.GetScene();

            if (scene.IsSelected(appendTo))
            {
                scene.Deselect(appendTo);
            }
            if (scene.IsSelected(append))
            {
                scene.Deselect(append);
            }

            Frame3f  f1     = appendTo.GetLocalFrame(CoordSpace.ObjectCoords);
            Vector3f scale1 = appendTo.GetLocalScale();
            Frame3f  f2     = append.GetLocalFrame(CoordSpace.ObjectCoords);
            Vector3f scale2 = append.GetLocalScale();

            DMesh3 mesh1 = appendTo.Mesh;

            DMesh3 mesh2 = append.Mesh;

            foreach (int vid in mesh2.VertexIndices())
            {
                // convert point in mesh2 to scene coords
                Vector3f v2 = (Vector3f)mesh2.GetVertex(vid);
                v2 *= scale2;
                Vector3f v2s = f2.FromFrameP(v2);

                // transfer that scene coord into local coords of mesh1
                Vector3f v2in1 = f1.ToFrameP(v2s);
                v2in1 /= scale1;
                mesh2.SetVertex(vid, v2in1);

                if (mesh1.HasVertexNormals && mesh2.HasVertexNormals)
                {
                    Vector3f n   = mesh2.GetVertexNormal(vid);
                    Vector3f ns  = f2.FromFrameV(n);
                    Vector3f ns2 = f1.ToFrameV(ns);
                    mesh2.SetVertexNormal(vid, ns2);
                }
            }

            MeshEditor editor = new MeshEditor(mesh1);

            editor.AppendMesh(mesh2);

            appendTo.NotifyMeshEdited();

            // [TODO] change record!

            scene.RemoveSceneObject(append, false);
        }
示例#6
0
        override public void UpdateGeometry()
        {
            if (cylinder == null)
            {
                return;
            }

            topCap.GetComponent <MeshFilter>().sharedMesh = MeshGenerators.CreateDisc(radius, 1, 16);
            topCap.transform.localPosition = 0.5f * height * Vector3.up;

            bottomCap.GetComponent <MeshFilter>().sharedMesh = MeshGenerators.CreateDisc(radius, 1, 16);
            bottomCap.transform.localPosition = -0.5f * height * Vector3.up;

            body.SetMesh(MeshGenerators.CreateCylider(radius, height, 16));
            body.transform.localPosition = -0.5f * height * Vector3.up;

            // if we want to scale away/towards bottom of cylinder, then we need to
            //  translate along axis as well...
            Frame3f f      = GetLocalFrame(CoordSpace.ObjectCoords);
            float   fScale = cylinder.transform.localScale[1];

            f.Origin -= f.FromFrameV(fScale * centerShift);
            update_shift();
            f.Origin += f.FromFrameV(fScale * centerShift);
            SetLocalFrame(f, CoordSpace.ObjectCoords);

            // apparently this is expensive?
            if (DeferRebuild == false)
            {
                topCap.GetComponent <MeshCollider>().sharedMesh    = topCap.GetComponent <MeshFilter>().sharedMesh;
                bottomCap.GetComponent <MeshCollider>().sharedMesh = bottomCap.GetComponent <MeshFilter>().sharedMesh;
                body.GetComponent <MeshCollider>().sharedMesh      = body.GetComponent <MeshFilter>().sharedMesh;
            }

            increment_timestamp();
        }
示例#7
0
        public static void FromFrame(IDeformableMesh mesh, Frame3f f)
        {
            int  NV          = mesh.MaxVertexID;
            bool bHasNormals = mesh.HasVertexNormals;

            for (int vid = 0; vid < NV; ++vid)
            {
                if (mesh.IsVertex(vid))
                {
                    Vector3D vf = mesh.GetVertex(vid);
                    Vector3D v  = f.FromFrameP((Vector3F)vf);
                    mesh.SetVertex(vid, v);
                    if (bHasNormals)
                    {
                        Vector3F n  = mesh.GetVertexNormal(vid);
                        Vector3F nf = f.FromFrameV(n);
                        mesh.SetVertexNormal(vid, nf);
                    }
                }
            }
        }
示例#8
0
        float fHeightStartT;         // start T-value along heightAxisW

        public override bool BeginCapture(ITransformable target, Ray3f worldRay, UIRayHit hit)
        {
            fStartValue = primitive.Parameters.GetValue <float>(AxisParamName);

            // save necessary frame info
            targetFrameW = target.GetLocalFrame(CoordSpace.WorldCoords);
            heightAxisW  = targetFrameW.FromFrameV(AxisVectorInFrame);

            // save t-value of closest point on height axis, so we can find delta-t
            Vector3f vWorldHitPos = hit.hitPos;

            fHeightStartT = Distance.ClosestPointOnLineT(
                targetFrameW.Origin, heightAxisW, vWorldHitPos);

            // construct plane we will ray-intersect with in UpdateCapture()
            Vector3f makeUp       = Vector3f.Cross(scene.ActiveCamera.Forward(), heightAxisW).Normalized;
            Vector3f vPlaneNormal = Vector3f.Cross(makeUp, heightAxisW).Normalized;

            raycastFrame = new Frame3f(vWorldHitPos, vPlaneNormal);

            return(true);
        }
示例#9
0
        /// <summary>
        /// Input objectN is a normal vector in local coords of SO, apply all intermediate inverse
        /// transforms to get it into scene coords. **NO SCALING**
        /// </summary>
        public static Vector3f ObjectToSceneN(SceneObject so, Vector3f objectN)
        {
            SceneObject curSO = so;

            while (curSO != null)
            {
                Frame3f  curF  = curSO.GetLocalFrame(CoordSpace.ObjectCoords);
                Vector3f scale = curSO.GetLocalScale();
                objectN = curF.FromFrameV(objectN / scale);
                objectN.Normalize();
                SOParent parent = curSO.Parent;
                if (parent is FScene)
                {
                    return(objectN);
                }
                curSO = (parent as SceneObject);
            }
            if (curSO == null)
            {
                DebugUtil.Error("SceneTransforms.ObjectToSceneN: found null parent SO!");
            }
            return(objectN);
        }
示例#10
0
        /// <summary>
        /// Input objectV is a vector in local coords of SO, apply all intermediate inverse
        /// transforms to get it into scene coords.
        /// </summary>
        public static Vector3f ObjectToSceneV(SceneObject so, Vector3f objectV)
        {
            Vector3f    sceneV = objectV;
            SceneObject curSO  = so;

            while (curSO != null)
            {
                Frame3f  curF  = curSO.GetLocalFrame(CoordSpace.ObjectCoords);
                Vector3f scale = curSO.GetLocalScale();
                sceneV *= scale;
                sceneV  = curF.FromFrameV(ref sceneV);
                SOParent parent = curSO.Parent;
                if (parent is FScene)
                {
                    return(sceneV);
                }
                curSO = (parent as SceneObject);
            }
            if (curSO == null)
            {
                DebugUtil.Error("SceneTransforms.ObjectToSceneV: found null parent SO!");
            }
            return(sceneV);
        }
示例#11
0
        public override bool UpdateCapture(ITransformable target, Ray3f worldRay)
        {
            // find hit
            SnapResult snap = Targets.FindHitSnapPoint(worldRay);

            snapState.UpdateState(snap);

            if (snapState.IsSnapped)
            {
                SnapResult useSnap   = snapState.ActiveSnapTarget;
                Frame3f    hitFrameS = Frame3f.Identity;
                if (useSnap != null)
                {
                    hitFrameS = new Frame3f(useSnap.FrameS);
                }

                Frame3f targetF = originalTargetS; // target.GetLocalFrame(CoordSpace.WorldCoords);
                Frame3f pivotF  = hitFrameS;
                targetF.Origin = pivotF.Origin;

                if (parent.CurrentFrameMode == FrameType.WorldFrame)
                {
                    targetF.Rotation = Quaternion.identity;
                }
                else
                {
                    targetF.Rotation = pivotF.Rotation;
                }

                Vector3f deltaInT = targetF.FromFrameV(SourceFrameL.Origin);
                targetF.Origin -= deltaInT;   // why is this minus?

                target.SetLocalFrame(targetF, CoordSpace.SceneCoords);
            }
            else
            {
                Func <SceneObject, bool> filter = null;
                if (TargetObjects != null && TargetObjects.Count > 0)
                {
                    filter = (x) => { return(TargetObjects.Contains(x) == false); }
                }
                ;

                AnyRayHit hit;
                if (scene.FindSceneRayIntersection(worldRay, out hit, true, filter))
                {
                    Vector3f hitPosS  = scene.ToSceneP(hit.hitPos);
                    Vector3f hitNormS = scene.ToSceneN(hit.hitNormal);

                    Frame3f targetF = originalTargetS;
                    targetF.Origin = hitPosS;
                    targetF.AlignAxis(1, hitNormS);

                    if (parent.CurrentFrameMode == FrameType.WorldFrame)
                    {
                        targetF.Rotation = Quaternion.identity;
                    }

                    Vector3f deltaInT = targetF.FromFrameV(SourceFrameL.Origin);
                    targetF.Origin -= deltaInT;   // why is this minus?
                    target.SetLocalFrame(targetF, CoordSpace.SceneCoords);
                }
                else
                {
                    target.SetLocalFrame(originalTargetS, CoordSpace.SceneCoords);
                }
            }

            return(true);
        }
示例#12
0
        /// <summary>
        /// Generates the actual mesh for the polyhedron
        /// </summary>
        private void MakeMesh()
        {
            MeshFilter mf;

            mf = Shape.GetComponent <MeshFilter>();
            if (mf == null)
            {
                mf = Shape.AddComponent <MeshFilter>();
            }
            mf.mesh = null;
            Mesh mesh = new Mesh();
            TriangulatedPolygonGenerator tpg = new TriangulatedPolygonGenerator();
            Frame3f frame = new Frame3f();

            tpg.Polygon = Polygon.ToPolygon(ref frame);
            tpg.Generate();
            int nv = tpg.vertices.Count;

            VertexTable.Clear();

            foreach (Dataline ring in Polygon)
            {
                foreach (VertexLookup v in ring.VertexTable)
                {
                    VertexTable.Add(v);
                }
            }
            IEnumerable <Vector3d> vlist = tpg.vertices.AsVector3d();

            Vector3[] vertices = new Vector3[vlist.Count()];

            for (int i = 0; i < vlist.Count(); i++)
            {
                Vector3d v = vlist.ElementAt(i);
                try {
                    VertexLookup vl = VertexTable.Find(item => v.xy.Distance(frame.ToPlaneUV(item.Com.transform.position, 3)) < 0.001);
                    vertices[i] = Shape.transform.InverseTransformPoint(vl.Com.transform.position);
                    vl.pVertex  = i;
                } catch {
                    VertexTable.Add(new VertexLookup()
                    {
                        pVertex = i, Com = VertexTable[0].Com
                    });
                    vertices[i] = Shape.transform.InverseTransformPoint((Vector3)frame.FromFrameV(v));
                }
            }

            List <Vector2>         uvs  = new List <Vector2>();
            IEnumerable <Vector2d> uv2d = tpg.uv.AsVector2f();

            foreach (Vector2d uv in uv2d)
            {
                uvs.Add((Vector2)uv);
            }
            mesh.vertices  = vertices.ToArray();
            mesh.triangles = tpg.triangles.ToArray <int>();
            mesh.uv        = uvs.ToArray <Vector2>();

            mesh.RecalculateBounds();
            mesh.RecalculateNormals();

            mf.mesh = mesh;
        }