예제 #1
0
    void Render(World world, FractalRenderState state, FractalSystemNode node)
    {
        ((TreeVer4_Ultra)node)?.SetColors(trunkBlock, branchBlock, leafSetup.leaf);
        ((TreeVer4_Ultra)node)?.SetLeaf(leafSetup);
        node.Express(world, ref state);

        foreach (FractalSystemNode child in node.child)
        {
            Render(world, state, child);
        }
    }
예제 #2
0
    void RenderNodeRec(FractalRenderState state, FractalSystemNode node)
    {
        node.Express(
            vertices, ref verticesCount,    //Vertices
            indices, ref indicesCount,      //Indices
            normals, ref normalsCount,      //Normals
            uvs, uv2s, uv3s, uv4s,          //TexCoord(uv)
            tangents, ref tmp,              //Tangents
            ref state);

        foreach (FractalSystemNode child in node.child)
        {
            RenderNodeRec(state, child);
        }
    }
예제 #3
0
    public void Generate(BoundsInt bound, World world)
    {
        Vector3 dirc = (bound.max - bound.min);

        //TestTree.FillCyclinder(world, (uint)Blocks.wood, bound.min, bound.max, 4, 2, dirc.normalized, dirc.normalized);

        System.Random random = new System.Random((bound.x << 16) ^ (bound.y << 8) ^ (bound.z));

        startNode          = new TreeVer4_Ultra(random, this.trunkLen, crownWidth, rootScale, endRadiusScale);
        startNode.growRate = startGrowRate;

        startNode.init();

        Descript(startNode, 0);

        FractalRenderState state = new FractalRenderState();

        state.position = new Vector3(bound.center.x, bound.min.y, bound.center.z);
        state.rotation = Quaternion.identity;
        state.scale    = this.scale;

        Render(world, state, startNode);
    }
예제 #4
0
        public override void Express(
            Vector3[] vertices,
            ref int verticesCount,
            List <int[]> indices,
            ref List <int> indicesCount,
            Vector3[] normals,
            ref int normalsCount,
            Vector2[] uvs,
            Vector2[] uv2s,
            Vector2[] uv3s,
            Vector2[] uv4s,
            Vector4[] tangents,
            ref int tangentsCount,
            ref FractalRenderState state
            )
        {
            /*
             *
             * 按圆柱表面坐标系上点的坐标给点标号。圆为横轴,高为纵轴。
             *
             * 顶点(x,y)坐标:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Vertex =        (cos(rad) * radius, y * heightStep, sin(rad) * radius);
             *
             * 顶点(x,y)法线:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Normal =        (cos(rad), 0, sin(rad))
             *
             * 构成整个子结构的面:
             *  for(x = 0; x < circleFragments - 1; x++)
             *      for(y = 0; y < heightFragments - 1; y++)
             * Indices =               ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
             *
             *  不封口。反正也看不见(
             */

            int   vert = 0, x, y;
            float radius = radiusRate * growRate, heightStep = height * growRate / heightFragments;
            float rad;

            #region Vertices & Normals

            for (x = 0; x < circleFragments; x++)
            {
                for (y = 0; y < heightFragments + 1; y++)
                {
                    rad = x * (2f * Mathf.PI / circleFragments);

                    vertices[verticesCount + (x + y * circleFragments)] =
                        state.centerPos + state.rotation * (rotation * (new Vector3(
                                                                            Mathf.Cos(rad) * radius,
                                                                            y * heightStep,
                                                                            Mathf.Sin(rad) * radius)) + centerPos);

                    normals[verticesCount + (x + y * circleFragments)] = state.rotation * rotation * new Vector3(
                        Mathf.Cos(rad),
                        0,
                        Mathf.Sin(rad));

                    vert++;
                }
            }

            #endregion

            #region Indices

            for (x = 0; x < circleFragments - 1; x++)
            {
                for (y = 0; y < heightFragments; y++)
                {
                    //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                    indices[0][indicesCount[0]++] = verticesCount + (x + y * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (y + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + y * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + y * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + (y + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (y + 1) * circleFragments);
                }
            }

            for (y = 0; y < heightFragments; y++)
            {
                x = circleFragments - 1;
                //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                indices[0][indicesCount[0]++] = verticesCount + (x + y * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (y + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + y * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + y * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + (y + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (y + 1) * circleFragments);
            }

            #endregion

            verticesCount += vert;
            normalsCount  += vert;
            //indicesCount已经在上面加过了

            state.centerPos += state.rotation * (rotation * new Vector3(0, 1, 0) * growRate + centerPos);
            state.rotation   = rotation * state.rotation;
        }
예제 #5
0
        public override void Express(
            Vector3[] vertices,
            ref int verticesCount,
            List <int[]> indices,
            ref List <int> indicesCount,
            Vector3[] normals,
            ref int normalsCount,
            Vector2[] uvs,
            Vector2[] uv2s,
            Vector2[] uv3s,
            Vector2[] uv4s,
            Vector4[] tangents,
            ref int tangentsCount,
            ref FractalRenderState state
            )
        {
            /*
             *                          z
             *               y        -+
             *              /|\       /|                    Normals:            +0          +8          +16
             | (7)--------(6)           (0) (-1, -1, -1)        -Z          -X          -Y
             | / |  /     / |           (1) ( 1, -1, -1)        -Z          +X          -Y
             |/    /     /  |           (2) ( 1, -1,  1)        +X          +Z          -Y
             |              (4)--+-----(5)  |           (3) (-1, -1,  1)        +Z          -X          -Y
             |  (3) - - + -(2)          (4) (-1,  1, -1)        -Z          -X          +Y
             | /        |  /            (5) ( 1,  1, -1)        -Z          +X          +Y
             |/         | /             (6) ( 1,  1,  1)        +X          +Z          +Y
             |          ----(0)--------(1)-----> x      (7) (-1,  1,  1)        +Z          -X          +Y
             |              /|
             |             / |
             |            /  |
             |
             |          0154    015 054                             0   5   1   0   4   5
             |          1265    126 165                             9   6   2   9   13  6
             |          2376    237 276                             10  7   3   10  14  7
             |          0473    047 073                             8   15  12  8   11  15
             |          4567    456 467                             20  22  21  20  23  22
             |          0321    032 021                             16  18  19  16  17  18
             */
            float lenth = 0.05f, width = 0.05f, height = 1.0f;//X, Z, Y

            #region Vertices

            vertices[verticesCount + 0] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 1] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 2] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 3] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 4] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 5] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 6] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 7] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);

            #endregion

            #region Normals

            normals[normalsCount + 0] = state.rotation * rotation * new Vector3(-1, 0, -1);
            normals[normalsCount + 1] = state.rotation * rotation * new Vector3(1, 0, -1);
            normals[normalsCount + 2] = state.rotation * rotation * new Vector3(1, 0, 1);
            normals[normalsCount + 3] = state.rotation * rotation * new Vector3(-1, 0, 1);
            normals[normalsCount + 4] = state.rotation * rotation * new Vector3(-1, 0, -1);
            normals[normalsCount + 5] = state.rotation * rotation * new Vector3(1, 0, -1);
            normals[normalsCount + 6] = state.rotation * rotation * new Vector3(1, 0, 1);
            normals[normalsCount + 7] = state.rotation * rotation * new Vector3(-1, 0, 1);

            #endregion

            #region Indices

            int[] tmpIndices = new int[36] {
                verticesCount + 0,
                verticesCount + 5,
                verticesCount + 1,
                verticesCount + 0,
                verticesCount + 4,
                verticesCount + 5,
                verticesCount + 1,
                verticesCount + 6,
                verticesCount + 2,
                verticesCount + 1,
                verticesCount + 5,
                verticesCount + 6,
                verticesCount + 2,
                verticesCount + 7,
                verticesCount + 3,
                verticesCount + 2,
                verticesCount + 6,
                verticesCount + 7,
                verticesCount + 0,
                verticesCount + 7,
                verticesCount + 4,
                verticesCount + 0,
                verticesCount + 3,
                verticesCount + 7,
                verticesCount + 4,
                verticesCount + 6,
                verticesCount + 5,
                verticesCount + 4,
                verticesCount + 7,
                verticesCount + 6,
                verticesCount + 0,
                verticesCount + 2,
                verticesCount + 3,
                verticesCount + 0,
                verticesCount + 1,
                verticesCount + 2
            };
            tmpIndices.CopyTo(indices[0], indicesCount[0]);

            #endregion

            verticesCount   += 8;
            indicesCount[0] += 36;
            normalsCount    += 8;

            state.centerPos += state.rotation * (rotation * new Vector3(0, 1, 0) * growRate + centerPos);
            state.rotation   = rotation * state.rotation;
        }
예제 #6
0
        public override void Express(
            Vector3[] vertices,
            ref int verticesCount,
            List <int[]> indices,
            ref List <int> indicesCount,
            Vector3[] normals,
            ref int normalsCount,
            Vector2[] uvs,
            Vector2[] uv2s,
            Vector2[] uv3s,
            Vector2[] uv4s,
            Vector4[] tangents,
            ref int tangentsCount,
            ref FractalRenderState state
            )
        {
            /*
             *                          z
             *               y        -+
             *              /|\       /|                    Normals:            +0          +8          +16
             | (7)--------(6)           (0) (-1, -1, -1)        -Z          -X          -Y
             | / |  /     / |           (1) ( 1, -1, -1)        -Z          +X          -Y
             |/    /     /  |           (2) ( 1, -1,  1)        +X          +Z          -Y
             |              (4)--+-----(5)  |           (3) (-1, -1,  1)        +Z          -X          -Y
             |  (3) - - + -(2)          (4) (-1,  1, -1)        -Z          -X          +Y
             | /        |  /            (5) ( 1,  1, -1)        -Z          +X          +Y
             |/         | /             (6) ( 1,  1,  1)        +X          +Z          +Y
             |          ----(0)--------(1)-----> x      (7) (-1,  1,  1)        +Z          -X          +Y
             |              /|
             |             / |
             |            /  |
             |
             |          0154    015 054                             0   5   1   0   4   5
             |          1265    126 165                             9   6   2   9   13  6
             |          2376    237 276                             10  7   3   10  14  7
             |          0473    047 073                             8   15  12  8   11  15
             |          4567    456 467                             20  22  21  20  23  22
             |          0321    032 021                             16  18  19  16  17  18
             */
            float lenth = 0.05f, width = 0.05f, height = 1.0f;//X, Z, Y

            //float lenth = 0.05f * (Mathf.Log(growRate, 2.0f) - 4f), width = 0.05f * (Mathf.Log(growRate, 2.0f) - 4f), height = 1.0f;//X, Z, Y

            #region Vertices

            vertices[verticesCount + 0] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 1] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 2] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 3] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 4] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 5] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 6] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 7] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);

            vertices[verticesCount + 0 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 1 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 2 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 3 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 4 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 5 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 6 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 7 + 8] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);

            vertices[verticesCount + 0 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 1 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 2 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 3 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, 0, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 4 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 5 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, -width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 6 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(+lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);
            vertices[verticesCount + 7 + 16] = state.centerPos + state.rotation * (rotation * (new Vector3(-lenth / 2.0f, height, +width / 2.0f) * growRate) + centerPos);

            #endregion

            #region Normals
            //-Z
            normals[normalsCount + 0] = state.rotation * rotation * Vector3.back;
            normals[normalsCount + 1] = state.rotation * rotation * Vector3.back;
            normals[normalsCount + 4] = state.rotation * rotation * Vector3.back;
            normals[normalsCount + 5] = state.rotation * rotation * Vector3.back;

            //+Z
            normals[normalsCount + 10] = state.rotation * rotation * Vector3.forward;
            normals[normalsCount + 3]  = state.rotation * rotation * Vector3.forward;
            normals[normalsCount + 7]  = state.rotation * rotation * Vector3.forward;
            normals[normalsCount + 14] = state.rotation * rotation * Vector3.forward;

            //-X
            normals[normalsCount + 8]  = state.rotation * rotation * Vector3.left;
            normals[normalsCount + 11] = state.rotation * rotation * Vector3.left;
            normals[normalsCount + 12] = state.rotation * rotation * Vector3.left;
            normals[normalsCount + 15] = state.rotation * rotation * Vector3.left;

            //+X
            normals[normalsCount + 9]  = state.rotation * rotation * Vector3.right;
            normals[normalsCount + 2]  = state.rotation * rotation * Vector3.right;
            normals[normalsCount + 13] = state.rotation * rotation * Vector3.right;
            normals[normalsCount + 6]  = state.rotation * rotation * Vector3.right;

            //-Y
            normals[normalsCount + 16] = state.rotation * rotation * Vector3.down;
            normals[normalsCount + 17] = state.rotation * rotation * Vector3.down;
            normals[normalsCount + 18] = state.rotation * rotation * Vector3.down;
            normals[normalsCount + 19] = state.rotation * rotation * Vector3.down;

            //+Y
            normals[normalsCount + 20] = state.rotation * rotation * Vector3.up;
            normals[normalsCount + 21] = state.rotation * rotation * Vector3.up;
            normals[normalsCount + 22] = state.rotation * rotation * Vector3.up;
            normals[normalsCount + 23] = state.rotation * rotation * Vector3.up;

            #endregion

            #region Indices

            int[] tmpIndices = new int[36] {
                verticesCount + 0,
                verticesCount + 5,
                verticesCount + 1,
                verticesCount + 0,
                verticesCount + 4,
                verticesCount + 5,
                verticesCount + 9,
                verticesCount + 6,
                verticesCount + 2,
                verticesCount + 9,
                verticesCount + 13,
                verticesCount + 6,
                verticesCount + 10,
                verticesCount + 7,
                verticesCount + 3,
                verticesCount + 10,
                verticesCount + 14,
                verticesCount + 7,
                verticesCount + 8,
                verticesCount + 15,
                verticesCount + 12,
                verticesCount + 8,
                verticesCount + 11,
                verticesCount + 15,
                verticesCount + 20,
                verticesCount + 22,
                verticesCount + 21,
                verticesCount + 20,
                verticesCount + 23,
                verticesCount + 22,
                verticesCount + 16,
                verticesCount + 18,
                verticesCount + 19,
                verticesCount + 16,
                verticesCount + 17,
                verticesCount + 18
            };
            tmpIndices.CopyTo(indices[0], indicesCount[0]);

            #endregion

            verticesCount   += 24;
            indicesCount[0] += 36;
            normalsCount    += 24;

            state.centerPos += state.rotation * (rotation * new Vector3(0, 1, 0) * growRate + centerPos);
            state.rotation   = rotation * state.rotation;
        }
예제 #7
0
        public override void Express(
            Vector3[] vertices,
            ref int verticesCount,
            List <int[]> indices,
            ref List <int> indicesCount,
            Vector3[] normals,
            ref int normalsCount,
            Vector2[] uvs,
            Vector2[] uv2s,
            Vector2[] uv3s,
            Vector2[] uv4s,
            Vector4[] tangents,
            ref int tangentsCount,
            ref FractalRenderState state
            )
        {
            /*
             * 按圆柱表面坐标系上点的坐标给点标号。圆为横轴,高为纵轴。
             * 顶点(x,y)坐标:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Vertex =        (cos(rad) * radius, y * heightStep, sin(rad) * radius);
             * 顶点(x,y)法线:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Normal =        (cos(rad), 0, sin(rad))
             * 构成整个子结构的面:
             *  for(x = 0; x < circleFragments - 1; x++)
             *      for(y = 0; y < heightFragments - 1; y++)
             * Indices =               ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
             *
             *  不封口。反正也看不见(
             */

            //int vert = 0, x, y;
            //float radius = radiusRate * growRate, heightStep = height * growRate / (spline.Count);
            //float rad;

            //绘制
            int   vert = 0, x, index;
            float rad, radiusReal;

            #region Vertices & Normals

            for (index = 0; index < spline.Count; index++)
            {
                for (x = 0; x < circleFragments; x++)
                {
                    radiusReal = spline[index].radius * Random.Range(0.9f, 1.1f);

                    rad = x * (2f * Mathf.PI / circleFragments);

                    vertices[verticesCount + (x + index * circleFragments)] =
                        state.centerPos + (spline[index].rotationGlobal * new Vector3(
                                               Mathf.Cos(rad) * radiusReal,
                                               0,
                                               Mathf.Sin(rad) * radiusReal)) + spline[index].position;

                    normals[verticesCount + (x + index * circleFragments)] = spline[index].rotationGlobal * new Vector3(
                        Mathf.Cos(rad),
                        0,
                        Mathf.Sin(rad));

                    vert++;
                }
            }

            #endregion

            #region Indices

            for (x = 0; x < circleFragments - 1; x++)
            {
                for (index = 0; index < (spline.Count - 1); index++)
                {
                    //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                    indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (index + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + (index + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (index + 1) * circleFragments);
                }
            }

            //“封口”(因为是圆形封闭截面,需要把最后一个和第一个也连起来)
            for (index = 0; index < (spline.Count - 1); index++)
            {
                //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (index + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + (index + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (index + 1) * circleFragments);
            }

            #endregion

            verticesCount += vert;
            normalsCount  += vert;
            //indicesCount已经在上面加过了

            state.centerPos = Vector3.zero;
            state.rotation  = Quaternion.identity;
        }
예제 #8
0
        public override void Express(
            Vector3[] vertices,
            ref int verticesCount,
            List <int[]> indices,
            ref List <int> indicesCount,
            Vector3[] normals,
            ref int normalsCount,
            Vector2[] uvs,
            Vector2[] uv2s,
            Vector2[] uv3s,
            Vector2[] uv4s,
            Vector4[] tangents,
            ref int tangentsCount,
            ref FractalRenderState state
            )
        {
            /////////////////////
            //树叶的情况
            /////////////////////

            float leaveRadius = 4.0f * startGrowRate / 24.0f;

            if (branchType == BranchType.Leaves)
            {
                //centerPos += new Vector3(0, 2.4f, 0);

                vertices[verticesCount] = rotation * new Vector3(-leaveRadius, -leaveRadius, 0) + centerPos;
                uvs[verticesCount]      = new Vector2(0.5f, 0f);
                uv2s[verticesCount]     = new Vector2(-leaveRadius, -leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
                normals[verticesCount]  = rotation * Vector3.back;
                tangents[verticesCount] = rotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = rotation * new Vector3(-leaveRadius, +leaveRadius, 0) + centerPos;
                uvs[verticesCount]      = new Vector2(0.5f, 0.5f);
                uv2s[verticesCount]     = new Vector2(-leaveRadius, +leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);
                normals[verticesCount]  = rotation * Vector3.back;
                tangents[verticesCount] = rotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = rotation * new Vector3(+leaveRadius, +leaveRadius, 0) + centerPos;
                uvs[verticesCount]      = new Vector2(0f, 0.5f);
                uv2s[verticesCount]     = new Vector2(+leaveRadius, +leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);
                normals[verticesCount]  = rotation * Vector3.back;
                tangents[verticesCount] = rotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = rotation * new Vector3(+leaveRadius, -leaveRadius, 0) + centerPos;
                uvs[verticesCount]      = new Vector2(0f, 0f);
                uv2s[verticesCount]     = new Vector2(+leaveRadius, -leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);
                normals[verticesCount]  = rotation * Vector3.back;
                tangents[verticesCount] = rotation * Vector3.right;
                verticesCount++;

                indices[1][indicesCount[1]++] = verticesCount - 4;
                indices[1][indicesCount[1]++] = verticesCount - 2;
                indices[1][indicesCount[1]++] = verticesCount - 3;
                indices[1][indicesCount[1]++] = verticesCount - 4;
                indices[1][indicesCount[1]++] = verticesCount - 1;
                indices[1][indicesCount[1]++] = verticesCount - 2;

                indices[1][indicesCount[1]++] = verticesCount - 4;
                indices[1][indicesCount[1]++] = verticesCount - 3;
                indices[1][indicesCount[1]++] = verticesCount - 2;
                indices[1][indicesCount[1]++] = verticesCount - 4;
                indices[1][indicesCount[1]++] = verticesCount - 2;
                indices[1][indicesCount[1]++] = verticesCount - 1;
                return;
            }

            /////////////////////
            //树枝的情况
            /////////////////////

            /*
             * 按圆柱表面坐标系上点的坐标给点标号。圆为横轴,高为纵轴。
             * 顶点(x,y)坐标:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Vertex =        (cos(rad) * radius, y * heightStep, sin(rad) * radius);
             * 顶点(x,y)法线:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Normal =        (cos(rad), 0, sin(rad))
             * 构成整个子结构的面:
             *  for(x = 0; x < circleFragments - 1; x++)
             *      for(y = 0; y < heightFragments - 1; y++)
             * Indices =               ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
             *
             *  不封口。反正也看不见(
             */

            //int vert = 0, x, y;
            //float radius = radiusRate * growRate, heightStep = height * growRate / (spline.Count);
            //float rad;

            //绘制
            int   vert = 0, x, index;
            float rad, radiusReal;

            #region Vertices & Normals

            for (index = 0; index < spline.Count; index++)
            {
                for (x = 0; x < circleFragments; x++)
                {
                    radiusReal = spline[index].radius * Random.Range(0.9f, 1.1f);

                    rad = x * (2f * Mathf.PI / circleFragments);

                    vertices[verticesCount + (x + index * circleFragments)] =
                        state.centerPos + (spline[index].rotationGlobal * new Vector3(
                                               Mathf.Cos(rad) * radiusReal,
                                               0,
                                               Mathf.Sin(rad) * radiusReal)) + spline[index].position;

                    normals[verticesCount + (x + index * circleFragments)] = spline[index].rotationGlobal * new Vector3(
                        Mathf.Cos(rad),
                        0,
                        Mathf.Sin(rad));

                    uv4s[verticesCount + (x + index * circleFragments)] = new Vector2(1, 0); //1, 0: 树枝 / 树干

                    vert++;
                }
            }

            #endregion

            #region Indices

            for (x = 0; x < circleFragments - 1; x++)
            {
                for (index = 0; index < (spline.Count - 1); index++)
                {
                    //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                    indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (index + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + (index + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (index + 1) * circleFragments);
                }
            }

            //“封口”(因为是圆形封闭截面,需要把最后一个和第一个也连起来)
            for (index = 0; index < (spline.Count - 1); index++)
            {
                //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (index + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + (index + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (index + 1) * circleFragments);
            }

            #endregion

            verticesCount += vert;
            normalsCount  += vert;
            //indicesCount已经在上面加过了

            state.centerPos = Vector3.zero;
            state.rotation  = Quaternion.identity;
        }
예제 #9
0
        public override void Express(
            Vector3[] vertices,
            ref int verticesCount,
            List <int[]> indices,
            ref List <int> indicesCount,
            Vector3[] normals,
            ref int normalsCount,
            Vector2[] uvs,
            Vector2[] uv2s,
            Vector2[] uv3s,
            Vector2[] uv4s,
            Vector4[] tangents,
            ref int tangentsCount,
            ref FractalRenderState state
            )
        {
            Random.seed = randomSeed;

            /////////////////////
            //树叶的情况
            /////////////////////

            //float leaveRadius = 2.4f * startGrowRate / 24.0f;


            float      leaveRadius = 2.4f * ((growRate > 1.5f) ? (1.0f) : (growRate / 1.5f));
            float      heightDiff  = -(leaveRadius * Mathf.Tan(30f / 180f * Mathf.PI));
            Quaternion lRotation   = Quaternion.FromToRotation(Vector3.up, new Vector3(Random.Range(-0.8f, 0.8f), 1, Random.Range(-0.8f, 0.8f)));

            //Quaternion lRotation = Quaternion.FromToRotation(Vector3.up, new Vector3((randomSeed / (float)13) * 1.6f - 0.8f, 1, (randomSeed / (float)13) * 1.6f - 0.8f));

            if (branchType == BranchType.Leaves)
            {
                if (fractalMode == 3)
                {
                    //枯枝
                    return;
                }
                //centerPos += 1.0f * growRate * (rotation * Vector3.up);
                //Debug.Log(growRate);
                #region Leaves

                vertices[verticesCount] = centerPos;
                uvs[verticesCount]      = new Vector2(0.25f, 0.25f);
                uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
                normals[verticesCount]  = lRotation * Vector3.up;
                tangents[verticesCount] = lRotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = lRotation * new Vector3(-leaveRadius, heightDiff, -leaveRadius) + centerPos;
                uvs[verticesCount]      = new Vector2(0.5f, 0f);
                uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
                normals[verticesCount]  = lRotation * Vector3.up;
                tangents[verticesCount] = lRotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = lRotation * new Vector3(-leaveRadius, heightDiff, +leaveRadius) + centerPos;
                uvs[verticesCount]      = new Vector2(0.5f, 0.5f);
                uv2s[verticesCount]     = new Vector2(-leaveRadius, +leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);
                normals[verticesCount]  = lRotation * Vector3.up;
                tangents[verticesCount] = lRotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = lRotation * new Vector3(+leaveRadius, heightDiff, +leaveRadius) + centerPos;
                uvs[verticesCount]      = new Vector2(0f, 0.5f);
                uv2s[verticesCount]     = new Vector2(+leaveRadius, +leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);
                normals[verticesCount]  = lRotation * Vector3.up;
                tangents[verticesCount] = lRotation * Vector3.right;
                verticesCount++;

                vertices[verticesCount] = lRotation * new Vector3(+leaveRadius, heightDiff, -leaveRadius) + centerPos;
                uvs[verticesCount]      = new Vector2(0f, 0f);
                uv2s[verticesCount]     = new Vector2(+leaveRadius, -leaveRadius);
                uv4s[verticesCount]     = new Vector2(0, 0);
                normals[verticesCount]  = lRotation * Vector3.up;
                tangents[verticesCount] = lRotation * Vector3.right;
                verticesCount++;

                indices[1][indicesCount[1]++] = verticesCount - 5;
                indices[1][indicesCount[1]++] = verticesCount - 4;
                indices[1][indicesCount[1]++] = verticesCount - 3;
                indices[1][indicesCount[1]++] = verticesCount - 5;
                indices[1][indicesCount[1]++] = verticesCount - 3;
                indices[1][indicesCount[1]++] = verticesCount - 2;

                indices[1][indicesCount[1]++] = verticesCount - 5;
                indices[1][indicesCount[1]++] = verticesCount - 2;
                indices[1][indicesCount[1]++] = verticesCount - 1;
                indices[1][indicesCount[1]++] = verticesCount - 5;
                indices[1][indicesCount[1]++] = verticesCount - 1;
                indices[1][indicesCount[1]++] = verticesCount - 4;

                #endregion

                if (fractalMode == 1)
                {
                    //////////////////
                    //开花
                    //////////////////
                    #region Flowers

                    //40%的树叶开花
                    if (Random.Range(0, 10) > 4)
                    {
                        return;
                    }

                    leaveRadius *= 0.3f;
                    Vector2 clr = new Vector2(Random.Range(0, 1), 0);

                    vertices[verticesCount] = rotation * new Vector3(-leaveRadius, -leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(1f, 0f);
                    uv2s[verticesCount]     = new Vector2(-leaveRadius, -leaveRadius);
                    uv3s[verticesCount]     = clr;
                    uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(-leaveRadius, +leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(1f, 1f);
                    uv2s[verticesCount]     = new Vector2(-leaveRadius, +leaveRadius);
                    uv3s[verticesCount]     = clr;
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(+leaveRadius, +leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(0f, 1f);
                    uv2s[verticesCount]     = new Vector2(+leaveRadius, +leaveRadius);
                    uv3s[verticesCount]     = clr;
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(+leaveRadius, -leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(0f, 0f);
                    uv2s[verticesCount]     = new Vector2(+leaveRadius, -leaveRadius);
                    uv3s[verticesCount]     = clr;
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    indices[2][indicesCount[2]++] = verticesCount - 4;
                    indices[2][indicesCount[2]++] = verticesCount - 3;
                    indices[2][indicesCount[2]++] = verticesCount - 2;
                    indices[2][indicesCount[2]++] = verticesCount - 4;
                    indices[2][indicesCount[2]++] = verticesCount - 2;
                    indices[2][indicesCount[2]++] = verticesCount - 1;

                    #endregion
                }

                if (fractalMode == 2)
                {
                    //////////////////
                    //开更多花
                    //////////////////
                    #region More Flowers

                    //每个树叶必定开花

                    leaveRadius *= 0.3f;

                    vertices[verticesCount] = rotation * new Vector3(-leaveRadius, -leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(1f, 0f);
                    uv2s[verticesCount]     = new Vector2(-leaveRadius, -leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(-leaveRadius, +leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(1f, 1f);
                    uv2s[verticesCount]     = new Vector2(-leaveRadius, +leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(+leaveRadius, +leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(0f, 1f);
                    uv2s[verticesCount]     = new Vector2(+leaveRadius, +leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(+leaveRadius, -leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(0f, 0f);
                    uv2s[verticesCount]     = new Vector2(+leaveRadius, -leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    indices[2][indicesCount[2]++] = verticesCount - 4;
                    indices[2][indicesCount[2]++] = verticesCount - 3;
                    indices[2][indicesCount[2]++] = verticesCount - 2;
                    indices[2][indicesCount[2]++] = verticesCount - 4;
                    indices[2][indicesCount[2]++] = verticesCount - 2;
                    indices[2][indicesCount[2]++] = verticesCount - 1;


                    //80%的树叶再开一朵
                    if (Random.Range(0, 10) > 8)
                    {
                        return;
                    }

                    centerPos += Random.insideUnitSphere;

                    vertices[verticesCount] = rotation * new Vector3(-leaveRadius, -leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(1f, 0f);
                    uv2s[verticesCount]     = new Vector2(-leaveRadius, -leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(-leaveRadius, +leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(1f, 1f);
                    uv2s[verticesCount]     = new Vector2(-leaveRadius, +leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(+leaveRadius, +leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(0f, 1f);
                    uv2s[verticesCount]     = new Vector2(+leaveRadius, +leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    vertices[verticesCount] = rotation * new Vector3(+leaveRadius, -leaveRadius, 0) + centerPos;
                    uvs[verticesCount]      = new Vector2(0f, 0f);
                    uv2s[verticesCount]     = new Vector2(+leaveRadius, -leaveRadius);
                    uv4s[verticesCount]     = new Vector2(0, 0);
                    normals[verticesCount]  = rotation * Vector3.back;
                    tangents[verticesCount] = rotation * Vector3.right;
                    verticesCount++;

                    indices[2][indicesCount[2]++] = verticesCount - 4;
                    indices[2][indicesCount[2]++] = verticesCount - 3;
                    indices[2][indicesCount[2]++] = verticesCount - 2;
                    indices[2][indicesCount[2]++] = verticesCount - 4;
                    indices[2][indicesCount[2]++] = verticesCount - 2;
                    indices[2][indicesCount[2]++] = verticesCount - 1;

                    #endregion
                }

                return;
            }

            /////////////////////
            //树枝的情况
            /////////////////////

            /*
             * 按圆柱表面坐标系上点的坐标给点标号。圆为横轴,高为纵轴。
             * 顶点(x,y)坐标:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Vertex =        (cos(rad) * radius, y * heightStep, sin(rad) * radius);
             * 顶点(x,y)法线:
             *  rad = x * (2f * Mathf.PI / circleFragments);
             * Normal =        (cos(rad), 0, sin(rad))
             * 构成整个子结构的面:
             *  for(x = 0; x < circleFragments - 1; x++)
             *      for(y = 0; y < heightFragments - 1; y++)
             * Indices =               ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
             *
             *  不封口。反正也看不见(
             */

            //int vert = 0, x, y;
            //float radius = radiusRate * growRate, heightStep = height * growRate / (spline.Count);
            //float rad;

            //绘制
            int   vert = 0, x, index;
            float rad, radiusReal;

            #region Vertices & Normals

            for (index = 0; index < spline.Count; index++)
            {
                for (x = 0; x < circleFragments; x++)
                {
                    radiusReal = spline[index].radius * Random.Range(0.9f, 1.1f);

                    rad = x * (2f * Mathf.PI / circleFragments);

                    vertices[verticesCount + (x + index * circleFragments)] =
                        state.centerPos + (spline[index].rotationGlobal * new Vector3(
                                               Mathf.Cos(rad) * radiusReal,
                                               0,
                                               Mathf.Sin(rad) * radiusReal)) + spline[index].position;

                    normals[verticesCount + (x + index * circleFragments)] = spline[index].rotationGlobal * new Vector3(
                        Mathf.Cos(rad),
                        0,
                        Mathf.Sin(rad));

                    uvs[verticesCount + (x + index * circleFragments)] = new Vector2(x / (float)(circleFragments - 1), index / (float)(spline.Count - 1));

                    uv2s[verticesCount + (x + index * circleFragments)] = new Vector2(
                        (growRate < standardGreenLine[iterationStep]) ? growRate / standardGreenLine[iterationStep] : 1, 0); //( greenRate, 0 )

                    uv4s[verticesCount + (x + index * circleFragments)] = new Vector2(1, 0);                                 //1, 0: 树枝 / 树干

                    vert++;
                }
            }

            #endregion

            #region Indices

            for (x = 0; x < circleFragments - 1; x++)
            {
                for (index = 0; index < (spline.Count - 1); index++)
                {
                    //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                    indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (index + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + (index + 1) * circleFragments);
                    indices[0][indicesCount[0]++] = verticesCount + (x + 1 + (index + 1) * circleFragments);
                }
            }

            //“封口”(因为是圆形封闭截面,需要把最后一个和第一个也连起来)
            for (index = 0; index < (spline.Count - 1); index++)
            {
                //Indices = ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (index + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + index * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (x + (index + 1) * circleFragments);
                indices[0][indicesCount[0]++] = verticesCount + (0 + (index + 1) * circleFragments);
            }

            #endregion

            verticesCount += vert;
            normalsCount  += vert;
            //indicesCount已经在上面加过了

            #region LeavesOverBranch

            //float radStart = 0f;
            //int count = 2;

            //Vector3 leaveCenter = spline[spline.Count - 1].position;
            //float
            //    lWidth = 0.5f * (growRate > (growLine[iterationStep] * 0.3f) ? 1.0f : 1.0f * (growRate / (growLine[iterationStep] * 0.3f))),
            //    lHeight = 0.8f * (growRate > (growLine[iterationStep] * 0.3f) ? 1.0f : 1.0f * (growRate / (growLine[iterationStep] * 0.3f))),
            //    lThick = 0.2f * (growRate > (growLine[iterationStep] * 0.3f) ? 1.0f : 1.0f * (growRate / (growLine[iterationStep] * 0.3f)));

            //for(int i = 0; i < count; i++)
            //{
            //    radStart += Random.Range(-20f, 20f);

            //    lRotation = Quaternion.FromToRotation(Vector3.up, new Vector3(Random.Range(-0.8f, 0.8f), 1, Random.Range(-0.8f, 0.8f)));
            //    lRotation = Quaternion.AngleAxis(radStart, Vector3.up) * lRotation;

            //    vertices[verticesCount] = lRotation * new Vector3(-lWidth, 0, 0) + leaveCenter;
            //    uvs[verticesCount] = new Vector2(0f, 0.7f);
            //    uv4s[verticesCount] = new Vector2(0, 0);                    //0, 0: 叶子
            //    normals[verticesCount] = lRotation * Vector3.up;
            //    tangents[verticesCount] = lRotation * Vector3.right;
            //    verticesCount++;

            //    vertices[verticesCount] = lRotation * new Vector3(-lWidth, 0, lHeight / 3) + leaveCenter;
            //    uvs[verticesCount] = new Vector2(0f, 0.4f);
            //    uv4s[verticesCount] = new Vector2(0, 0);                    //0, 0: 叶子
            //    normals[verticesCount] = lRotation * Vector3.up;
            //    tangents[verticesCount] = lRotation * Vector3.right;
            //    verticesCount++;

            //    vertices[verticesCount] = lRotation * new Vector3(-lWidth, -lThick, lHeight) + leaveCenter;
            //    uvs[verticesCount] = new Vector2(0f, 0f);
            //    uv4s[verticesCount] = new Vector2(0, 0);
            //    normals[verticesCount] = lRotation * Vector3.up;
            //    tangents[verticesCount] = lRotation * Vector3.right;
            //    verticesCount++;

            //    vertices[verticesCount] = lRotation * new Vector3(+lWidth, -lThick, lHeight) + leaveCenter;
            //    uvs[verticesCount] = new Vector2(1f, 0f);
            //    uv4s[verticesCount] = new Vector2(0, 0);
            //    normals[verticesCount] = lRotation * Vector3.up;
            //    tangents[verticesCount] = lRotation * Vector3.right;
            //    verticesCount++;

            //    vertices[verticesCount] = lRotation * new Vector3(+lWidth, 0, lHeight / 3) + leaveCenter;
            //    uvs[verticesCount] = new Vector2(1f, 0.4f);
            //    uv4s[verticesCount] = new Vector2(0, 0);
            //    normals[verticesCount] = lRotation * Vector3.up;
            //    tangents[verticesCount] = lRotation * Vector3.right;
            //    verticesCount++;

            //    vertices[verticesCount] = lRotation * new Vector3(+lWidth, 0, 0) + leaveCenter;
            //    uvs[verticesCount] = new Vector2(1f, 0.7f);
            //    uv4s[verticesCount] = new Vector2(0, 0);
            //    normals[verticesCount] = lRotation * Vector3.up;
            //    tangents[verticesCount] = lRotation * Vector3.right;
            //    verticesCount++;

            //    indices[3][indicesCount[3]++] = verticesCount - 6;
            //    indices[3][indicesCount[3]++] = verticesCount - 5;
            //    indices[3][indicesCount[3]++] = verticesCount - 2;
            //    indices[3][indicesCount[3]++] = verticesCount - 6;
            //    indices[3][indicesCount[3]++] = verticesCount - 2;
            //    indices[3][indicesCount[3]++] = verticesCount - 1;
            //    indices[3][indicesCount[3]++] = verticesCount - 5;
            //    indices[3][indicesCount[3]++] = verticesCount - 4;
            //    indices[3][indicesCount[3]++] = verticesCount - 3;
            //    indices[3][indicesCount[3]++] = verticesCount - 5;
            //    indices[3][indicesCount[3]++] = verticesCount - 3;
            //    indices[3][indicesCount[3]++] = verticesCount - 2;

            //    indices[3][indicesCount[3]++] = verticesCount - 6;
            //    indices[3][indicesCount[3]++] = verticesCount - 2;
            //    indices[3][indicesCount[3]++] = verticesCount - 5;
            //    indices[3][indicesCount[3]++] = verticesCount - 6;
            //    indices[3][indicesCount[3]++] = verticesCount - 1;
            //    indices[3][indicesCount[3]++] = verticesCount - 2;
            //    indices[3][indicesCount[3]++] = verticesCount - 5;
            //    indices[3][indicesCount[3]++] = verticesCount - 3;
            //    indices[3][indicesCount[3]++] = verticesCount - 4;
            //    indices[3][indicesCount[3]++] = verticesCount - 5;
            //    indices[3][indicesCount[3]++] = verticesCount - 2;
            //    indices[3][indicesCount[3]++] = verticesCount - 3;

            //    radStart += 360f / count;
            //}

            #endregion

            float line = growLine[iterationStep];
            if (iterationStep == 0)
            {
                line *= 0.4f;
            }
            if (iterationStep == 1)
            {
                line *= 0.5f;
            }
            if (iterationStep == 2)
            {
                line *= 10.0f;
            }

            #region oldLeaves

            leaveRadius = 1.0f * (growRate > (line * 0.8f) ? 1.0f : 1.0f * (growRate / (line * 0.8f)));
            heightDiff  = -(leaveRadius * Mathf.Tan(30f / 180f * Mathf.PI));

            lRotation = Quaternion.FromToRotation(Vector3.up, new Vector3(Random.Range(-0.8f, 0.8f), 1, Random.Range(-0.8f, 0.8f)));
            Vector3 leaveCenter = spline[spline.Count - 1].position;

            vertices[verticesCount] = leaveCenter;
            uvs[verticesCount]      = new Vector2(0.25f, 0.25f);
            uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
            normals[verticesCount]  = lRotation * Vector3.up;
            tangents[verticesCount] = lRotation * Vector3.right;
            verticesCount++;

            vertices[verticesCount] = lRotation * new Vector3(-leaveRadius, heightDiff, -leaveRadius) + leaveCenter;
            uvs[verticesCount]      = new Vector2(0.5f, 0f);
            uv4s[verticesCount]     = new Vector2(0, 0);                //0, 0: 叶子
            normals[verticesCount]  = lRotation * Vector3.up;
            tangents[verticesCount] = lRotation * Vector3.right;
            verticesCount++;

            vertices[verticesCount] = lRotation * new Vector3(-leaveRadius, heightDiff, +leaveRadius) + leaveCenter;
            uvs[verticesCount]      = new Vector2(0.5f, 0.5f);
            uv2s[verticesCount]     = new Vector2(-leaveRadius, +leaveRadius);
            uv4s[verticesCount]     = new Vector2(0, 0);
            normals[verticesCount]  = lRotation * Vector3.up;
            tangents[verticesCount] = lRotation * Vector3.right;
            verticesCount++;

            vertices[verticesCount] = lRotation * new Vector3(+leaveRadius, heightDiff, +leaveRadius) + leaveCenter;
            uvs[verticesCount]      = new Vector2(0f, 0.5f);
            uv2s[verticesCount]     = new Vector2(+leaveRadius, +leaveRadius);
            uv4s[verticesCount]     = new Vector2(0, 0);
            normals[verticesCount]  = lRotation * Vector3.up;
            tangents[verticesCount] = lRotation * Vector3.right;
            verticesCount++;

            vertices[verticesCount] = lRotation * new Vector3(+leaveRadius, heightDiff, -leaveRadius) + leaveCenter;
            uvs[verticesCount]      = new Vector2(0f, 0f);
            uv2s[verticesCount]     = new Vector2(+leaveRadius, -leaveRadius);
            uv4s[verticesCount]     = new Vector2(0, 0);
            normals[verticesCount]  = lRotation * Vector3.up;
            tangents[verticesCount] = lRotation * Vector3.right;
            verticesCount++;

            indices[1][indicesCount[1]++] = verticesCount - 5;
            indices[1][indicesCount[1]++] = verticesCount - 4;
            indices[1][indicesCount[1]++] = verticesCount - 3;
            indices[1][indicesCount[1]++] = verticesCount - 5;
            indices[1][indicesCount[1]++] = verticesCount - 3;
            indices[1][indicesCount[1]++] = verticesCount - 2;

            indices[1][indicesCount[1]++] = verticesCount - 5;
            indices[1][indicesCount[1]++] = verticesCount - 2;
            indices[1][indicesCount[1]++] = verticesCount - 1;
            indices[1][indicesCount[1]++] = verticesCount - 5;
            indices[1][indicesCount[1]++] = verticesCount - 1;
            indices[1][indicesCount[1]++] = verticesCount - 4;

            #endregion

            state.centerPos = Vector3.zero;
            state.rotation  = Quaternion.identity;
        }
예제 #10
0
        public override void Express(
            World world,
            ref FractalRenderState state
            )
        {
            /////////////////////
            //树叶的情况
            /////////////////////

            float leaveRadius = 4.0f * startGrowRate / 24.0f;

            uint blk = branchBlock;

            if (branchType == BranchType.Leaves)
            {
                TestTree.FillLeaf(world, leafRenderingSetup, state.position + state.scale * centerPos, state.scale * Mathf.Min(growRate * 0.31f, 3.0f) * MathCore.RandomRange(random, 3.0f, 5.0f));
                //Debug.Log(growRate);
                //Debug.Log("Using green branch as leaves");
            }
            else
            {
                /////////////////////
                //树枝的情况
                /////////////////////

                /*
                 * 按圆柱表面坐标系上点的坐标给点标号。圆为横轴,高为纵轴。
                 * 顶点(x,y)坐标:
                 *  rad = x * (2f * Mathf.PI / circleFragments);
                 * Vertex =        (cos(rad) * radius, y * heightStep, sin(rad) * radius);
                 * 顶点(x,y)法线:
                 *  rad = x * (2f * Mathf.PI / circleFragments);
                 * Normal =        (cos(rad), 0, sin(rad))
                 * 构成整个子结构的面:
                 *  for(x = 0; x < circleFragments - 1; x++)
                 *      for(y = 0; y < heightFragments - 1; y++)
                 * Indices =               ( x, y ) ( x + 1, y + 1 ) ( x + 1, y ); ( x, y ) ( x , y + 1 ) ( x + 1, y + 1 )
                 *
                 *  不封口。反正也看不见(
                 */

                //绘制
                int   vert = 0, x, index;
                float rad, radiusReal;

                for (index = 0; index < (spline.Count - 1); index++)
                {
                    TestTree.FillCyclinder(
                        world,
                        blk,
                        (state.rotation * (state.scale * spline[index].position)) + state.position,
                        (state.rotation * (state.scale * spline[index + 1].position)) + state.position,
                        state.scale * spline[index].radius,
                        state.scale * spline[index + 1].radius,
                        state.rotation * (spline[index].rotationGlobal * Vector3.up),
                        state.rotation * (spline[index + 1].rotationGlobal * Vector3.up)
                        );
                    //break;
                }
            }

            //state.centerPos = Vector3.zero;
            //state.rotation = Quaternion.identity;
        }