Beispiel #1
0
    private PlantNode AddNode(PlantNode parent, int index, Vector3 angle, PlantNode.NodeType nodeType)
    {
        var node = new PlantNode((new GameObject()).GetComponent <Transform>());

        node.SetNode(index, parent.Depth + 1, config.thickness, nodeType, parent);
        node.transform.localRotation = Quaternion.Euler(angle);
        return(node);
    }
Beispiel #2
0
    /// <summary>
    /// 再帰:ノード構成から付属情報を生成する
    /// </summary>
    private void BuildNodes(PlantNode node, Vector3 posRoot)
    {
        float scale = 1.0f - depthScale * node.Depth;

        node.SetNodeJointPoints(posRoot, scale);
        var children = node.children;

        for (int i = children.Count; i-- > 0;)
        {
            BuildNodes(children[i], posRoot);
        }
    }
Beispiel #3
0
    //spawn plant nodes
    public void fillSector(GameObject node, int density)
    {
        Vector3    newPosition  = transform.position + new Vector3(50, 0, 50);
        GameObject plantCluster = Instantiate(node, newPosition, transform.rotation);

        PlantNode scriptReference = plantCluster.GetComponent <PlantNode>();

        for (int i = 0; i < density; i++)
        {
            scriptReference.spawnPlant();
        }
        Destroy(this.gameObject);
    }
Beispiel #4
0
    private void DrawNodes(PlantNode node)
    {
        if (node == null)
        {
            return;
        }
        var nodes = node.children;

        if (nodes != null)
        {
            int len = nodes.Count;
            for (int i = nodes.Count; i-- > 0;)
            {
                DrawNodes(nodes[i]);
            }
        }
        node.DrawGizmos();
    }
Beispiel #5
0
    public PlantNode SetNode(int _index, int _depth, float _thickness, NodeType ntype, PlantNode _parent = null)
    {
        parent     = _parent;
        growLength = 0f;
        index      = _index;
        depth      = _depth;
        nodeType   = ntype;
        thickness  = _thickness;

        if (parent != null)
        {
            transform.SetParent(parent.transform, true);
            transform.localPosition = posZero;
            transform.localScale    = scaleOne;
            parent.children.Add(this);
        }

        transform.name = ntype.ToString() + "_" + Index;
        return(this);
    }
Beispiel #6
0
    public List <PlantNode> GetLeafs(PlantNode node)
    {
        List <PlantNode> result = new List <PlantNode>();

        foreach (PlantNode pn in children)
        {
            if (pn != null)
            {
                result.AddRange(pn.GetLeafs(pn));
            }
        }

        if (result.Count == 0)
        {
            if (nodeType == NodeType.Leaf)
            {
                result.Add(this);
            }
        }

        return(result);
    }
Beispiel #7
0
    private PlantNode[] CreateNodes(char[] dna)
    {
        if (nodeOrigin != null)
        {
            ReleaseBones();
            nodeOrigin = null;
        }
        var nodeList = new List <PlantNode>();

        nodeOrigin = new PlantNode(boneOrigin);
        nodeOrigin.SetNode(0, 0, config.thickness, PlantNode.NodeType.Origin);
        nodeOrigin.transform.name = "origin";
        nodeList.Add(nodeOrigin);

        DecodeNodes(nodeList, nodeOrigin, dna, 0, angleZero);
        depthScale = 0f;
        if (maxDepth > 0)
        {
            depthScale = (1.0f - config.tipScale) / maxDepth;
        }
        BuildNodes(nodeOrigin, boneOrigin.position);

        return(nodeList.ToArray());
    }
Beispiel #8
0
    /// <summary>
    /// 再帰:設定からノードを構成する
    /// </summary>
    private int DecodeNodes(List <PlantNode> nodeList, PlantNode parent, char[] dna, int readPos, Vector3 angle)
    {
        char      prev = ' ', code;
        PlantNode node = parent;

        while (readPos < dna.Length)
        {
            switch (code = dna[readPos++])
            {
            case 'F':
            case 'G':
                var len = Mathf.Lerp(config.distMin, config.distMax, random.Range(0f, 1f));
                node = AddNode(node, nodeList.Count, angle, PlantNode.NodeType.Node);
                nodeList.Add(node);
                node.SetGrowLength(len);
                angle = angleZero;
                break;

            case '[':
                readPos = DecodeNodes(nodeList, node, dna, readPos, angle);
                angle   = angleZero;
                break;

            case ']':
                if (node.transform.childCount <= 0)
                {
                    node     = AddNode(node, nodeList.Count, angleZero, PlantNode.NodeType.Terminal);
                    maxDepth = Mathf.Max(node.Depth, maxDepth);
                    nodeList.Add(node);

                    node = AddNode(node, nodeList.Count, angleZero, PlantNode.NodeType.Leaf);
                    nodeList.Add(node);
                }
                return(readPos);

            case '+': //Left
                angle += Vector3.Lerp(config.angleMin, config.angleMax, random.Range(0f, 1f));
                angle  = MathUtil.AngleRange360(angle);
                break;

            case '-': //Right
                angle += -Vector3.Lerp(config.angleMin, config.angleMax, random.Range(0f, 1f));
                angle  = MathUtil.AngleRange360(angle);
                break;

            default:
                //Log.Output("code:" + dna[readPos - 1]);
                break;
            }
            prev = code;
        }
        if ((prev != '[') && (node.transform.childCount <= 0))
        {
            node     = AddNode(node, nodeList.Count, angleZero, PlantNode.NodeType.Terminal);
            maxDepth = Mathf.Max(node.Depth, maxDepth);
            nodeList.Add(node);

            node = AddNode(node, nodeList.Count, angleZero, PlantNode.NodeType.Leaf);
            nodeList.Add(node);
        }

        return(readPos);
    }