예제 #1
0
        public Vector3[] GetSegmentPoints(Vector3 worldPoint)
        {
            Vector3[] res = new Vector3[2];

            Vector3 initSegment = Vector3.zero;
            Vector3 endSegment  = Vector3.zero;

            BranchPoint nearestPoint  = GetNearestPointFrom(worldPoint);
            BranchPoint nextPoint     = nearestPoint.GetNextPoint();
            BranchPoint previousPoint = nearestPoint.GetPreviousPoint();

            if (nextPoint != null && previousPoint != null)
            {
                float distanceToNextPoint     = (worldPoint - nextPoint.point).magnitude;
                float distanceToPreviousPoint = (worldPoint - previousPoint.point).magnitude;

                if (distanceToNextPoint <= distanceToPreviousPoint)
                {
                    initSegment = nearestPoint.point;
                    endSegment  = nextPoint.point;
                }
                else
                {
                    initSegment = previousPoint.point;
                    endSegment  = nearestPoint.point;
                }
            }

            res[0] = initSegment;
            res[1] = endSegment;


            return(res);
        }
예제 #2
0
        public void AddBranchPoint(Vector3 point, Vector3 grabVector, bool isNewBranch, int newBranchIndex)
        {
            BranchPoint newBranchPoint = new BranchPoint(point, grabVector,
                                                         branchPoints.Count, isNewBranch, newBranchIndex, totalLenght, this);

            branchPoints.Add(newBranchPoint);
        }
예제 #3
0
        public List <LeafPoint> GetLeavesInSegment(BranchPoint initSegment)
        {
            List <LeafPoint> res = new List <LeafPoint>();

            GetLeavesInSegment(initSegment, res);
            return(res);
        }
예제 #4
0
        /*BORRAR - Borrar este metodo!!!*/
        public void AddBranchPoint(BranchPoint branchPoint, float length, float stepSize)
        {
            branchPoint.branchContainer = this;
            branchPoints.Add(branchPoint);

            //totalLenght += stepSize;
        }
예제 #5
0
        //private Vector3 GetNewLeafUpward()
        //{
        //	Vector3 oldForward = leaves[i].lpForward;
        //	Vector3 newLeafPosition = Vector3.LerpUnclamped(previousPoint.point, nextPoint.point, leaves[i].displacementFromInitSegment);

        //	float angle = Vector3.Angle(oldForward, newForward);
        //	Vector3 axis = cameraForward;
        //	Vector3 crossNewForwardOldForward = Vector3.Cross(newForward, oldForward);
        //	float sign = Mathf.Sign(Vector3.Dot(crossNewForwardOldForward, cameraForward));
        //	Vector3 newUpward = Quaternion.AngleAxis(angle * -sign, axis) * leaves[i].lpUpward;

        //	return newUpward;
        //}

        public void RepositionLeavesAfterAdd02(BranchPoint newPoint)
        {
            BranchPoint previousPoint = newPoint.GetPreviousPoint();
            BranchPoint nextPoint     = newPoint.GetNextPoint();

            List <LeafPoint> leaves = new List <LeafPoint>();

            GetLeavesInSegment(previousPoint, leaves);
            //leaves.AddRange(GetLeavesInSegment(removedPoint));

            Vector3 dirSegment01 = (newPoint.point - previousPoint.point).normalized;
            Vector3 dirSegment02 = (nextPoint.point - newPoint.point).normalized;

            for (int i = 0; i < leaves.Count; i++)
            {
                Vector3 oldLeafVector01 = leaves[i].point - branchPoints[leaves[i].initSegmentIdx].point;
                Vector3 oldLeafVector02 = leaves[i].point - branchPoints[leaves[i].endSegmentIdx].point;

                Vector3 projectionOnSegment01     = previousPoint.point + dirSegment01 * Vector3.Dot(oldLeafVector01, dirSegment01);
                Vector3 projectionOnSegment02     = nextPoint.point + dirSegment02 * Vector3.Dot(oldLeafVector02, dirSegment02);
                Vector3 newLeafPositionToNewPoint = newPoint.point - projectionOnSegment01;

                if (Vector3.Dot(newLeafPositionToNewPoint, dirSegment01) >= 0)
                {
                    leaves[i].SetValues(projectionOnSegment01, leaves[i].lpLength, dirSegment01, leaves[i].lpUpward, leaves[i].chosenLeave, previousPoint, newPoint);
                }
                else
                {
                    leaves[i].SetValues(projectionOnSegment02, leaves[i].lpLength, dirSegment02, leaves[i].lpUpward, leaves[i].chosenLeave, newPoint, nextPoint);
                }
            }
        }
예제 #6
0
        //Si no encontramos muro en el paso anterior, entonces buscamos si tenemos suelo. tiramos el rayo y si da positivo, añadimos punto, calculamos growdirection y decimos al sistema que no estamos cayendo. Si por el contrario no
        //hemos encontrado suelo, intenamos agarrarnos al otro lado de la posible esquina.
        void CheckFloor(BranchContainer branch, BranchPoint potentialPoint, Vector3 oldSurfaceNormal)
        {
            Ray        ray = new Ray(potentialPoint.point, -oldSurfaceNormal);
            RaycastHit RC;

            if (Physics.Raycast(ray, out RC, branch.currentHeight * 2f, infoPool.ivyParameters.layerMask.value))
            {
                AddPoint(branch, RC.point, RC.normal);
                NewGrowDirection(branch);
                branch.fallIteration = 0f;
                branch.falling       = false;
            }
            else
            {
                if (Random.value < infoPool.ivyParameters.grabProvabilityOnFall)
                {
                    CheckCorner(branch, potentialPoint, oldSurfaceNormal);
                }
                else
                {
                    AddFallingPoint(branch);
                    branch.fallIteration  += 1f - infoPool.ivyParameters.stiffness;
                    branch.falling         = true;
                    branch.currentHeight   = 0f;
                    branch.heightParameter = -45f;
                }
            }
        }
예제 #7
0
        /*public List<LeafPoint> FilterLeavesByLengthRT(List<LeafPoint> leaves, float maxLength)
         * {
         *  List<LeafPoint> res = new List<LeafPoint>();
         *  for (int i = 0; i < leaves.Count; i++)
         *  {
         *      if(leaves[i].lpLength <= maxLength * 1.15f)
         *      {
         *          res.Add(leaves[i]);
         *      }
         *  }
         *
         *  return res;
         * }*/



        public void GetLeavesInSegment(BranchPoint initSegment, List <LeafPoint> res)
        {
            for (int i = 0; i < leaves.Count; i++)
            {
                if (leaves[i].initSegmentIdx == initSegment.index)
                {
                    res.Add(leaves[i]);
                }
            }
        }
예제 #8
0
        public void AddBranchPoint(BranchPoint branchPoint)
        {
            branchPoint.index           = branchPoints.Count;
            branchPoint.newBranch       = false;
            branchPoint.newBranchNumber = -1;
            branchPoint.branchContainer = this;
            branchPoint.length          = totalLenght;

            branchPoints.Add(branchPoint);
        }
예제 #9
0
        public LeafPoint InsertLeaf(Vector3 leafPoint, float lpLength, Vector3 lpForward, Vector3 lpUpward,
                                    int chosenLeave, int leafIndex, BranchPoint initSegment, BranchPoint endSegment)
        {
            LeafPoint newLeaf = new LeafPoint(leafPoint, lpLength, lpForward, lpUpward, chosenLeave, initSegment, endSegment);

            int clampedLeafIndex = Mathf.Clamp(leafIndex, 0, int.MaxValue);

            leaves.Insert(clampedLeafIndex, newLeaf);
            return(newLeaf);
        }
예제 #10
0
        private void AddLeaf(LeafInfo leafInfo)
        {
            BranchPoint branchPoint = overBranch.GetNearestPointFrom(mousePoint);

            LeafPoint nextLeaf  = GetNextLeaf(leafInfo);
            int       leafIndex = overBranch.leaves.IndexOf(nextLeaf);

            lastLeafPoint = overBranch.AddRandomLeaf(leafInfo.pointWS, overSegment[0], overSegment[1], leafIndex, infoPool);

            RefreshMesh(true, true);
        }
예제 #11
0
        public void RepositionLeaves02(List <LeafPoint> leaves, bool updatePosition)
        {
            for (int i = 0; i < leaves.Count; i++)
            {
                BranchPoint previousPoint = null;
                BranchPoint nextPoint     = null;


                if (leaves[i].initSegmentIdx > branchPoints.Count - 1 || leaves[i].endSegmentIdx > branchPoints.Count - 1)
                {
                    previousPoint = branchPoints[leaves[i].initSegmentIdx];
                    nextPoint     = branchPoints[leaves[i].endSegmentIdx - 1];
                }
                else
                {
                    previousPoint = branchPoints[leaves[i].initSegmentIdx];
                    nextPoint     = branchPoints[leaves[i].endSegmentIdx];
                }


                Vector3 newForward = (nextPoint.point - previousPoint.point).normalized;


                Vector3 oldForward = leaves[i].lpForward;

                leaves[i].forwarRot = Quaternion.FromToRotation(oldForward, newForward);


                Vector3 newLeafPosition = Vector3.LerpUnclamped(previousPoint.point, nextPoint.point, leaves[i].displacementFromInitSegment);
                float   angle           = Vector3.Angle(oldForward, newForward);


                //Vector3 axis = SceneView.currentDrawingSceneView.camera.transform.forward;
                Vector3 cameraForward = SceneView.currentDrawingSceneView.camera.transform.forward;

                /*Vector3 axis = -leaves[i].left.normalized;*/
                Vector3 axis = cameraForward;

                Vector3 crossNewForwardOldForward = Vector3.Cross(newForward, oldForward);
                float   sign = Mathf.Sign(Vector3.Dot(crossNewForwardOldForward, cameraForward));

                //float sign = Mathf.Sign(Vector3.Dot(newForward, oldForward));
                //Vector3 newUpward = Vector3.Cross(-newForward, -leaves[i].left.normalized).normalized;
                //Vector3 newUpward = Quaternion.AngleAxis(angle * -sign, axis) * leaves[i].lpUpward;
                //newUpward = newUpward.normalized;*/

                if (updatePosition)
                {
                    leaves[i].point = newLeafPosition;
                }
            }
        }
예제 #12
0
        public LeafPoint AddRandomLeaf(Vector3 pointWS, BranchPoint initSegment, BranchPoint endSegment, int leafIndex, InfoPool infoPool)
        {
            int chosenLeave = UnityEngine.Random.Range(0, infoPool.ivyParameters.leavesPrefabs.Length);

            Vector3 forward = initSegment.initialGrowDir;

            float     lpLength = initSegment.length + Vector3.Distance(pointWS, initSegment.point);
            LeafPoint res      = InsertLeaf(pointWS, lpLength, forward,
                                            -initSegment.grabVector, chosenLeave, leafIndex,
                                            initSegment, endSegment);

            return(res);
        }
예제 #13
0
        public BranchPoint GetNearestPointSSFrom(Vector2 from)
        {
            BranchPoint res         = null;
            float       minDistance = float.MaxValue;

            for (int i = 0; i < branchPoints.Count; i++)
            {
                float newSqrDst = (branchPoints[i].pointSS - from).sqrMagnitude;
                if (newSqrDst <= minDistance)
                {
                    res         = branchPoints[i];
                    minDistance = newSqrDst;
                }
            }

            return(res);
        }
예제 #14
0
        public BranchPoint InsertBranchPoint(Vector3 point, Vector3 grabVector, int index)
        {
            //float newPointLength = branchPoints[index].length + Vector3.Distance(branchPoints[index].point, point);
            float newPointLength = Mathf.Lerp(branchPoints[index - 1].length, branchPoints[index].length, 0.5f);

            BranchPoint newBranchPoint = new BranchPoint(point, grabVector, index, newPointLength, this);

            branchPoints.Insert(index, newBranchPoint);


            //Debug.Log("LENGTHS: " + branchPoints[index].length.ToString("F10") + " - " + newPointLength.ToString("F10") + " - " + branchPoints[index + 1].length.ToString("F10"));

            for (int i = index + 1; i < branchPoints.Count; i++)
            {
                branchPoints[i].index += 1;
            }

            return(newBranchPoint);
        }
예제 #15
0
        public void SetValues(Vector3 point, float lpLength, Vector3 lpForward, Vector3 lpUpward,
                              int chosenLeave, BranchPoint initSegment, BranchPoint endSegment)
        {
            this.point          = point;
            this.lpLength       = lpLength;
            this.lpForward      = lpForward;
            this.lpUpward       = lpUpward;
            this.chosenLeave    = chosenLeave;
            this.initSegmentIdx = initSegment.index;
            this.endSegmentIdx  = endSegment.index;
            this.forwarRot      = Quaternion.identity;

            float segmentDistance = (initSegment.point - endSegment.point).magnitude;
            float t = (point - initSegment.point).magnitude / segmentDistance;

            this.displacementFromInitSegment = Mathf.Clamp(t, 0.01f, 0.99f);
            this.left = Vector3.Cross(lpForward, lpUpward).normalized;

            //this.verticesLeaves = new List<RTVertexData>();
        }
예제 #16
0
 public void SetValues(Vector3 growDirection, float randomizeHeight,
                       float currentHeight, float heightParameter, int branchSense, BranchPoint originPointOfThisBranch)
 {
     this.branchPoints            = new List <BranchPoint>(1000);
     this.growDirection           = growDirection;
     this.leaves                  = new List <LeafPoint>(1000);
     this.totalLenght             = 0f;
     this.fallIteration           = 0f;
     this.falling                 = false;
     this.rotationOnFallIteration = Quaternion.identity;
     this.branchSense             = branchSense;
     this.heightParameter         = heightParameter;
     this.randomizeHeight         = randomizeHeight;
     this.heightVar               = 0f;
     this.currentHeight           = currentHeight;
     this.deltaHeight             = 0f;
     this.newHeight               = 0f;
     this.originPointOfThisBranch = originPointOfThisBranch;
     this.branchNumber            = -1;
 }
예제 #17
0
        public void RepositionLeavesAfterRemove02(BranchPoint removedPoint)
        {
            BranchPoint previousPoint = removedPoint.GetPreviousPoint();
            BranchPoint nextPoint     = removedPoint.GetNextPoint();

            List <LeafPoint> leaves = GetLeavesInSegment(previousPoint);

            leaves.AddRange(GetLeavesInSegment(removedPoint));

            for (int i = 0; i < leaves.Count; i++)
            {
                Vector3 pointToLeaf   = leaves[i].point - previousPoint.point;
                Vector3 newSegmentDir = (nextPoint.point - previousPoint.point).normalized;
                float   dotProduct    = Vector3.Dot(pointToLeaf, newSegmentDir);

                Vector3 newLeafPosition = previousPoint.point + newSegmentDir * dotProduct;

                //leaves[i].point = newLeafPosition;
                leaves[i].SetValues(newLeafPosition, leaves[i].lpLength, previousPoint.initialGrowDir,
                                    -previousPoint.grabVector, leaves[i].chosenLeave, previousPoint, nextPoint);

                //leaves[i].lpLength = 0f;
            }
        }