Example #1
0
        public Vector3 GetFirstVector(RTBranchPoint rtBranchPoint, RTBranchContainer rtBranchContainer,
                                      RTIvyContainer rtIvyContainer, IvyParameters ivyParameters, Vector3 axis)
        {
            Vector3 firstVector = Vector3.zero;

            if (rtBranchContainer.branchNumber == 0 && rtBranchPoint.index == 0)
            {
                if (!ivyParameters.halfgeom)
                {
                    firstVector = rtIvyContainer.firstVertexVector;
                }
                else
                {
                    firstVector = Quaternion.AngleAxis(90f, axis) * rtIvyContainer.firstVertexVector;
                }
            }
            else
            {
                if (!ivyParameters.halfgeom)
                {
                    firstVector = Vector3.Normalize(Vector3.ProjectOnPlane(rtBranchPoint.grabVector, axis));
                }
                else
                {
                    firstVector = Quaternion.AngleAxis(90f, axis) * Vector3.Normalize(Vector3.ProjectOnPlane(rtBranchPoint.grabVector, axis));
                }
            }

            return(firstVector);
        }
Example #2
0
        public void CalculateVerticesLoop(IvyParameters ivyParameters, RTIvyContainer rtIvyContainer, GameObject ivyGO, Vector3 firstVector, Vector3 axis, float radius)
        {
            this.firstVector = firstVector;
            this.axis        = axis;
            this.radius      = radius;

            CalculateVerticesLoop(ivyParameters, rtIvyContainer, ivyGO);
        }
Example #3
0
        public void CalculateVerticesLoop(IvyParameters ivyParameters, RTIvyContainer rtIvyContainer, GameObject ivyGO)
        {
            float angle = 0f;

            if (!ivyParameters.halfgeom)
            {
                angle = Mathf.Rad2Deg * 2 * Mathf.PI / ivyParameters.sides;
            }
            else
            {
                angle = Mathf.Rad2Deg * 2 * Mathf.PI / ivyParameters.sides / 2;
            }



            Vector3    vertex    = Vector3.zero;
            Vector3    normal    = Vector3.zero;
            Vector2    uv        = Vector2.zero;
            Quaternion quat      = Quaternion.identity;
            Vector3    direction = Vector3.zero;


            Quaternion inverseIvyGORotation = Quaternion.Inverse(ivyGO.transform.rotation);



            for (int i = 0; i < ivyParameters.sides + 1; i++)
            {
                quat      = Quaternion.AngleAxis(angle * i, axis);
                direction = quat * firstVector;

                if (ivyParameters.halfgeom && ivyParameters.sides == 1)
                {
                    normal = -grabVector;
                }
                else
                {
                    normal = direction;
                }

                normal = inverseIvyGORotation * normal;



                vertex  = direction * radius + point;
                vertex -= ivyGO.transform.position;
                vertex  = inverseIvyGORotation * vertex;

                uv = new Vector2(length * ivyParameters.uvScale.y + ivyParameters.uvOffset.y - ivyParameters.stepSize,
                                 1f / ivyParameters.sides * i * ivyParameters.uvScale.x + ivyParameters.uvOffset.x);


                verticesLoop[i] = new RTVertexData(vertex, normal, uv, Vector2.zero, Color.black);
            }
        }
Example #4
0
        public Vector3 GetLoopAxis(RTBranchPoint rtBranchPoint, RTBranchContainer rtBranchContainer, RTIvyContainer rtIvyContainer, GameObject ivyGo)
        {
            Vector3 axis = Vector3.zero;

            if (rtBranchPoint.index == 0 && rtBranchContainer.branchNumber == 0)
            {
                axis = ivyGo.transform.up;
            }
            else
            {
                if (rtBranchPoint.index == 0)
                {
                    axis = rtBranchPoint.GetNextPoint().point - rtBranchPoint.point;
                }
                else
                {
                    axis = Vector3.Normalize(Vector3.Lerp(rtBranchPoint.point - rtBranchPoint.GetPreviousPoint().point,
                                                          rtBranchPoint.GetNextPoint().point - rtBranchPoint.point, 0.5f));
                }
            }

            return(axis);
        }
Example #5
0
        public void Init(RTIvyContainer ivyContainer, IvyParameters ivyParameters,
                         GameObject ivyGO, RTMeshData[] leavesMeshesByChosenLeaf,
                         int numPoints, int numLeaves, int maxNumVerticesPerLeaf)
        {
            this.rtIvyContainer           = ivyContainer;
            this.ivyParameters            = ivyParameters;
            this.ivyGO                    = ivyGO;
            this.leavesMeshesByChosenLeaf = leavesMeshesByChosenLeaf;
            this.numPoints                = numPoints;
            this.numLeaves                = numLeaves;
            this.maxNumVerticesPerLeaf    = maxNumVerticesPerLeaf;

            this.branchPointsPool     = new RTBranchPoint[numPoints];
            this.branchPointPoolIndex = 0;

            for (int i = 0; i < numPoints; i++)
            {
                RTBranchPoint branchPoint = new RTBranchPoint();
                branchPoint.PreInit(ivyParameters);
                branchPointsPool[i] = branchPoint;
            }

            this.leavesPool      = new RTLeafPoint[numLeaves];
            this.leavesPoolIndex = 0;
            for (int i = 0; i < numLeaves; i++)
            {
                RTLeafPoint leafPoint = new RTLeafPoint();
                leafPoint.PreInit(maxNumVerticesPerLeaf);
                leavesPool[i] = leafPoint;
            }

            this.branchesPool = new RTBranchContainer[ivyParameters.maxBranchs];
            for (int i = 0; i < ivyParameters.maxBranchs; i++)
            {
                this.branchesPool[i] = new RTBranchContainer(numPoints, numLeaves);
            }


            Random.InitState(System.Environment.TickCount);


            RTBranchContainer firstBranch = GetNextBranchContainer();



            ivyContainer.AddBranch(firstBranch);



            RTBranchPoint nextRTBranchPoint = GetNextFreeBranchPoint();

            nextRTBranchPoint.SetValues(ivyGO.transform.position, -ivyGO.transform.up, false, 0);
            firstBranch.AddBranchPoint(nextRTBranchPoint, ivyParameters.stepSize);

            CalculateVerticesLastPoint(firstBranch);
            //Vector3 axis = GetLoopAxis(nextRTBranchPoint, firstBranch, rtIvyContainer, ivyGO);
            //Vector3 firstVector = GetFirstVector(nextRTBranchPoint, firstBranch, rtIvyContainer, ivyParameters, axis);
            //nextRTBranchPoint.CalculateCenterLoop(ivyGO);
            //nextRTBranchPoint.CalculateVerticesLoop(ivyParameters, rtIvyContainer, ivyGO, firstVector, axis);



            ivyContainer.branches[0].growDirection = Quaternion.AngleAxis(Random.value * 360f, ivyGO.transform.up) *
                                                     ivyGO.transform.forward;

            ivyContainer.firstVertexVector           = ivyContainer.branches[0].growDirection;
            ivyContainer.branches[0].randomizeHeight = Random.Range(4f, 8f);
            CalculateNewHeight(ivyContainer.branches[0]);
            ivyContainer.branches[0].branchSense = ChooseBranchSense();
            randomstate = Random.state;
        }
Example #6
0
        public RTBranchContainer(BranchContainer branchContainer, IvyParameters ivyParameters, RTIvyContainer rtIvyContainer,
                                 GameObject ivyGO, RTMeshData[] leavesMeshesByChosenLeaf)
        {
            this.totalLength             = branchContainer.totalLenght;
            this.growDirection           = branchContainer.growDirection;
            this.randomizeHeight         = branchContainer.randomizeHeight;
            this.heightVar               = branchContainer.heightVar;
            this.newHeight               = branchContainer.newHeight;
            this.heightParameter         = branchContainer.heightParameter;
            this.deltaHeight             = branchContainer.deltaHeight;
            this.currentHeight           = branchContainer.currentHeight;
            this.branchSense             = branchContainer.branchSense;
            this.falling                 = branchContainer.falling;
            this.rotationOnFallIteration = branchContainer.rotationOnFallIteration;
            this.branchNumber            = branchContainer.branchNumber;



            this.branchPoints = new List <RTBranchPoint>(branchContainer.branchPoints.Count);
            for (int i = 0; i < branchContainer.branchPoints.Count; i++)
            {
                RTBranchPoint rtBranchPoint = new RTBranchPoint(branchContainer.branchPoints[i], this);

                rtBranchPoint.CalculateCenterLoop(ivyGO);
                rtBranchPoint.PreInit(ivyParameters);
                rtBranchPoint.CalculateVerticesLoop(ivyParameters, rtIvyContainer, ivyGO);

                this.branchPoints.Add(rtBranchPoint);
            }


            branchContainer.PrepareRTLeavesDict();


            if (ivyParameters.generateLeaves)
            {
                this.leavesOrderedByInitSegment = new RTLeafPoint[branchPoints.Count][];
                for (int i = 0; i < branchPoints.Count; i++)
                {
                    List <LeafPoint> leavesToBake = branchContainer.dictRTLeavesByInitSegment[i];
                    int numLeaves = 0;
                    if (leavesToBake != null)
                    {
                        numLeaves = leavesToBake.Count;
                    }


                    this.leavesOrderedByInitSegment[i] = new RTLeafPoint[numLeaves];


                    for (int j = 0; j < numLeaves; j++)
                    {
                        RTLeafPoint rtLeafPoint  = new RTLeafPoint(leavesToBake[j], ivyParameters);
                        RTMeshData  leafMeshData = leavesMeshesByChosenLeaf[rtLeafPoint.chosenLeave];

                        rtLeafPoint.CreateVertices(ivyParameters, leafMeshData, ivyGO);
                        this.leavesOrderedByInitSegment[i][j] = rtLeafPoint;
                    }
                }
            }
        }
Example #7
0
        /* public void InitIvy(RuntimeGrowthParameters growthParameters, IvyContainer ivyContainer,
         *      IvyParameters ivyParameters)
         * {
         *      this.growthParameters = growthParameters;
         *      Init(ivyContainer, ivyParameters);
         *      AddFirstBranch();
         * } */



        protected virtual void Init(IvyContainer ivyContainer, IvyParameters ivyParameters)
        {
            this.rtIvyContainer = new RTIvyContainer();

            this.ivyParameters = new IvyParameters();
            this.ivyParameters.CopyFrom(ivyParameters);

            CreateLeavesDict();

            if (ivyContainer != null)
            {
                this.rtIvyContainer.Initialize(ivyContainer, ivyParameters, gameObject,
                                               leavesMeshesByChosenLeaf, ivyContainer.firstVertexVector);
            }
            else
            {
                this.rtIvyContainer.Initialize();
            }



            SetUpMaxBranches(ivyContainer);


            activeBakedBranches    = new List <RTBranchContainer>(maxBranches);
            activeBuildingBranches = new List <RTBranchContainer>(maxBranches);

            rtBuildingIvyContainer = new RTIvyContainer();

            Vector3 firstVertexVector = ivyContainer == null?CalculateFirstVertexVector() : ivyContainer.firstVertexVector;

            rtBuildingIvyContainer.Initialize(firstVertexVector);
            lastIdxActiveBranch = -1;

            leafLengthCorrrectionFactor = 1;

            int submeshCount = ivyParameters.leavesPrefabs.Length + 1;

            this.processedMesh         = new Mesh();
            processedMesh.subMeshCount = submeshCount;
            mfProcessedMesh.sharedMesh = processedMesh;

            this.refreshProcessedMesh = false;
            this.backtrackingPoints   = GetBacktrackingPoints();

            if (this.bakedMesh == null)
            {
                this.bakedMesh = new Mesh();
                this.bakedMesh.subMeshCount = submeshCount;
            }

            /* meshBuilder = new RTBakedMeshBuilder(rtIvyContainer, gameObject);
             * meshBuilder.InitializeMeshBuilder(ivyParameters, rtBuildingIvyContainer, rtIvyContainer,
             *      gameObject, bakedMesh, meshRenderer, meshFilter, maxBranches,
             *      processedMesh, growthParameters.growthSpeed, mrProcessedMesh,
             *      backtrackingPoints, submeshByChoseLeaf, leavesMeshesDict, leavesMaterials.ToArray()); */



            lastCopiedIndexPerBranch = new List <int>(maxBranches);
            leavesToCopyMesh         = new List <LeafPoint>(50);
            srcPoints = new List <Vector3>(maxBranches);
            dstPoints = new List <Vector3>(maxBranches);
            growingFactorPerBranch  = new List <float>(maxBranches);
            srcTotalLengthPerBranch = new List <float>(maxBranches);
            dstTotalLengthPerBranch = new List <float>(maxBranches);
            lengthPerBranch         = new List <float>(maxBranches);


            for (int i = 0; i < maxBranches; i++)
            {
                srcPoints.Add(Vector3.zero);
                dstPoints.Add(Vector3.zero);
                growingFactorPerBranch.Add(0f);
                srcTotalLengthPerBranch.Add(0f);
                dstTotalLengthPerBranch.Add(0f);
                lastCopiedIndexPerBranch.Add(-1);
                lengthPerBranch.Add(0f);

                int branchPointsSize = GetMaxNumPoints();
                int numLeaves        = GetMaxNumLeaves();

                RTBranchContainer branchContainer = new RTBranchContainer(branchPointsSize, numLeaves);
                activeBuildingBranches.Add(branchContainer);
            }
        }
        public void InitializeMeshBuilder(IvyParameters ivyParameters, RTIvyContainer ivyContainer,
                                          RTIvyContainer bakedIvyContainer, GameObject ivyGO, Mesh bakedMesh, MeshRenderer meshRenderer,
                                          MeshFilter meshFilter, int numBranches, Mesh processedMesh, float growSpeed, MeshRenderer mrProcessedMesh,
                                          int backtrackingPoints, int[] submeshByChoseLeaf, RTMeshData[] leavesMeshesByChosenLeaf, Material[] materials)
        {
            this.ivyParameters       = ivyParameters;
            this.rtIvyContainer      = ivyContainer;
            this.rtBakedIvyContainer = bakedIvyContainer;
            this.ivyGO = ivyGO;

            this.meshRenderer = meshRenderer;
            this.meshFilter   = meshFilter;

            this.processedMesh             = processedMesh;
            this.processedMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt16;

            this.mrProcessedMesh          = mrProcessedMesh;
            this.submeshByChoseLeaf       = submeshByChoseLeaf;
            this.leavesMeshesByChosenLeaf = leavesMeshesByChosenLeaf;

            activeBranches = new List <RTBranchContainer>();

            this.fromTo  = new int[2];
            this.vectors = new Vector3[2];

            this.growthSpeed        = growSpeed;
            this.backtrackingPoints = backtrackingPoints;

            this.submeshCount = meshRenderer.sharedMaterials.Length;

            vertCountsPerBranch                       = new int[numBranches];
            lastTriangleIndexPerBranch                = new int[numBranches];
            vertCountLeavesPerBranch                  = new int[numBranches];
            processedVerticesIndicesPerBranch         = new List <List <int> >(numBranches);
            processedBranchesVerticesIndicesPerBranch = new List <List <int> >(numBranches);

            for (int i = 0; i < numBranches; i++)
            {
                processedVerticesIndicesPerBranch.Add(new List <int>());
                processedBranchesVerticesIndicesPerBranch.Add(new List <int>());
            }


            this.vertCount = 0;

            ivyMesh = new Mesh();
            ivyMesh.subMeshCount = submeshCount;
            ivyMesh.name         = Constants.IVY_MESH_NAME;

            meshFilter.mesh = ivyMesh;


            ivyGO.GetComponent <MeshRenderer>().sharedMaterials = materials;
            mrProcessedMesh.sharedMaterials = materials;



            leavesDataInitialized = true;



            ivyGoPosition        = ivyGO.transform.position;
            ivyGoRotation        = ivyGO.transform.rotation;
            ivyGoInverseRotation = Quaternion.Inverse(ivyGO.transform.rotation);
            zeroVector3          = Vector3.zero;
            zeroVector2          = Vector2.zero;
            blackColor           = Color.black;
        }
 public RTBakedMeshBuilder(RTIvyContainer ivyContainer, GameObject ivyGo)
 {
     this.rtIvyContainer = ivyContainer;
     this.ivyGO          = ivyGo;
 }