コード例 #1
0
ファイル: GraphLines.cs プロジェクト: huangdonghai/titan3d
        public void SetBezier3D(Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4, int count)
        {
            EngineNS.Bezier3D bezier3d = new EngineNS.Bezier3D(v1, v2, v3, v4);

            Points.Clear();
            AABB.InitEmptyBox();
            float times = 1.0f / (float)count;

            for (int i = 0; i < count; i++)
            {
                var pt = bezier3d.GetValue(Math.Min(times * i, 1.0f));
                unsafe
                {
                    Points.Add(pt);
                    AABB.Merge(ref pt);
                }
            }
        }
コード例 #2
0
ファイル: TrailData.cs プロジェクト: huangdonghai/titan3d
        public void CalculateVertexindexes(int StartIndex)
        {
            //处理索引
            if (Positions.Count < 4)
            {
                return;
            }

            VertexIndexes.Clear((uint)((Positions.Count - 2) * 3));
            for (int i = 2; i < Positions.Count; i += 2)
            {
                VertexIndexes.Add(StartIndex + i - 2);
                VertexIndexes.Add(StartIndex + i - 1);
                VertexIndexes.Add(StartIndex + i);

                VertexIndexes.Add(StartIndex + i + 1);
                VertexIndexes.Add(StartIndex + i);
                VertexIndexes.Add(StartIndex + i - 1);
            }
        }
コード例 #3
0
ファイル: SceneDataSM3.cs プロジェクト: huangdonghai/titan3d
        private unsafe void UpdateIndexBufferCPU(CCommandList cmd, Graphics.CGfxCamera Camera)
        {
            if (mCpuDrawIndexBuffer == null)
            {
                return;
            }
            CBMeshBatch cbMesh = new CBMeshBatch();
            {
                var pPlanes = stackalloc Plane[6];
                Camera.CullingFrustum.GetPlanes(pPlanes);
                CBMeshBatch *pCBuffer  = &cbMesh;
                Plane *      planesTar = (Plane *)pCBuffer;
                for (uint i = 0; i < 6; i++)
                {
                    planesTar[i] = pPlanes[i];
                }
                cbMesh.GpuDrivenCameraPosition = Camera.CullingFrustum.TipPos;
                BoundingBox box = new BoundingBox();
                Camera.CullingFrustum.GetBoundBox(ref box);
                cbMesh.GpuDrivenFrustumMinPoint = box.Minimum;
                cbMesh.GpuDrivenFrustumMaxPoint = box.Maximum;
                cbMesh.MeshBatchVertexStride    = (uint)AllVertices.Count;
                cbMesh.ClusterNumber            = (uint)GpuClusters.Count;

                UpdateCBMeshbatch(cmd, Camera);
            }

            UInt32_3 tri = new UInt32_3();

            mDrawIndices.Clear();
            for (int i = 0; i < GpuClusters.Count; i++)
            {
                var cluster = GpuClusters[i];
                var GpuDrivenCameraDirection = GpuClusters[i].BoundCenter - cbMesh.GpuDrivenCameraPosition;
                var cameraDirInBoundSpace    = Vector3.TransposeTransformNormal(GpuDrivenCameraDirection, GpuInstanceDatas[(int)cluster.InstanceId].InvMatrix);
                if (FrustumCull(ref cluster, ref cbMesh) == false)
                {
                    UInt64 visBits = 0;
                    if (cameraDirInBoundSpace.X >= 0)
                    {
                        visBits |= cluster.CubeFaces[(int)Cluster.GpuCluster.ECubeFace.CubeFace_NX];
                    }
                    else
                    {
                        visBits |= cluster.CubeFaces[(int)Cluster.GpuCluster.ECubeFace.CubeFace_X];
                    }
                    if (cameraDirInBoundSpace.Y >= 0)
                    {
                        visBits |= cluster.CubeFaces[(int)Cluster.GpuCluster.ECubeFace.CubeFace_NY];
                    }
                    else
                    {
                        visBits |= cluster.CubeFaces[(int)Cluster.GpuCluster.ECubeFace.CubeFace_Y];
                    }
                    if (cameraDirInBoundSpace.Z >= 0)
                    {
                        visBits |= cluster.CubeFaces[(int)Cluster.GpuCluster.ECubeFace.CubeFace_NZ];
                    }
                    else
                    {
                        visBits |= cluster.CubeFaces[(int)Cluster.GpuCluster.ECubeFace.CubeFace_Z];
                    }

                    uint InstanceStartIndex = cbMesh.MeshBatchVertexStride * cluster.InstanceId;
                    for (int j = 0; j < cluster.FaceCount; j++)
                    {
                        if (TestBit(visBits, j) == false)
                        {
                            continue;
                        }
                        int srcIndex = (int)(cluster.StartFaceIndex + j) * 3;
                        tri.x = InstanceStartIndex + AllIndices[srcIndex];
                        tri.y = InstanceStartIndex + AllIndices[srcIndex + 1];
                        tri.z = InstanceStartIndex + AllIndices[srcIndex + 2];
                        mDrawIndices.Add(tri);
                    }
                }
            }

            mDrawArgs.InstanceCount         = 1;
            mDrawArgs.IndexCountPerInstance = (uint)mDrawIndices.Count * 3;
            uint size = (uint)(mDrawIndices.Count * sizeof(UInt32_3));

            if (mCpuDrawIndexBuffer.Desc.ByteWidth > size)
            {
                mCpuDrawIndexBuffer.UpdateBuffData(cmd, mDrawIndices.GetBufferPtr(), size);
            }
        }
コード例 #4
0
ファイル: GraphLines.cs プロジェクト: huangdonghai/titan3d
        public void BuildGraph(Support.NativeList <LinePosition> position, Support.NativeList <LineUV> uv, float halfWidth)
        {
            BeforeGraph(position);

            LineUV rectUV = new LineUV();

            rectUV.SetUV();

            Vector3 cur, tar;
            bool    drawing      = true;
            float   remainLength = GetSegment(position);
            bool    isContinued  = false;

            do
            {
                var normal = GetNormalDir();
                NextStartPoint(out cur);
                isContinued = NextEndPoint(out tar); //NextStartPoint(out tar);
                float dist = Vector3.Distance(ref cur, ref tar);
                var   dir  = tar - cur;
                dir.Normalize();
                Vector3 extDir = Vector3.Cross(dir, normal);
                extDir.Normalize();
                while (dist >= remainLength)
                {
                    if (drawing)
                    {
                        var end = cur + dir * remainLength;
                        var l   = new LinePosition();
                        l.SetLine(ref cur, ref end, ref extDir, halfWidth);
                        //position.Add(l);
                        //uv.Add(rectUV);
                        unsafe
                        {
                            position.Add(l);
                            uv.Add(rectUV);
                        }


                        drawing      = false;
                        dist        -= remainLength;
                        remainLength = GetInterval(position);

                        cur = end;
                    }
                    else
                    {
                        drawing      = true;
                        dist        -= remainLength;
                        cur          = cur + dir * remainLength;
                        remainLength = GetSegment(position);

                        extDir = Vector3.Cross(dir, normal);
                        extDir.Normalize();
                    }
                }

                if (drawing)
                {
                    var end = cur + dir * dist;
                    var l   = new LinePosition();
                    l.SetLine(ref cur, ref end, ref extDir, halfWidth);
                    remainLength = remainLength - dist;
                    //position.Add(l);
                    //uv.Add(rectUV);
                    unsafe
                    {
                        position.Add(l);
                        uv.Add(rectUV);
                    }
                    dist = 0;
                }
                else
                {
                    remainLength = remainLength - dist;
                    dist         = 0;
                }
            } while (isContinued);
        }
コード例 #5
0
ファイル: GraphLines.cs プロジェクト: huangdonghai/titan3d
        public void UpdateGeomMesh(CRenderContext rc, float halfWidth = 0.1F)
        {
            if (GeomMesh == null)
            {
                return;
            }

            CDrawPrimitiveDesc dpDesc = new CDrawPrimitiveDesc();

            dpDesc.SetDefault();

            if (UseGeometry)
            {
                mTempPosition.Clear();
                mTempUV.Clear();
                LinesGen.BuildGraph(mTempPosition, mTempUV, halfWidth);

                mTempNormal.Clear();
                mTempNormal.SetGrowStep(mTempPosition.Count);
                for (int i = 0; i < mTempPosition.Count; i++)
                {
                    //mTempNormal.Add(mTempPosition[i].CalcNormals());
                    unsafe
                    {
                        LinePosition tempNorm = new LinePosition();
                        var          ptr      = (LinePosition *)mTempPosition.UnsafeAddressAt(i).ToPointer();
                        ptr->CalcNormals(ref tempNorm);
                        mTempNormal.Add(tempNorm);
                    }
                }

                dpDesc.StartIndex = 0xFFFFFFFF;
                if (halfWidth == 0)
                {
                    dpDesc.PrimitiveType = EPrimitiveType.EPT_LineList;
                    dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 3 * 3);
                }
                else
                {
                    dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 3 * 2);
                }
                GeomMesh.SetAtom(0, 0, ref dpDesc);

                unsafe
                {
                    GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Position, mTempPosition.GetBufferPtr(),
                                                  (UInt32)(sizeof(LinePosition) * mTempPosition.Count), (UInt32)sizeof(Vector3), 0);
                    GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Normal, mTempNormal.GetBufferPtr(),
                                                  (UInt32)(sizeof(LinePosition) * mTempNormal.Count), (UInt32)sizeof(Vector3), 0);
                    GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_UV, mTempUV.GetBufferPtr(),
                                                  (UInt32)(sizeof(LineUV) * mTempUV.Count), (UInt32)sizeof(Vector2), 0);
                }
            }
            else
            {
                mTempPosition.Clear();
                mTempUV.Clear();
                LinesGen.BuildGraph(mTempPosition, mTempUV, halfWidth);

                mTempNormal.Clear();
                mTempNormal.SetGrowStep(mTempPosition.Count);
                for (int i = 0; i < mTempPosition.Count; i++)
                {
                    //mTempNormal.Add(mTempPosition[i].CalcNormals());
                    unsafe
                    {
                        LinePosition tempNorm = new LinePosition();
                        var          ptr      = (LinePosition *)mTempPosition.UnsafeAddressAt(i).ToPointer();
                        ptr->CalcNormals(ref tempNorm);
                        mTempNormal.Add(tempNorm);
                    }
                }

                dpDesc.StartIndex = 0xFFFFFFFF;
                if (halfWidth == 0)
                {
                    dpDesc.PrimitiveType = EPrimitiveType.EPT_LineList;
                    dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 3);
                }
                else
                {
                    dpDesc.NumPrimitives = (UInt32)(mTempPosition.Count * 2);
                }
                GeomMesh.SetAtom(0, 0, ref dpDesc);

                unsafe
                {
                    GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Position, mTempPosition.GetBufferPtr(),
                                                  (UInt32)(sizeof(LinePosition) * mTempPosition.Count), (UInt32)sizeof(Vector3), 0);
                    GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_Normal, mTempNormal.GetBufferPtr(),
                                                  (UInt32)(sizeof(LinePosition) * mTempNormal.Count), (UInt32)sizeof(Vector3), 0);
                    GeomMesh.SetGeomtryMeshStream(rc, EVertexSteamType.VST_UV, mTempUV.GetBufferPtr(),
                                                  (UInt32)(sizeof(LineUV) * mTempUV.Count), (UInt32)sizeof(Vector2), 0);
                }
            }

            GeomMesh.AABB = LinesGen.AABB;
            GeomMesh.GeometryMesh.Dirty = true;
        }
コード例 #6
0
ファイル: TrailData.cs プロジェクト: huangdonghai/titan3d
        public void CreateTrailData(ref Vector3 ppos, ref Quaternion prot, ref Vector3 pscale, ref Matrix worldmatrix, bool isbind)
        {
            if (first)
            {
                PrePosition = ppos;
                first       = false;
                return;
            }
            var distance = Vector3.Distance(ref ppos, ref PrePosition);

            if (distance < MinVertextDistance)
            {
                return;
            }

            PrePosition = ppos;
            Vector3 p2 = Direction * (-Width) * 0.5f;
            Vector3 p3 = Direction * Width * 0.5f;

            if (isbind)
            {
                Vector3    scale;
                Quaternion rotation;
                Vector3    translation;

                worldmatrix.Decompose(out scale, out rotation, out translation);
                p2 = pscale * p2;
                p3 = pscale * p3;

                if (prot.IsValid)
                {
                    p2 = prot * p2;
                    p3 = prot * p3;
                }

                p2 = scale * p2;
                p3 = scale * p3;

                if (rotation.IsValid)
                {
                    p2 = rotation * p2;
                    p3 = rotation * p3;
                }

                p2 = ppos + p2;
                p3 = ppos + p3;
            }
            else
            {
                Vector4 temp = new Vector4();
                p2 = pscale * p2;
                p3 = pscale * p3;

                if (prot.IsValid)
                {
                    p2 = prot * p2;
                    p3 = prot * p3;
                }


                Vector3.Transform(ref p2, ref worldmatrix, out temp);
                p2.X = temp.X + ppos.X;
                p2.Y = temp.Y + ppos.Y;
                p2.Z = temp.Z + ppos.Z;

                Vector3.Transform(ref p3, ref worldmatrix, out temp);
                p3.X = temp.X + ppos.X;
                p3.Y = temp.Y + ppos.Y;
                p3.Z = temp.Z + ppos.Z;
            }

            Positions.Add(p2);
            Positions.Add(p3);

            Colors.Add(DefaultColor);
            Colors.Add(DefaultColor);

            UVs.Add(new Vector2(0, 0));
            UVs.Add(new Vector2(0, 0));

            Tangents.Add(Vector4.UnitX);
            Tangents.Add(Vector4.UnitX);
            Normals.Add(new Vector3(0, 0, -1));
            Normals.Add(new Vector3(0, 0, -1));

            LifeTicks.Add(0);
            LifeTicks.Add(0);

            //CalculateVertexindexes();
        }
コード例 #7
0
        public void AddDefaultColorBuff(CRenderContext rc, CGfxMeshPrimitives result)
        {
            if (result == null)
            {
                return;
            }

            var mesh = result.GeometryMesh;

            if (mesh == null)
            {
                return;
            }

            CVertexBuffer colorbuff = mesh.GetVertexBuffer(EVertexSteamType.VST_Color);

            if (colorbuff != null)
            {
                return;
            }

            CVertexBuffer posbuff = mesh.GetVertexBuffer(EVertexSteamType.VST_Position);

            if (posbuff == null)
            {
                return;
            }

            var blob = new Support.CBlobObject();

            posbuff.GetBufferData(rc, blob);
            int vertNum = 0;

            unsafe
            {
                vertNum = (int)blob.Size / sizeof(Vector3);
            }

            if (vertNum == 0)
            {
                return;
            }

            Support.NativeList <Byte4> Colors = new Support.NativeList <Byte4>();
            var color = new Byte4();

            color.X = 255;
            color.Y = 255;
            color.Z = 255;
            color.W = 255;
            for (int i = 0; i < vertNum; i++)
            {
                Colors.Add(color);
            }

            var dpDesc = new CDrawPrimitiveDesc();

            dpDesc.SetDefault();
            dpDesc.NumPrimitives = (UInt32)vertNum;
            result.SetAtom(0, 0, ref dpDesc);

            UInt32 resourceSize = 0;

            unsafe
            {
                var vbDesc = new CVertexBufferDesc();
                vbDesc.CPUAccess = (UInt32)ECpuAccess.CAS_WRITE;

                {
                    vbDesc.InitData  = Colors.UnsafeAddressAt(0);
                    vbDesc.Stride    = (UInt32)sizeof(Byte4);
                    vbDesc.ByteWidth = (UInt32)(sizeof(Byte4) * Colors.Count);
                    var vb = rc.CreateVertexBuffer(vbDesc);
                    mesh.BindVertexBuffer(EVertexSteamType.VST_Color, vb);
                    resourceSize += vbDesc.ByteWidth;
                }

                result.ResourceState.ResourceSize = (UInt32)(resourceSize);
                mesh.Dirty = true;
            }
            result.ResourceState.StreamState = EStreamingState.SS_Valid;
            result.ResourceState.KeepValid   = true;
        }