예제 #1
0
        public void  Evaluate(int SpreadMax)
        {
            if (this.FBodies.IsConnected)
            {
                this.FOutNodes.SliceCount  = SpreadMax;
                this.FOutCustom.SliceCount = SpreadMax;
                this.FOutId.SliceCount     = SpreadMax;
                this.FOutMass.SliceCount   = SpreadMax;

                for (int i = 0; i < SpreadMax; i++)
                {
                    SoftBody sb = this.FBodies[i];
                    this.FOutNodes[i].SliceCount = sb.Nodes.Count;
                    //sb.Nodes[0].

                    for (int j = 0; j < sb.Nodes.Count; j++)
                    {
                        this.FOutNodes[i][j] = sb.Nodes[j].X.ToVVVVector();
                    }

                    this.FOutMass[i] = sb.TotalMass;

                    SoftBodyCustomData custom = (SoftBodyCustomData)sb.UserObject;
                    this.FOutCustom[i] = custom.Custom;
                    this.FOutId[i]     = custom.Id;
                }
            }
            else
            {
                this.FOutNodes.SliceCount  = 0;
                this.FOutCustom.SliceCount = 0;
                this.FOutId.SliceCount     = 0;
                this.FOutMass.SliceCount   = 0;
            }
        }
예제 #2
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FWorld.PluginIO.IsConnected && this.FShapes.PluginIO.IsConnected)
            {
                List <SoftBody> bodies = new List <SoftBody>();
                for (int i = 0; i < SpreadMax; i++)
                {
                    if (FDoCreate[i])
                    {
                        AbstractSoftShapeDefinition shapedef = this.FShapes[i];

                        SoftBody body = shapedef.GetSoftBody(this.FWorld[0].WorldInfo);

                        body.Translate(this.FPosition[i].ToBulletVector());
                        body.Scale(this.FScale[i].ToBulletVector());
                        body.Rotate(this.FRotate[i].ToBulletQuaternion());

                        body.Friction    = this.FFriction[i];
                        body.Restitution = this.FRestitution[i];

                        SoftBodyCustomData bd = new SoftBodyCustomData();
                        bd.Id           = this.FWorld[0].GetNewBodyId();
                        bd.Custom       = this.FCustom[i];
                        bd.HasUV        = shapedef.HasUV;
                        bd.UV           = shapedef.GetUV(body);
                        body.UserObject = bd;


                        if (this.FCustomObj.PluginIO.IsConnected)
                        {
                            bd.CustomObject = (ICloneable)this.FCustomObj[i].Clone();
                        }
                        else
                        {
                            bd.CustomObject = null;
                        }



                        this.FWorld[0].Register(body);
                        bodies.Add(body);
                    }
                }

                this.FOutBodies.SliceCount = bodies.Count;
                for (int i = 0; i < bodies.Count; i++)
                {
                    this.FOutBodies[i] = bodies[i];
                }
            }
            else
            {
                this.FOutBodies.SliceCount = 0;
            }
        }
        public void Evaluate(int SpreadMax)
        {
            if (this.FWorld.PluginIO.IsConnected && this.FShapes.PluginIO.IsConnected)
            {
                List <SoftBody> bodies = new List <SoftBody>();
                for (int i = 0; i < SpreadMax; i++)
                {
                    if (FDoCreate[i])
                    {
                        AbstractSoftShapeDefinition shapedef = this.FShapes[i];

                        SoftBody      body = shapedef.GetSoftBody(this.FWorld[0].WorldInfo);
                        RigidBodyPose pose = this.initialPoseInput.IsConnected ? this.initialPoseInput[i] : RigidBodyPose.Default;

                        body.Translate(pose.Position);
                        body.Scale(this.FScale[i].ToBulletVector());
                        body.Rotate(pose.Orientation);
                        body.Friction    = this.FFriction[i];
                        body.Restitution = this.FRestitution[i];

                        SoftBodyCustomData bd = new SoftBodyCustomData(this.FWorld[0].GetNewSoftBodyId());
                        bd.Custom       = this.FCustom[i];
                        bd.HasUV        = shapedef.HasUV;
                        bd.UV           = shapedef.GetUV(body);
                        bd.Definition   = shapedef;
                        body.UserObject = bd;


                        this.FWorld[0].Register(body);
                        bodies.Add(body);

                        //Attach if to all nodes
                        for (int j = 0; j < body.Nodes.Count; j++)
                        {
                            body.Nodes[j].Tag = (IntPtr)j;
                        }
                    }
                }

                this.FOutBodies.SliceCount = bodies.Count;
                for (int i = 0; i < bodies.Count; i++)
                {
                    this.FOutBodies[i] = bodies[i];
                }
            }
            else
            {
                this.FOutBodies.SliceCount = 0;
            }
        }
예제 #4
0
        public void  Evaluate(int SpreadMax)
        {
            if (this.FBodies.IsConnected)
            {
                this.FOutId.SliceCount = SpreadMax;

                for (int i = 0; i < SpreadMax; i++)
                {
                    SoftBody           sb     = this.FBodies[i];
                    SoftBodyCustomData custom = (SoftBodyCustomData)sb.UserObject;
                    this.FOutId[i] = custom.Id;
                }
            }
            else
            {
                this.FOutId.SliceCount = 0;
            }
        }
        public void  Evaluate(int SpreadMax)
        {
            this.FOutNodes.SliceCount  = this.FBodies.SliceCount;
            this.FOutCustom.SliceCount = SpreadMax;
            this.FOutId.SliceCount     = SpreadMax;
            this.FOutMass.SliceCount   = SpreadMax;

            for (int i = 0; i < SpreadMax; i++)
            {
                SoftBody sb = this.FBodies[i];
                this.FOutNodes[i].SliceCount = sb.Nodes.Count;
                //sb.Nodes[0].

                for (int j = 0; j < sb.Nodes.Count; j++)
                {
                    this.FOutNodes[i][j] = sb.Nodes[j].X.ToVVVVector();
                }

                this.FOutMass[i] = sb.TotalMass;

                SoftBodyCustomData custom = (SoftBodyCustomData)sb.UserObject;
                this.FOutCustom[i] = custom.Custom;
                this.FOutId[i]     = custom.Id;

                if (custom.CustomObject != null)
                {
                    this.FHasCustomObj[i] = true;
                    this.FCustomObj[i]    = custom.CustomObject;
                }
                else
                {
                    this.FHasCustomObj[i] = false;
                    this.FCustomObj[i]    = null;
                }
            }
        }
        public void Evaluate(int SpreadMax)
        {
            if (this.FWorld.PluginIO.IsConnected)
            {
                this.FHit.SliceCount = SpreadMax;

                List <double>   fraction = new List <double>();
                List <Vector3D> position = new List <Vector3D>();
                List <Vector3D> normal   = new List <Vector3D>();
                List <SoftBody> body     = new List <SoftBody>();
                List <int>      bodyid   = new List <int>();
                List <int>      qidx     = new List <int>();
                List <int>      nidx     = new List <int>();
                List <EFeature> feat     = new List <EFeature>();
                List <Node>     nodes    = new List <Node>();


                for (int i = 0; i < SpreadMax; i++)
                {
                    Vector3 from = this.FFrom[i].ToBulletVector();
                    Vector3 to   = this.FTo[i].ToBulletVector();

                    List <SoftBody> sbs = this.FWorld[0].SoftBodies;
                    //List<SRayCast> results = new List<SRayCast>();

                    SRayCast closest = null;

                    for (int ib = 0; ib < sbs.Count; ++ib)
                    {
                        SoftBody psb = sbs[ib];
                        SRayCast res = new SRayCast();
                        if (psb.RayTest(from, to, res))
                        {
                            if (closest == null)
                            {
                                closest = res;
                            }
                            else
                            {
                                if (res.Fraction < closest.Fraction)
                                {
                                    closest = res;
                                }
                            }
                        }
                    }

                    if (closest != null)
                    {
                        this.FHit[i] = true;
                        fraction.Add(closest.Fraction);

                        Vector3 pos = from + (to - from) * closest.Fraction;
                        position.Add(pos.ToVVVVector());
                        body.Add(closest.Body);
                        qidx.Add(i);
                        SoftBodyCustomData sd = (SoftBodyCustomData)closest.Body.UserObject;
                        bodyid.Add(sd.Id);
                        feat.Add(closest.Feature);

                        int nodeidx = closest.Index;

                        Node fnode = null;

                        if (closest.Feature == EFeature.Face)
                        {
                            var  f    = closest.Body.Faces[closest.Index];
                            Node node = f.N[0];
                            for (int ni = 1; ni < 3; ++ni)
                            {
                                if ((node.X - pos).LengthSquared() >
                                    (f.N[ni].X - pos).LengthSquared())
                                {
                                    fnode = f.N[ni];
                                }
                            }
                        }

                        if (closest.Feature == EFeature.Node)
                        {
                            fnode = closest.Body.Nodes[nodeidx];
                        }

                        nidx.Add(nodeidx);
                        nodes.Add(fnode);
                    }
                    else
                    {
                        this.FHit[i] = false;
                    }
                }


                this.FId.AssignFrom(bodyid);
                this.FHitFraction.AssignFrom(fraction);
                this.FHitPosition.AssignFrom(position);
                this.FQueryIndex.AssignFrom(qidx);
                this.FBody.AssignFrom(body);
                this.FHitFeature.AssignFrom(feat);
                this.FHitIndex.AssignFrom(nidx);
                this.FNode.AssignFrom(nodes);
            }
            else
            {
                this.FHit.SliceCount         = 0;
                this.FId.SliceCount          = 0;
                this.FHitFraction.SliceCount = 0;
                this.FHitPosition.SliceCount = 0;
                this.FHitFeature.SliceCount  = 0;
                this.FHitIndex.SliceCount    = 0;
                this.FBody.SliceCount        = 0;
                this.FNode.SliceCount        = 0;
            }
        }
예제 #7
0
        public void UpdateResource(IPluginOut ForPin, Device OnDevice)
        {
            List <Mesh> soft = new List <Mesh>();

            if (this.FMesh != null)
            {
                this.FMesh.Dispose();
                this.FMesh = null;
            }
            if (this.FBodies.SliceCount > 0)
            {
                int cnt = this.FBodies.SliceCount;

                for (int i = 0; i < cnt; i++)
                {
                    SoftBody body = this.FBodies[i];

                    SoftBodyCustomData sc = (SoftBodyCustomData)body.UserObject;

                    AlignedFaceArray faces = body.Faces;

                    if (FValid[i])
                    {
                        if (body.Faces.Count > 0)
                        {
                            #region Build from Faces
                            VertexFormat decl;
                            if (sc.HasUV)
                            {
                                decl = VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture1;
                            }
                            else
                            {
                                decl = VertexFormat.Position | VertexFormat.Normal;
                            }

                            Mesh mesh = new Mesh(OnDevice, faces.Count, faces.Count * 3, MeshFlags.SystemMemory | MeshFlags.Use32Bit, decl);

                            SlimDX.DataStream verts   = mesh.LockVertexBuffer(LockFlags.None);
                            SlimDX.DataStream indices = mesh.LockIndexBuffer(LockFlags.None);

                            int j;
                            int uvcnt = 0;
                            for (j = 0; j < faces.Count; j++)
                            {
                                NodePtrArray nodes = faces[j].N;
                                verts.Write(nodes[0].X);
                                verts.Write(nodes[0].Normal);
                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                verts.Write(nodes[1].X);
                                verts.Write(nodes[1].Normal);

                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                verts.Write(nodes[2].X);
                                verts.Write(nodes[2].Normal);
                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                indices.Write(j * 3);
                                indices.Write(j * 3 + 1);
                                indices.Write(j * 3 + 2);
                            }

                            mesh.UnlockVertexBuffer();
                            mesh.UnlockIndexBuffer();
                            //mesh.ComputeNormals();

                            soft.Add(mesh);
                            #endregion
                        }
                        else
                        {
                            #region Build from tetras
                            int tetraCount  = body.Tetras.Count;
                            int vertexCount = tetraCount * 12;

                            VertexFormat decl;
                            if (sc.HasUV)
                            {
                                decl = VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture1;
                            }
                            else
                            {
                                decl = VertexFormat.Position | VertexFormat.Normal;
                            }

                            Mesh mesh = new Mesh(OnDevice, tetraCount * 4, vertexCount, MeshFlags.SystemMemory | MeshFlags.Use32Bit, decl);


                            SlimDX.DataStream indices = mesh.LockIndexBuffer(LockFlags.Discard);

                            for (int idx = 0; idx < vertexCount; idx++)
                            {
                                indices.Write(idx);
                            }
                            mesh.UnlockIndexBuffer();

                            SlimDX.DataStream verts = mesh.LockVertexBuffer(LockFlags.None);
                            foreach (Tetra t in body.Tetras)
                            {
                                NodePtrArray nodes = t.Nodes;

                                verts.Write(nodes[2].X);
                                verts.Write(nodes[2].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[1].X);
                                verts.Write(nodes[1].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[0].X);
                                verts.Write(nodes[0].Normal);
                                //verts.Position += 12;

                                verts.Write(nodes[0].X);
                                verts.Write(nodes[0].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[1].X);
                                verts.Write(nodes[1].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[3].X);
                                verts.Write(nodes[3].Normal);
                                //verts.Position += 12;

                                verts.Write(nodes[2].X);
                                verts.Write(nodes[2].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[3].X);
                                verts.Write(nodes[3].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[1].X);
                                verts.Write(nodes[1].Normal);
                                //verts.Position += 12;

                                verts.Write(nodes[2].X);
                                verts.Write(nodes[2].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[0].X);
                                verts.Write(nodes[0].Normal);
                                //verts.Position += 12;
                                verts.Write(nodes[3].X);
                                verts.Write(nodes[3].Normal);
                                //verts.Position += 12;
                            }

                            mesh.UnlockVertexBuffer();
                            mesh.UnlockIndexBuffer();
                            //mesh.ComputeNormals();

                            soft.Add(mesh);
                            #endregion
                        }
                    }
                }

                Mesh merge = null;
                if (OnDevice is DeviceEx)
                {
                    merge = Mesh.Concatenate(OnDevice, soft.ToArray(), MeshFlags.Use32Bit);
                }
                else
                {
                    merge = Mesh.Concatenate(OnDevice, soft.ToArray(), MeshFlags.Use32Bit | MeshFlags.Managed);
                }


                this.FMesh = merge;

                foreach (Mesh m in soft)
                {
                    m.Dispose();
                }
            }
        }
        public void Update(IPluginIO pin, DX11RenderContext context)
        {
            for (int i = 0; i < this.FOutput.SliceCount; i++)
            {
                if (this.FOutput[i].Contains(context))
                {
                    this.FOutput[i].Dispose(context);
                }
            }

            if (this.FBodies.SliceCount > 0)
            {
                int cnt = this.FBodies.SliceCount;

                for (int i = 0; i < cnt; i++)
                {
                    SoftBody body = this.FBodies[i];

                    SoftBodyCustomData sc = (SoftBodyCustomData)body.UserObject;

                    AlignedFaceArray faces = body.Faces;

                    if (FValid[i])
                    {
                        if (body.Faces.Count > 0)
                        {
                            #region Build from Faces
                            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

                            geom.VerticesCount = faces.Count * 3;


                            if (sc.HasUV)
                            {
                                geom.InputLayout = Pos3Norm3Tex2Vertex.Layout;
                                geom.VertexSize  = Pos3Norm3Tex2Vertex.VertexSize;
                            }
                            else
                            {
                                geom.InputLayout = Pos3Norm3Vertex.Layout;
                                geom.VertexSize  = Pos3Norm3Vertex.VertexSize;
                            }

                            //Mesh mesh = new Mesh(OnDevice, faces.Count, faces.Count * 3, MeshFlags.SystemMemory | MeshFlags.Use32Bit, decl);

                            SlimDX.DataStream verts   = new SlimDX.DataStream(geom.VerticesCount * geom.VertexSize * 3, false, true);
                            SlimDX.DataStream indices = new SlimDX.DataStream(faces.Count * sizeof(int) * 3, false, true);

                            int j;
                            int uvcnt = 0;
                            for (j = 0; j < faces.Count; j++)
                            {
                                NodePtrArray nodes = faces[j].N;
                                verts.Write(nodes[0].X);
                                verts.Write(nodes[0].Normal);
                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                verts.Write(nodes[1].X);
                                verts.Write(nodes[1].Normal);

                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                verts.Write(nodes[2].X);
                                verts.Write(nodes[2].Normal);
                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                indices.Write(j * 3);
                                indices.Write(j * 3 + 1);
                                indices.Write(j * 3 + 2);
                            }

                            geom.VertexBuffer = BufferHelper.CreateVertexBuffer(context, verts, false, true);

                            geom.HasBoundingBox = false;

                            DX11IndexBuffer ibo = new DX11IndexBuffer(context, indices, false, true);
                            geom.IndexBuffer         = ibo;
                            this.FOutput[i][context] = geom;
                            #endregion
                        }
                    }
                }
            }
        }