Exemple #1
0
        //----------------------------------
        void getPointsIntersectingSphere(List <int> points, float3 sphereCenter, float sphereRadius)
        {
            List <SpatialQuadTreeCell> nodes = new List <SpatialQuadTreeCell>();

            getLeafCellsIntersectingSphere(nodes, sphereCenter, sphereRadius);
            if (nodes.Count == 0)
            {
                return;
            }

            Vector3 center = sphereCenter.toVec3();

            for (int q = 0; q < nodes.Count; q++)
            {
                RealLOSRepQuadCell cell = ((RealLOSRepQuadCell)nodes[q].mExternalData);
                Debug.Assert(cell != null);
                for (uint i = (uint)cell.mMinXVert; i < (uint)cell.mMinXVert + mNumXVertsPerCell; i++)
                {
                    for (uint j = (uint)cell.mMinZVert; j < (uint)cell.mMinZVert + mNumZVertsPerCell; j++)
                    {
                        uint    index = (uint)((i) + mWidth * (j));
                        Vector3 vert  = getWorldspacePoint((int)(i), (int)(j)).toVec3();
                        if (BMathLib.pointSphereIntersect(ref center, sphereRadius, ref vert))
                        {
                            points.Add((int)index);
                        }
                    }
                }
            }
        }
Exemple #2
0
        public bool intersectsRay(float3 rayOrig, float3 rayDir)
        {
            Vector3 tc     = Vector3.Empty;
            float   tt     = 0;
            Vector3 origin = rayOrig.toVec3();
            Vector3 dir    = rayDir.toVec3();
            Vector3 mn     = mAABBMin.toVec3();
            Vector3 mx     = mAABBMax.toVec3();

            return(BMathLib.ray3AABB(ref tc, ref tt, ref origin, ref dir, ref mn, ref mx));
        }
Exemple #3
0
        public Vector3 getIntersectPointFromScreenCursor()
        {
            float3  currIntersectionPt = float3.Empty;
            Vector3 orig = TerrainGlobals.getEditor().getRayPosFromMouseCoords(false);
            Vector3 dir  = TerrainGlobals.getEditor().getRayPosFromMouseCoords(true) - orig;

            dir = BMathLib.Normalize(dir);

            //get our intersect point
            if (!getClosestIntersectionPoint(new float3(orig), new float3(dir), out currIntersectionPt))
            {
                return(Vector3.Empty);
            }

            return(currIntersectionPt.toVec3());
        }
Exemple #4
0
        public void applySimRepBrush(bool alternate)
        {
            if (TerrainGlobals.getEditor().getStrokeInputType() == BTerrainEditor.eEditorStrokeInput.cStrokeInputMouse)
            {
                //raycast and get our intersection
                Vector3 orig = TerrainGlobals.getEditor().getRayPosFromMouseCoords(false);
                Vector3 dir  = TerrainGlobals.getEditor().getRayPosFromMouseCoords(true) - orig;
                dir = BMathLib.Normalize(dir);

                //get our intersect point
                if (!getClosestIntersectionPoint(new float3(orig), new float3(dir), out mLastMoustIntPt))
                {
                    return;
                }

                //If we're in tile mode, shift our center over to better match what our cursor is doing...
                if (TerrainGlobals.getEditor().getMode() == BTerrainEditor.eEditorMode.cModeSimBuildibility ||
                    TerrainGlobals.getEditor().getMode() == BTerrainEditor.eEditorMode.cModeSimPassibility ||
                    TerrainGlobals.getEditor().getMode() == BTerrainEditor.eEditorMode.cModeSimFloodPassibility ||
                    TerrainGlobals.getEditor().getMode() == BTerrainEditor.eEditorMode.cModeSimScarabPassibility)
                {
                    mLastMoustIntPt.X += mTileScale;
                    mLastMoustIntPt.Z += mTileScale;
                }

                // Undo info
                // addVertexUndoNodes(nodes, false);

                // Find affected points
                List <int> points = new List <int>();
                getPointsIntersectingSphere(points, mLastMoustIntPt, TerrainGlobals.getEditor().mBrushInfo.mRadius);

                Vector3   intPoint  = mLastMoustIntPt.toVec3();
                Vector3   intNormal = BMathLib.unitY;
                BrushInfo bi        = TerrainGlobals.getEditor().getCurrentBrushInfo();
                ((BTerrainSimBrush)TerrainGlobals.getEditor().getCurrentBrush()).applyOnBrush(points, ref intPoint, ref intNormal,
                                                                                              ref bi,
                                                                                              alternate);

                // addVertexUndoNodes(nodes, true);
            }
            else if (TerrainGlobals.getEditor().getStrokeInputType() == BTerrainEditor.eEditorStrokeInput.cStrokeInputKeyboard)
            {
                BrushInfo bi = TerrainGlobals.getEditor().getCurrentBrushInfo();
                ((BTerrainSimBrush)TerrainGlobals.getEditor().getCurrentBrush()).applyOnSelection(Masking.getCurrSelectionMaskWeights(), bi.mIntensity, alternate);
            }
        }
Exemple #5
0
        public void applyCameraRepBrush(bool alternate)
        {
            if (TerrainGlobals.getEditor().getStrokeInputType() == BTerrainEditor.eEditorStrokeInput.cStrokeInputMouse)
            {
                //raycast and get our intersection
                Vector3 orig = TerrainGlobals.getEditor().getRayPosFromMouseCoords(false);
                Vector3 dir  = TerrainGlobals.getEditor().getRayPosFromMouseCoords(true) - orig;
                dir = BMathLib.Normalize(dir);

                //get our intersect point
                if (!getClosestIntersectionPoint(new float3(orig), new float3(dir), out mLastMoustIntPt))
                {
                    return;
                }

                // Undo info
                // addVertexUndoNodes(nodes, false);

                // Find affected points
                List <int> points = new List <int>();
                getPointsIntersectingSphere(points, mLastMoustIntPt, TerrainGlobals.getEditor().mBrushInfo.mRadius);

                Vector3   intPoint  = mLastMoustIntPt.toVec3();
                Vector3   intNormal = BMathLib.unitY;
                BrushInfo bi        = TerrainGlobals.getEditor().getCurrentBrushInfo();
                ((BTerrainCameraBrush)TerrainGlobals.getEditor().getCurrentBrush()).applyOnBrush(points, ref intPoint, ref intNormal,
                                                                                                 ref bi,
                                                                                                 alternate);

                // addVertexUndoNodes(nodes, true);
            }
            else if (TerrainGlobals.getEditor().getStrokeInputType() == BTerrainEditor.eEditorStrokeInput.cStrokeInputKeyboard)
            {
                //int multiplier = 10;
                //// Apply on selection

                //List<BTerrainQuadNode> nodes = new List<BTerrainQuadNode>();
                //TerrainGlobals.getTerrain().getQuadNodeRoot().getTileBoundsIntersection(nodes, Masking.mCurrSelectionMaskExtends.minX, Masking.mCurrSelectionMaskExtends.maxX,
                //                                                                         Masking.mCurrSelectionMaskExtends.minZ, Masking.mCurrSelectionMaskExtends.maxZ);



                //((BTerrainFoliageBrush)mCurrBrush).applyOnSelection(Masking.getCurrSelectionMaskWeights(), this.mBrushInfo.mIntensity,
                //   alternate || mCurrMode == eEditorMode.cModeFoliageErase);
            }
        }
Exemple #6
0
        void recalculateCellBB(SpatialQuadTreeCell spatialCell)
        {
            BBoundingBox bb = new BBoundingBox();

            bb.empty();
            RealLOSRepQuadCell cell = ((RealLOSRepQuadCell)spatialCell.mExternalData);

            for (uint i = (uint)cell.mMinXVert; i <= cell.mMinXVert + mNumXVertsPerCell; i++)
            {
                for (uint j = (uint)cell.mMinZVert; j <= cell.mMinZVert + mNumZVertsPerCell; j++)
                {
                    if (i >= mWidth || j >= mHeight)
                    {
                        continue;
                    }

                    float3 worldPos = getWorldspacePoint((int)i, (int)j);
                    bb.addPoint(worldPos.toVec3());
                }
            }
            spatialCell.setBounds(new float3(bb.min), new float3(bb.max));
            bb = null;
        }
Exemple #7
0
 //--------------------------------------------------------------------
 public bool intersectsSphere(float3 sphereCenter, float sphereRadius)
 {
     return(BMathLib.sphereAABBIntersect(sphereCenter.toVec3(), sphereRadius, mAABBMin.toVec3(), mAABBMax.toVec3()));
 }
Exemple #8
0
        bool getClosestIntersectionPoint(float3 rayOrig, float3 rayDir, out float3 intPt)
        {
            intPt = float3.Empty;

            List <SpatialQuadTreeCell> nodes = new List <SpatialQuadTreeCell>();

            getLeafCellsIntersectingRay(nodes, rayOrig, rayDir);
            if (nodes.Count == 0)
            {
                return(false);
            }


            //walk through each cell returned, do a per polygon intersection with each one..

            Vector3 origin = rayOrig.toVec3();
            Vector3 dir    = rayDir.toVec3();

            Vector3 closestIntersect = Vector3.Empty;
            float   closestDist      = float.MaxValue;

            Vector3[] verts = new Vector3[3];
            bool      hit   = false;

            for (int q = 0; q < nodes.Count; q++)
            {
                RealLOSRepQuadCell cell = ((RealLOSRepQuadCell)nodes[q].mExternalData);
                Debug.Assert(cell != null);

                int minx = (int)(cell.mMinXVert);
                int minz = (int)(cell.mMinZVert);
                for (uint i = 0; i < mNumXVertsPerCell; i++)
                {
                    for (uint j = 0; j < mNumZVertsPerCell; j++)
                    {
                        int tileI = (int)(minx + i);
                        int tileJ = (int)(minz + j);
                        if (tileI >= mWidth - 1 || tileJ >= mHeight - 1)
                        {
                            continue;
                        }


                        bool    tHit = false;
                        Vector3 pt   = Vector3.Empty;

                        float[] h = new float[4];
                        h[0] = getCompositeHeight(tileI, tileJ);
                        h[1] = getCompositeHeight(tileI, tileJ + 1);
                        h[2] = getCompositeHeight(tileI + 1, tileJ);
                        h[3] = getCompositeHeight(tileI + 1, tileJ + 1);


                        if (h[0] != cJaggedEmptyValue && h[3] != cJaggedEmptyValue && h[1] != cJaggedEmptyValue)
                        {
                            verts[0] = new Vector3(tileI * mTileScale, h[0], (tileJ) * mTileScale);
                            verts[1] = new Vector3((tileI + 1) * mTileScale, h[3], (tileJ + 1) * mTileScale);
                            verts[2] = new Vector3(tileI * mTileScale, h[1], (tileJ + 1) * mTileScale);

                            if (BMathLib.raySegmentIntersectionTriangle(verts, ref origin, ref dir, false, ref pt))
                            {
                                Vector3 vec = pt - origin;
                                //ensure this hit point is the closest to the origin
                                float len = vec.Length();
                                if (len < closestDist)
                                {
                                    closestDist      = len;
                                    closestIntersect = pt;
                                }
                                hit = true;
                            }
                        }

                        if (h[0] != cJaggedEmptyValue && h[3] != cJaggedEmptyValue && h[2] != cJaggedEmptyValue)
                        {
                            verts[0] = new Vector3(tileI * mTileScale, h[0], (tileJ) * mTileScale);
                            verts[1] = new Vector3((tileI + 1) * mTileScale, h[3], (tileJ + 1) * mTileScale);
                            verts[2] = new Vector3((tileI + 1) * mTileScale, h[2], (tileJ) * mTileScale);

                            if (BMathLib.raySegmentIntersectionTriangle(verts, ref origin, ref dir, false, ref pt))
                            {
                                Vector3 vec = pt - origin;
                                //ensure this hit point is the closest to the origin
                                float len = vec.Length();
                                if (len < closestDist)
                                {
                                    closestDist      = len;
                                    closestIntersect = pt;
                                }
                                hit = true;
                            }
                        }
                    }
                }
            }

            intPt.X = closestIntersect.X;
            intPt.Y = closestIntersect.Y;
            intPt.Z = closestIntersect.Z;
            return(hit);
        }