コード例 #1
0
    public void Spawn(SgtTerrain terrain, SgtTerrainLevel level, SgtVector3D localPoint)
    {
        if (OnSpawn != null)
        {
            OnSpawn();
        }

        transform.SetParent(level.transform, false);

        // Snap to surface
        localPoint = terrain.GetLocalPoint(localPoint);

        // Rotate up
        var up = Quaternion.Euler(0.0f, Random.Range(0.0f, 360.0f), 0.0f) * Vector3.up;

        // Spawn on surface
        transform.localPosition = (Vector3)localPoint;
        transform.localRotation = Quaternion.FromToRotation(up, terrain.transform.TransformDirection(transform.localPosition));
        transform.localScale    = Prefab.transform.localScale * Random.Range(ScaleMin, ScaleMax);
        //transform.rotation   = Quaternion.FromToRotation(up, terrain.transform.TransformDirection(localPosition));

        if (AlignToNormal != 0.0f)
        {
            //var worldRight   = transform.right   * AlignToNormal;
            //var worldForward = transform.forward * AlignToNormal;
            //var worldNormal  = terrain.GetLocalNormal(localPoint, worldRight, worldForward);

            //transform.rotation = Quaternion.FromToRotation(up, worldNormal);
        }
    }
コード例 #2
0
ファイル: SgtTerrainPlane.cs プロジェクト: BackToGround/XGame
    private void Build(SgtTerrain terrain, Vector3 bestPoint)
    {
        if (meshCollider == null)
        {
            var gameObject = new GameObject("Plane");
#if UNITY_EDITOR
            gameObject.hideFlags = HideFlags.DontSave;
#endif
            meshCollider = gameObject.AddComponent <MeshCollider>();
        }

        if (mesh == null)
        {
            mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh();

#if UNITY_EDITOR
            mesh.hideFlags = HideFlags.DontSave;
#endif
            mesh.name = "Plane";
        }

        var sideE        = Detail;
        var sideP        = Detail + 1;
        var vertexCount  = sideP * sideP;
        var indexCount   = sideE * sideE * 6;
        var rotation     = Quaternion.Inverse(terrain.transform.rotation) * Quaternion.LookRotation(bestPoint);
        var distance     = bestPoint.magnitude;
        var uniformScale = SgtHelper.UniformScale(terrain.transform.lossyScale);
        var size         = Size * SgtHelper.Reciprocal(uniformScale);
        var step         = (size * 2.0f) / Detail;

        if (positions == null || positions.Length != vertexCount)
        {
            positions = new Vector3[vertexCount];
        }

        for (var y = 0; y <= Detail; y++)
        {
            for (var x = 0; x <= Detail; x++)
            {
                var index = x + y * sideP;
                var point = rotation * new Vector3(x * step - size, y * step - size, distance);

                positions[index] = terrain.GetLocalPoint(point);
            }
        }

        // Regen indices?
        if (indices == null || indices.Length != indexCount)
        {
            indices = new int[indexCount];

            for (var y = 0; y < sideE; y++)
            {
                for (var x = 0; x < sideE; x++)
                {
                    var index  = (x + y * sideE) * 6;
                    var vertex = x + y * sideP;

                    indices[index + 0] = vertex;
                    indices[index + 1] = vertex + 1;
                    indices[index + 2] = vertex + sideP;
                    indices[index + 3] = vertex + sideP + 1;
                    indices[index + 4] = vertex + sideP;
                    indices[index + 5] = vertex + 1;
                }
            }

            mesh.Clear();
        }

        mesh.vertices  = positions;
        mesh.triangles = indices;

        meshCollider.sharedMesh = mesh;

        meshCollider.transform.SetParent(terrain.transform, false);
    }