コード例 #1
0
        // Given a face, splits it into a uniform grid and extrudes each grid face
        // out and back in again, making an exhaust shape.
        private void AddExhaustToFace(GenMesh genmesh, GenMeshFace faceForExhaust)
        {
            // The more square the face is, the more grid divisions it might have
            var num_cuts = Random.Range(1, (int)(4 - faceForExhaust.AspectRatio));
            var result   = genmesh.Subdivide(faceForExhaust, num_cuts);

            var exhaust_length = Random.Range(0.1f, 0.2f);
            var scaleOuter     = 1f / Random.Range(1.3f, 1.6f);
            var scale_inner    = 1f / Random.Range(1.05f, 1.1f);

            for (var i = 0; i < result.Count(); i++)
            {
                var face = result[i];
                face.MaterialIndex = 2;// Material.hull_dark;

                face = ExtrudeFace(genmesh, face, exhaust_length);
                ScaleFace(genmesh, face, scaleOuter, scaleOuter, scaleOuter);

                face = ExtrudeFace(genmesh, face, 0);
                ScaleFace(genmesh, face, scaleOuter * 0.9f, scaleOuter * 0.9f, scaleOuter * 0.9f);

                var extruded_face_list = new List <GenMeshFace>();
                face = ExtrudeFace(genmesh, face, -exhaust_length * 0.9f, extruded_face_list);

                foreach (var extruded_face in extruded_face_list)
                {
                    extruded_face.MaterialIndex = 3;// Material.exhaust_burn
                }
                ScaleFace(genmesh, face, scale_inner, scale_inner, scale_inner);
            }
        }
コード例 #2
0
        private void AddDiscToFace(GenMesh genmesh, GenMeshFace fac)
        {
            var faceWidth  = fac.Width;
            var faceHeight = fac.Height;
            var depth      = 0.125f * Mathf.Min(faceWidth, faceHeight);

            genmesh.CreateCylinder(
                32,
                depth * 3,
                depth * 4,
                depth,
                GetFaceMatrix(fac, fac.CalculateCenterBounds() + fac.Normal * depth * 0.5f));

            genmesh.CreateCylinder(
                32,
                depth * 1.25f,
                depth * 2.25f,
                0.0f,
                GetFaceMatrix(fac, fac.CalculateCenterBounds() + fac.Normal * depth * 1.05f));

            /*
             * for vert in result['verts']:
             * for face in vert.link_faces:
             * face.material_index = Material.glow_disc
             *
             */
        }
コード例 #3
0
        private void AddGridToFace(GenMesh genmesh, GenMeshFace fac)
        {
            var result     = genmesh.Subdivide(fac, Random.Range(2, 4));
            var gridLength = Random.Range(0.025f, 0.15f);
            var scale      = 0.8f;

            for (var i = 0; i < result.Length; i++)
            {
                var face             = result[i];
                var materialIndex    = Random.value > 0.5f ? 1 /*Material.hull_lights*/ : 4 /*Material.hull*/;
                var extrudedFaceList = new List <GenMeshFace>();

                face = ExtrudeFace(genmesh, face, gridLength, extrudedFaceList);

                foreach (var f in extrudedFaceList)
                {
                    if (Mathf.Abs(face.Normal.z) < 0.707) // # side face
                    {
                        f.MaterialIndex = materialIndex;
                    }
                }

                ScaleFace(genmesh, face, scale, scale, scale);
            }
        }
コード例 #4
0
        public GenMeshFace[] Subdivide(GenMeshFace face, int numberOfCuts)
        {
            var result = face.Subdivide(numberOfCuts);

            this.Faces.Remove(face);
            this.Faces.AddRange(result);

            return(result);
        }
コード例 #5
0
        private void AddSphereToFace(GenMesh genmesh, GenMeshFace fac)
        {
            var sphereSize   = Random.Range(0.4f, 1f) * Mathf.Min(fac.Width, fac.Height);
            var sphereMatrix = GetFaceMatrix(fac, fac.CalculateCenterBounds() - fac.Normal * Random.Range(0f, sphereSize * 0.5f));

            genmesh.CreateIcosphere(3, sphereSize, sphereMatrix);

            /*
             * for vert in result['verts']:
             * for face in vert.link_faces:
             * face.material_index = Material.hull
             */
        }
コード例 #6
0
        public GenMeshFace[] ExtrudeDiscreetFace(GenMeshFace face)
        {
            var faces = face.Extrude();

            foreach (var addedFace in faces)
            {
                this.Faces.Add(addedFace);
            }

            this.Faces.Remove(face);

            return(faces);
        }
コード例 #7
0
        private void AddWeaponsToFace(GenMesh genmesh, GenMeshFace fac)
        {
            var horizontalStep = Random.Range(1, 3);
            var verticalStep   = Random.Range(1, 3);
            var numSegments    = 16;

            var weaponSize  = 0.5f * Mathf.Min(fac.Width / (horizontalStep + 2), fac.Height / (verticalStep + 2));
            var weaponDepth = weaponSize * 0.2f;

            for (var h = 0; h < horizontalStep; h++)
            {
                var top    = Vector3.Lerp(fac.LeftTop.Coordinates, fac.RightTop.Coordinates, (float)(h + 1) / (horizontalStep + 1));
                var bottom = Vector3.Lerp(fac.LeftBottom.Coordinates, fac.RightBottom.Coordinates, (float)(h + 1) / (horizontalStep + 1));

                for (var v = 0; v < verticalStep; v++)
                {
                    var pos        = Vector3.Lerp(top, bottom, (float)(v + 1) / (verticalStep + 1));
                    var faceMatrix = GetFaceMatrix(fac, pos + fac.Normal * weaponDepth * 0.5f) *
                                     Matrix4x4.Rotate(Quaternion.AngleAxis(Random.Range(0, 90), new Vector3(0, 0, 1)));


                    // Turret foundation
                    genmesh.CreateCylinder(numSegments, weaponSize * 0.9f, weaponSize, weaponDepth, faceMatrix);

                    // Turret left guard
                    var leftGuardMat = faceMatrix *
                                       Matrix4x4.Rotate(Quaternion.AngleAxis(90, new Vector3(0, 1, 0))) *
                                       Matrix4x4.Translate(new Vector3(0, 0, weaponSize * 0.6f));
                    genmesh.CreateCylinder(numSegments, weaponSize * 0.6f, weaponSize * 0.5f, weaponDepth * 2, leftGuardMat);

                    // Turret right guard
                    var rightGuardMat = faceMatrix *
                                        Matrix4x4.Rotate(Quaternion.AngleAxis(90, new Vector3(0, 1, 0))) *
                                        Matrix4x4.Translate(new Vector3(0, 0, weaponSize * -0.6f));
                    genmesh.CreateCylinder(numSegments, weaponSize * 0.5f, weaponSize * 0.6f, weaponDepth * 2, rightGuardMat);

                    // Turret housing
                    var upwardAngle    = Random.Range(0, 45);
                    var turretHouseMat = faceMatrix *
                                         Matrix4x4.Rotate(Quaternion.AngleAxis(upwardAngle, new Vector3(1, 0, 0))) *
                                         Matrix4x4.Translate(new Vector3(0, weaponSize * -0.4f, 0));
                    genmesh.CreateCylinder(8, weaponSize * 0.4f, weaponSize * 0.4f, weaponDepth * 5, turretHouseMat);

                    // Turret barrels L + R
                    genmesh.CreateCylinder(8, weaponSize * 0.1f, weaponSize * 0.1f, weaponDepth * 6, turretHouseMat *
                                           Matrix4x4.Translate(new Vector3(weaponSize * 0.2f, 0, -weaponSize)));
                    genmesh.CreateCylinder(8, weaponSize * 0.1f, weaponSize * 0.1f, weaponDepth * 6, turretHouseMat *
                                           Matrix4x4.Translate(new Vector3(weaponSize * -0.2f, 0, -weaponSize)));
                }
            }
        }
コード例 #8
0
        private GenMeshFace ExtrudeFace(GenMesh genmesh, GenMeshFace face, float distance, List <GenMeshFace> extrudedFaces = null)
        {
            var newFaces = genmesh.ExtrudeDiscreetFace(face);

            if (extrudedFaces != null)
            {
                extrudedFaces.AddRange(newFaces);
            }

            var newFace = newFaces[0];

            genmesh.Translate(newFace.Normal * distance, newFace.Vertices);

            return(newFace);
        }
コード例 #9
0
        private void AddSurfaceAntennaToFace(GenMesh genmesh, GenMeshFace fac)
        {
            var horizontalStep = Random.Range(4, 10);
            var verticalStep   = Random.Range(4, 10);

            for (var h = 0; h < horizontalStep; h++)
            {
                var top    = Vector3.Lerp(fac.LeftTop.Coordinates, fac.RightTop.Coordinates, ((float)h + 1) / (horizontalStep + 1));
                var bottom = Vector3.Lerp(fac.LeftBottom.Coordinates, fac.RightBottom.Coordinates, ((float)h + 1) / (horizontalStep + 1));

                for (var v = 0; v < verticalStep; v++)
                {
                    if (Random.value > 0.9f)
                    {
                        var pos           = Vector3.Lerp(top, bottom, ((float)v + 1) / (verticalStep + 1));
                        var faceSize      = Mathf.Sqrt(fac.Area());
                        var depth         = Random.Range(0.1f, 1.5f) * faceSize;
                        var depthShort    = depth * Random.Range(0.02f, 0.15f);
                        var baseDiameter  = Random.Range(0.005f, 0.05f);
                        var materialIndex = Random.value > 0.5f ? 0 /*Material.hull*/ : 1;/*Material.hull_dark*/

                        // Spire
                        var numSegments = Random.Range(3, 6);
                        genmesh.CreateCylinder(numSegments, 0, baseDiameter, depth, GetFaceMatrix(fac, pos + fac.Normal * depth * 0.5f));

                        //for vert in result['verts']:
                        //    for vert_face in vert.link_faces:
                        //        vert_face.material_index = material_index
                        //    }

                        // Base
                        genmesh.CreateCylinder(
                            numSegments,
                            baseDiameter * Random.Range(1f, 1.5f),
                            baseDiameter * Random.Range(1.5f, 2f),
                            depthShort,
                            GetFaceMatrix(fac, pos + fac.Normal * depthShort * 0.45f));

                        //    for vert in result['verts']:
                        //for vert_face in vert.link_faces:
                        //    vert_face.material_index = material_index
                    }
                }
            }
        }
コード例 #10
0
        private GenMeshFace RibbedExtrudeFace(GenMesh genmesh, GenMeshFace face, float distance, int numberOfRibs = 3, float ribScale = 0.9f)
        {
            var distancePerRib = distance / numberOfRibs;
            var newFace        = face;

            for (var i = 0; i < numberOfRibs; i++)
            {
                newFace = ExtrudeFace(genmesh, newFace, distancePerRib * 0.25f);
                newFace = ExtrudeFace(genmesh, newFace, 0.0f);
                ScaleFace(genmesh, newFace, ribScale, ribScale, ribScale);
                newFace = ExtrudeFace(genmesh, newFace, distancePerRib * 0.5f);
                newFace = ExtrudeFace(genmesh, newFace, 0.0f);
                ScaleFace(genmesh, newFace, 1 / ribScale, 1 / ribScale, 1 / ribScale);
                newFace = ExtrudeFace(genmesh, newFace, distancePerRib * 0.25f);
            }

            return(newFace);
        }
コード例 #11
0
        private Matrix4x4 GetFaceMatrix(GenMeshFace face, Vector3?position = null)
        {
            var xAxis = (face.RightTop.Coordinates - face.LeftTop.Coordinates).normalized;
            var zAxis = -face.Normal;
            var yAxis = Vector3.Cross(zAxis, xAxis);

            if (!position.HasValue)
            {
                position = face.CalculateCenterBounds();
            }

            //return Matrix4x4.Translate(position.Value);

            // Construct a 4x4 matrix from axes + position:
            // http://i.stack.imgur.com/3TnQP.png

            var mat = new Matrix4x4();

            mat[0, 0] = xAxis.x;
            mat[1, 0] = xAxis.y;
            mat[2, 0] = xAxis.z;
            mat[3, 0] = 0;
            mat[0, 1] = yAxis.x;
            mat[1, 1] = yAxis.y;
            mat[2, 1] = yAxis.z;
            mat[3, 1] = 0;
            mat[0, 2] = zAxis.x;
            mat[1, 2] = zAxis.y;
            mat[2, 2] = zAxis.z;
            mat[3, 2] = 0;
            mat[0, 3] = position.Value.x;
            mat[1, 3] = position.Value.y;
            mat[2, 3] = position.Value.z;
            mat[3, 3] = 1;

            return(mat);
        }
コード例 #12
0
        private void AddCylindersToFace(GenMesh genmesh, GenMeshFace fac)
        {
            var horizontalStep   = Random.Range(1, 3);
            var verticalStep     = Random.Range(1, 3);
            var numberOfSegments = Random.Range(6, 12);
            var faceWidth        = fac.Width;
            var faceHeight       = fac.Height;
            var cylinderDepth    = 1.3f * Mathf.Min(faceWidth / (horizontalStep + 2), faceHeight / (verticalStep + 2));
            var cylinderSize     = cylinderDepth * 0.5f;

            for (var h = 0; h < horizontalStep; h++)
            {
                var top    = Vector3.Lerp(fac.LeftTop.Coordinates, fac.RightTop.Coordinates, ((float)h + 1) / (horizontalStep + 1));
                var bottom = Vector3.Lerp(fac.LeftBottom.Coordinates, fac.RightBottom.Coordinates, ((float)h + 1) / (horizontalStep + 1));

                for (var v = 0; v < verticalStep; v++)
                {
                    var pos            = Vector3.Lerp(top, bottom, ((float)v + 1) / (verticalStep + 1));
                    var cylinderMatrix = GetFaceMatrix(fac, pos) * Matrix4x4.Rotate(Quaternion.AngleAxis(90, new Vector3(0, 1, 0)));

                    genmesh.CreateCylinder(numberOfSegments, cylinderSize, cylinderSize, cylinderDepth, cylinderMatrix);
                }
            }
        }
コード例 #13
0
 private void ScaleFace(GenMesh genmesh, GenMeshFace face, float sx, float sy, float sz)
 {
     genmesh.Scale(new Vector3(sx, sy, sz), GetFaceMatrix(face).inverse, face.Vertices);
 }