Exemple #1
0
        //Vector2
        public void GetClosestPointToCell(Vector2 targetPos, out Vector3 closestPoint, out bool isOutsideCell)
        {
            float closestSqrDistance = float.MaxValue;

            closestPoint = Vector3.zero;

            foreach (var edgeData in _contentDictionary.Keys)
            {
                if (SomeMath.PointInTriangle(edgeData.leftV2, edgeData.rightV2, centerVector2, targetPos))
                {
                    closestPoint  = new Vector3(targetPos.x, SomeMath.CalculateHeight(edgeData.leftV3, edgeData.rightV3, centerVector3, targetPos.x, targetPos.y), targetPos.y);
                    isOutsideCell = false;
                    return;
                }
                else
                {
                    Vector3 curInte;
                    SomeMath.ClosestToSegmentTopProjection(edgeData.leftV3, edgeData.rightV3, targetPos, true, out curInte);
                    float curSqrDist = SomeMath.SqrDistance(targetPos.x, targetPos.y, curInte.x, curInte.z);

                    if (curSqrDist < closestSqrDistance)
                    {
                        closestSqrDistance = curSqrDist;
                        closestPoint       = curInte;
                    }
                }
            }
            isOutsideCell = true;
            return;
        }
 //Vector2
 public bool GetPointInsideCell(Vector2 targetPos, out Vector3 result)
 {
     foreach (var edgeData in _contentDictionary.Keys)
     {
         if (SomeMath.PointInTriangle(edgeData.leftV2, edgeData.rightV2, centerV2, targetPos))
         {
             result = new Vector3(targetPos.x, SomeMath.CalculateHeight(edgeData.leftV3, edgeData.rightV3, centerV3, targetPos.x, targetPos.y), targetPos.y);
             return(true);
         }
     }
     result = Vector3.zero;
     return(false);
 }
Exemple #3
0
        /// <summary>
        /// check all cell triangles and return Y position if point inside of any triangle
        /// </summary>
        /// <param name="y">result Y if point inside cell</param>
        /// <returns>true if point inside cell</returns>
        public bool GetPointInsideCell(float x, float z, out float y)
        {
            CellContentData edgeData;

            foreach (var pair in _contentDictionary)
            {
                edgeData = pair.Key;
                if (SomeMath.PointInTriangle(centerVector2.x, centerVector2.y, edgeData.xLeft, edgeData.zLeft, edgeData.xRight, edgeData.zRight, x, z))
                {
                    y = SomeMath.CalculateHeight(edgeData.leftV3, edgeData.rightV3, centerVector3, x, z);
                    return(true);
                }
            }
            y = 0;
            return(false);
        }
        //public void AppendTerrain(CSRasterization2DResult rasterizationResult, TerrainColliderInfoMesh terrainInfo) {
        //    var voxels = rasterizationResult.voxels;
        //    for (int x = 0; x < sizeX; x++) {
        //        for (int z = 0; z < sizeZ; z++) {
        //            var curVoxel = voxels[x + (z * sizeX)];
        //            if (curVoxel.exist)
        //                SetVoxel(x, z, curVoxel.height - 20f, curVoxel.height, (sbyte)curVoxel.passability);
        //        }
        //    }
        //}

        private  DataCompact[] GetTerrainCompactData(Vector3[] vrts, int[] trs) {
            var compactData = TakeCompactData();

            float maxSlopeCos = Mathf.Cos(template.maxSlope * Mathf.PI / 180.0f);
            float voxelSize = template.voxelSize;

            Vector3 realChunkPos = template.realOffsetedPosition;
            float chunkPosX = realChunkPos.x;
            float chunkPosZ = realChunkPos.z;

            int offsetX = Mathf.RoundToInt(chunkPosX / voxelSize);
            int offsetZ = Mathf.RoundToInt(chunkPosZ / voxelSize);

            int sizeX = template.lengthX_extra;
            int sizeZ = template.lengthZ_extra;

            //actual rasterization
            for (int i = 0; i < trs.Length; i += 3) {
                Vector3 A = vrts[trs[i]];
                Vector3 B = vrts[trs[i + 1]];
                Vector3 C = vrts[trs[i + 2]];

                sbyte passability = CalculateWalk(A, B, C, maxSlopeCos) ? (sbyte)Passability.Walkable : (sbyte)Passability.Slope;//if true then walkable else slope;

                int minX = Mathf.Clamp(Mathf.FloorToInt(SomeMath.Min(A.x, B.x, C.x) / voxelSize) - offsetX, 0, sizeX);
                int maxX = Mathf.Clamp(Mathf.CeilToInt(SomeMath.Max(A.x, B.x, C.x) / voxelSize) - offsetX, 0, sizeX);
                int minZ = Mathf.Clamp(Mathf.FloorToInt(SomeMath.Min(A.z, B.z, C.z) / voxelSize) - offsetZ, 0, sizeZ);
                int maxZ = Mathf.Clamp(Mathf.CeilToInt(SomeMath.Max(A.z, B.z, C.z) / voxelSize) - offsetZ, 0, sizeZ);

                for (int x = minX; x < maxX; x++) {
                    for (int z = minZ; z < maxZ; z++) {
                        float pointX = (x * voxelSize) + chunkPosX;
                        float pointZ = (z * voxelSize) + chunkPosZ;
                        if (SomeMath.LineSide(A.x, A.z, B.x, B.z, pointX, pointZ) <= 0.001 &
                            SomeMath.LineSide(B.x, B.z, C.x, C.z, pointX, pointZ) <= 0.001 &
                            SomeMath.LineSide(C.x, C.z, A.x, A.z, pointX, pointZ) <= 0.001) {
                            float height = SomeMath.CalculateHeight(A, B, C, pointX, pointZ);

                            compactData[GetIndex(x, z)].Update(height - 20, height, passability);
                        }
                    }
                }
            }

            return compactData;
        }
Exemple #5
0
        public CSRasterization2DResult Rasterize(Vector3[] verts, int[] tris, int sizeX, int sizeZ, float chunkPosX, float chunkPosZ, float voxelSize, float maxSlopeCos, bool debug = false)
        {
            if (tris.Length == 0)
            {
                return(null);
            }

            int dsLength       = tris.Length / 3;
            int dsTargetLength = Mathf.Max(Mathf.CeilToInt((float)dsLength / (float)cellSizeX), 1) * cellSizeX;

            DataSegment2D[] ds        = new DataSegment2D[dsTargetLength];
            int             sizeTotal = sizeX * sizeZ;

            int offsetX = Mathf.RoundToInt(chunkPosX / voxelSize);
            int offsetZ = Mathf.RoundToInt(chunkPosZ / voxelSize);

            for (int i = 0; i < tris.Length; i += 3)
            {
                Vector3 A = verts[tris[i]];
                Vector3 B = verts[tris[i + 1]];
                Vector3 C = verts[tris[i + 2]];

                //making data segments
                //data segment represent:
                //triangle index
                //expected passablity based on inclanation
                //min and max indexes in grid to check it presence
                ds[i / 3] =
                    new DataSegment2D(
                        i,
                        (CalculateWalk(A, B, C, maxSlopeCos) ? 3 : 1),                                              //if true then walkable else slope;
                        Mathf.Clamp(Mathf.FloorToInt(SomeMath.Min(A.x, B.x, C.x) / voxelSize) - offsetX, 0, sizeX), //minX
                        Mathf.Clamp(Mathf.CeilToInt(SomeMath.Max(A.x, B.x, C.x) / voxelSize) - offsetX, 0, sizeX),  //maxX
                        Mathf.Clamp(Mathf.FloorToInt(SomeMath.Min(A.z, B.z, C.z) / voxelSize) - offsetZ, 0, sizeZ), //minZ
                        Mathf.Clamp(Mathf.CeilToInt(SomeMath.Max(A.z, B.z, C.z) / voxelSize) - offsetZ, 0, sizeZ)   //maxZ
                        );
            }

            int kernel = CS.FindKernel("Rasterize");

            ComputeBuffer vertsBuffer       = new ComputeBuffer(verts.Length, sizeof(float) * 3);
            ComputeBuffer trisBuffer        = new ComputeBuffer(tris.Length, sizeof(int));
            ComputeBuffer voxelBuffer       = new ComputeBuffer(sizeTotal, Voxel2D.stride);
            ComputeBuffer dataSegmentBuffer = new ComputeBuffer(dsTargetLength, DataSegment2D.stride);

            CS.SetInt("SizeX", sizeX);
            CS.SetInt("SizeZ", sizeZ);
            CS.SetVector("ChunkPos", new Vector4(chunkPosX, 0, chunkPosZ, 0));
            CS.SetFloat("VoxelSize", voxelSize);

            CS.SetBuffer(kernel, "CurTris", trisBuffer);
            CS.SetBuffer(kernel, "CurVerts", vertsBuffer);
            CS.SetBuffer(kernel, "Result", voxelBuffer);
            CS.SetBuffer(kernel, "TargetSegments", dataSegmentBuffer);

            vertsBuffer.SetData(verts);
            trisBuffer.SetData(tris);
            Voxel2D[] voxels = new Voxel2D[sizeTotal];

            for (int i = 0; i < sizeTotal; i++)
            {
                voxels[i].passability = -1;
            }


            voxelBuffer.SetData(voxels);
            dataSegmentBuffer.SetData(ds);
            CS.Dispatch(kernel, dsTargetLength / cellSizeX, 1, 1);
            voxelBuffer.GetData(voxels);

            CSRasterization2DResult result = new CSRasterization2DResult(voxels);


            //debug
#if UNITY_EDITOR
            if (debug)
            {
                Debuger_K.AddMesh(verts, tris, new Color(1, 0, 1, 0.1f));

                //implementation of things goint on in compute shader

                foreach (var dataSet in ds)
                {
                    Vector3 A = verts[tris[dataSet.index]];
                    Vector3 B = verts[tris[dataSet.index + 1]];
                    Vector3 C = verts[tris[dataSet.index + 2]];

                    for (int x = dataSet.minX; x < dataSet.maxX; x++)
                    {
                        for (int z = dataSet.minZ; z < dataSet.maxZ; z++)
                        {
                            float pointX = (x * voxelSize) + chunkPosX;
                            float pointZ = (z * voxelSize) + chunkPosZ;


                            //if (SomeMath.LineSide(B.x, B.z, A.x, A.z, pointX, pointZ) <= 0.001 &
                            //    SomeMath.LineSide(A.x, A.z, C.x, C.z, pointX, pointZ) <= 0.001 &
                            //    SomeMath.LineSide(C.x, C.z, B.x, B.z, pointX, pointZ) <= 0.001) {

                            //    float height = SomeMath.CalculateHeight(A, B, C, pointX, pointZ);
                            //    Debuger_K.AddDot(new Vector3((x * voxelSize) + chunkPosX, height, (z * voxelSize) + chunkPosZ));

                            //    Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green);
                            //    Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green);
                            //    Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green);
                            //}
                            //else {
                            //    Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red);
                            //    Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red);
                            //    Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red);
                            //}


                            if (SomeMath.PointInTriangleSimple(A, B, C, pointX, pointZ))
                            {
                                float height = SomeMath.CalculateHeight(A, B, C, pointX, pointZ);
                                Debuger_K.AddDot(new Vector3((x * voxelSize) + chunkPosX, height, (z * voxelSize) + chunkPosZ));

                                //Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green);
                                //Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green);
                                //Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.green);
                            }
                            else
                            {
                                //Debuger_K.AddLine(A, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red);
                                //Debuger_K.AddLine(B, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red);
                                //Debuger_K.AddLine(C, new Vector3((x * voxelSize) + chunkPosX, 0, (z * voxelSize) + chunkPosZ), Color.red);
                            }
                        }
                    }
                }

                //for (int x = 0; x < sizeX; x++) {
                //    for (int z = 0; z < sizeZ; z++) {
                //        var curVoxel = voxels[x + (z * sizeX)];
                //        if (curVoxel.exist) {
                //            Debuger_K.AddDot(new Vector3((x * voxelSize) + chunkPosX, curVoxel.height, (z * voxelSize) + chunkPosZ));
                //        }
                //    }
                //}
            }
#endif

            vertsBuffer.Dispose();
            trisBuffer.Dispose();
            voxelBuffer.Dispose();
            dataSegmentBuffer.Dispose();
            return(result);
        }