Ejemplo n.º 1
0
    /// <summary>
    /// Builds the stem.
    /// </summary>
    /// <param name="meshBuilder">The mesh builder currently being added to.</param>
    /// <param name="currentOffset">Vector3 to store the position at the end of the stem.</param>
    /// <param name="currentRotation">Quaternion to store the rotation at the end of the stem.</param>
    /// <param name="partData">The parameters describing the cylinder to be built.</param>
    private void BuildStem(MeshBuilder meshBuilder, out Vector3 currentOffset, out Quaternion currentRotation, CylinderData partData)
    {
        currentOffset   = Vector3.zero;
        currentRotation = Quaternion.identity;

        //bail if this part has been disabled:
        if (!partData.m_Build)
        {
            return;
        }

        //build a straight stem if partData.m_BendAngle is zero:
        if (partData.m_BendAngle == 0.0f)
        {
            //straight cylinder:
            float heightInc = partData.m_Height / partData.m_HeightSegmentCount;

            for (int i = 0; i <= partData.m_HeightSegmentCount; i++)
            {
                currentOffset = Vector3.up * heightInc * i;

                BuildRing(meshBuilder, partData.m_RadialSegmentCount, currentOffset, partData.m_Radius, (float)i / partData.m_HeightSegmentCount, i > 0);
            }
        }
        else
        {
            //get the bend angle in radians:
            float stemBendRadians = partData.m_BendAngle * Mathf.Deg2Rad;

            //the radius of our bend (vertical) circle:
            float stemBendRadius = partData.m_Height / stemBendRadians;

            //the angle increment per height segment (based on arc length):
            float angleInc = stemBendRadians / partData.m_HeightSegmentCount;

            //calculate a start offset that will place the centre of the first ring (angle 0.0f) on the mesh origin:
            //(x = cos(0.0f) * stemBendRadius, y = sin(0.0f) * stemBendRadius)
            Vector3 startOffset = new Vector3(stemBendRadius, 0.0f, 0.0f);

            //build the rings:
            for (int i = 0; i <= partData.m_HeightSegmentCount; i++)
            {
                //current normalised height value:
                float heightNormalised = (float)i / partData.m_HeightSegmentCount;

                //unit position along the edge of the vertical circle:
                currentOffset   = Vector3.zero;
                currentOffset.x = Mathf.Cos(angleInc * i);
                currentOffset.y = Mathf.Sin(angleInc * i);

                //rotation at that position on the circle:
                float zAngleDegrees = angleInc * i * Mathf.Rad2Deg;
                currentRotation = Quaternion.Euler(0.0f, 0.0f, zAngleDegrees);

                //multiply the unit postion by the bend radius:
                currentOffset *= stemBendRadius;

                //offset the position so that the base ring (at angle zero) centres around zero:
                currentOffset -= startOffset;

                //build the ring:
                BuildRing(meshBuilder, partData.m_RadialSegmentCount, currentOffset, partData.m_Radius, heightNormalised, i > 0, currentRotation);
            }
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Builds the stem.
    /// </summary>
    /// <param name="meshBuilder">The mesh builder currently being added to.</param>
    /// <param name="currentOffset">Vector3 to store the position at the end of the stem.</param>
    /// <param name="currentRotation">Quaternion to store the rotation at the end of the stem.</param>
    /// <param name="partData">The parameters describing the cylinder to be built.</param>
    private void BuildStem(MeshBuilder meshBuilder, out Vector3 currentOffset, out Quaternion currentRotation, CylinderData partData)
    {
        currentOffset = Vector3.zero;
        currentRotation = Quaternion.identity;

        //bail if this part has been disabled:
        if (!partData.m_Build)
            return;

        //build a straight stem if partData.m_BendAngle is zero:
        if (partData.m_BendAngle == 0.0f)
        {
            //straight cylinder:
            float heightInc = partData.m_Height / partData.m_HeightSegmentCount;

            for (int i = 0; i <= partData.m_HeightSegmentCount; i++)
            {
                currentOffset = Vector3.up * heightInc * i;

                BuildRing(meshBuilder, partData.m_RadialSegmentCount, currentOffset, partData.m_Radius, (float)i / partData.m_HeightSegmentCount, i > 0);
            }
        }
        else
        {
            //get the bend angle in radians:
            float stemBendRadians = partData.m_BendAngle * Mathf.Deg2Rad;

            //the radius of our bend (vertical) circle:
            float stemBendRadius = partData.m_Height / stemBendRadians;

            //the angle increment per height segment (based on arc length):
            float angleInc = stemBendRadians / partData.m_HeightSegmentCount;

            //calculate a start offset that will place the centre of the first ring (angle 0.0f) on the mesh origin:
            //(x = cos(0.0f) * stemBendRadius, y = sin(0.0f) * stemBendRadius)
            Vector3 startOffset = new Vector3(stemBendRadius, 0.0f, 0.0f);

            //build the rings:
            for (int i = 0; i <= partData.m_HeightSegmentCount; i++)
            {
                //current normalised height value:
                float heightNormalised = (float)i / partData.m_HeightSegmentCount;

                //unit position along the edge of the vertical circle:
                currentOffset = Vector3.zero;
                currentOffset.x = Mathf.Cos(angleInc * i);
                currentOffset.y = Mathf.Sin(angleInc * i);

                //rotation at that position on the circle:
                float zAngleDegrees = angleInc * i * Mathf.Rad2Deg;
                currentRotation = Quaternion.Euler(0.0f, 0.0f, zAngleDegrees);

                //multiply the unit postion by the bend radius:
                currentOffset *= stemBendRadius;

                //offset the position so that the base ring (at angle zero) centres around zero:
                currentOffset -= startOffset;

                //build the ring:
                BuildRing(meshBuilder, partData.m_RadialSegmentCount, currentOffset, partData.m_Radius, heightNormalised, i > 0, currentRotation);
            }
        }
    }
Ejemplo n.º 3
0
 public MCylinder(ushort id, CylinderData data)
 {
     ID      = id;
     CylData = data;
 }