Наследование: MonoBehaviour
Пример #1
0
    // Start is called before the first frame update
    void Start()
    {
        m_ObjectCollider = GetComponent <PolygonCollider2D>();
        fase             = 4;

        dr = this.gameObject.GetComponent <DrawMesh>();
    }
Пример #2
0
    ////创建弯曲阴影
    ////一条贝塞尔
    //GameObject CreateMesh(Vector3 from, Vector3 to)
    //{
    //    var bezierMesh = new DrawMesh();
    //    var bezier = bezierMesh.DrawBezierMesh(from, to - character.right * (focus - character.position).magnitude / 3, to, 0.15f);

    //    tweenDic.Add(bezier.GetComponentInChildren<MeshRenderer>().material, new TweenInfo("_TilingY"));
    //    CreateTween(bezier.GetComponentInChildren<MeshRenderer>().material);

    //    return bezier;
    //}

    //创建弯曲阴影
    GameObject CreateMesh(Vector3 from, Vector3 to)
    {
        //适配末梢位置在脚下
        to = to + (to - from).normalized * 0.04f * (to - from).magnitude;

        var bezierMesh = new DrawMesh();
        var p11        = from + character.forward * (to - from).magnitude * 1 / 6;
        var p12        = to - character.forward * (to - from).magnitude * 1 / 6;
        var bezier     = bezierMesh.DrawDoubleBezierMesh(from, p11, p12, to, 0.15f);

        //物理忽略
        bezier.layer = 2;

        var mat = bezier.GetComponentInChildren <MeshRenderer>().material;

        CreateTween(mat, "_TilingY", 1f, 1f);

        return(bezier);
    }
Пример #3
0
    public Chunk(float scale, int chunkSize, Material instanceMaterial, Transform viewer)
    {
        directions  = new Vector3[] { new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, -1, 0), new Vector3(0, 0, 1), new Vector3(0, 0, -1) };
        directionsY = new Vector3[] { new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 0, 0), new Vector3(1, 0, 0) };

        Vector3 axisA = new Vector3(directions[0].y, directions[0].z, directions[0].x);
        Vector3 axisB = Vector3.Cross(directions[0], axisA);


        this.scale     = scale;
        this.chunkSize = chunkSize;
        this.viewer    = viewer;

        planetRadius = (chunkSize - 1) * scale / 2;

        viewerPosition = viewer.position;
        chunkFace      = new ChunkFace[6];

        chunkFace[0] = new ChunkFace(null, new Vector3(0, 0, planetRadius), this.scale, (chunkSize - 1), viewerPosition, directions[0], directionsY[0]);
        chunkFace[1] = new ChunkFace(null, new Vector3(0, 0, -planetRadius), this.scale, (chunkSize - 1), viewerPosition, directions[1], directionsY[1]);
        chunkFace[2] = new ChunkFace(null, new Vector3(planetRadius, 0, 0), this.scale, (chunkSize - 1), viewerPosition, directions[2], directionsY[2]);
        chunkFace[3] = new ChunkFace(null, new Vector3(-planetRadius, 0, 0), this.scale, (chunkSize - 1), viewerPosition, directions[3], directionsY[3]);
        chunkFace[4] = new ChunkFace(null, new Vector3(0, planetRadius, 0), this.scale, (chunkSize - 1), viewerPosition, directions[4], directionsY[4]);
        chunkFace[5] = new ChunkFace(null, new Vector3(0, -planetRadius, 0), this.scale, (chunkSize - 1), viewerPosition, directions[5], directionsY[5]);


        meshData = MeshGenerator.GenerateTerrainMesh(chunkSize);
        meshData.CreateMesh();

        drawMesh = new DrawMesh(meshData.GetMesh(), instanceMaterial);

        for (int i = 0; i < test; i++)
        {
            positionsList.AddRange(chunkFace[i].GetPositionList());
            directionList.AddRange(chunkFace[i].GetDirectionList());
        }

        viewedChunkCoord = positionsList.ToArray();
        directionArray   = directionList.ToArray();

        drawMesh.UpdateData(positionsList.Count, viewedChunkCoord, directionArray);
        //  UpdateChunkMesh();
    }
Пример #4
0
    public void Generate()
    {
        if (blockParent != null)
        {
            DestroyImmediate(blockParent);
        }
        if (cornerParent != null)
        {
            DestroyImmediate(cornerParent);
        }

        Vector3[,] verticies  = new Vector3[xSize, zSize];
        GameObject[,] corners = new GameObject[xSize, zSize];

        //make a new empty game object for the parents of the corners and blocks
        blockParent = new GameObject();
        blockParent.transform.name = "Block Parent";

        cornerParent = new GameObject();
        cornerParent.transform.name = "Corner Parent";

        for (int x = 0; x < xSize; x++)
        {
            for (int z = 0; z < zSize; z++)
            {
                //Spawn in the corner prefab
                //Debug.Log(Mathf.PerlinNoise(x * scale + seed, z * scale + seed));
                corners[x, z] = Instantiate(cornerPrefab, new Vector3(gridDistance * x, 0, gridDistance * z), Quaternion.identity, cornerParent.transform);

                verticies[x, z] = new Vector3(gridDistance * x, 0, gridDistance * z);
            }
        }

        //Generate the links between all the corners
        for (int x = 0; x < xSize; x++)
        {
            for (int z = 0; z < zSize; z++)
            {
                Corner currentCorner = corners[x, z].GetComponent <Corner>();

                if (x > 0)
                {
                    //Add the x-1 node
                    currentCorner.connectedCorners.Add(corners[x - 1, z].GetComponent <Corner>());
                }
                if (z > 0)
                {
                    //Add the z-1 node
                    currentCorner.connectedCorners.Add(corners[x, z - 1].GetComponent <Corner>());
                }
                if (z < zSize - 1)
                {
                    //Add the z+1 node
                    currentCorner.connectedCorners.Add(corners[x, z + 1].GetComponent <Corner>());
                }
                if (x < xSize - 1)
                {
                    //Add the x+1 node
                    currentCorner.connectedCorners.Add(corners[x + 1, z].GetComponent <Corner>());
                }
            }
        }



        List <Block> generatedBlocks = new List <Block>();

        //Landscape has been generated
        for (int x = 1; x < xSize; x++)
        {
            for (int z = 1; z < zSize; z++)
            {
                //(a + b + c + d)/4
                Vector3 avgPos = (
                    verticies[x - 1, z - 1] +
                    verticies[x, z - 1] +
                    verticies[x - 1, z] +
                    verticies[x, z]) * 0.25f;


                GameObject block = Instantiate(blockPrefab, avgPos, Quaternion.Euler(0, 0, 180), blockParent.transform);

                Mesh blockMesh = new Mesh();

                blockMesh = DrawMesh.AddNewQuad(blockMesh, verticies[x, z] - avgPos, verticies[x - 1, z] - avgPos, verticies[x, z - 1] - avgPos, verticies[x - 1, z - 1] - avgPos);

                block.GetComponent <MeshFilter>().mesh = blockMesh;
                //block.transform.Rotate(Vector3.forward, 180);

                block.GetComponent <MeshCollider>().sharedMesh = blockMesh;

                //Generate the links to each of the corners the block is attached too
                block.GetComponent <Block>().connectedCorners = new List <Corner>
                {
                    corners[x - 1, z - 1].GetComponent <Corner>(),
                    corners[x, z - 1].GetComponent <Corner>(),
                    corners[x - 1, z].GetComponent <Corner>(),
                    corners[x, z].GetComponent <Corner>()
                };

                //give each corner attached this block to add
                Block thisBlock = block.GetComponent <Block>();
                corners[x - 1, z - 1].GetComponent <Corner>().connectedBlocks.Add(thisBlock);
                corners[x, z - 1].GetComponent <Corner>().connectedBlocks.Add(thisBlock);
                corners[x - 1, z].GetComponent <Corner>().connectedBlocks.Add(thisBlock);
                corners[x, z].GetComponent <Corner>().connectedBlocks.Add(thisBlock);

                generatedBlocks.Add(thisBlock);
            }
        }


        //Generate all the links to the neighboring blocks
        foreach (Block currentBlock in generatedBlocks)
        {
            foreach (Corner corner in currentBlock.connectedCorners)
            {
                foreach (Block connectTo in corner.connectedBlocks)
                {
                    if (connectTo != currentBlock && !currentBlock.connectedBlocks.Contains(connectTo))
                    {
                        currentBlock.connectedBlocks.Add(connectTo);
                    }
                }
            }
        }

        //Remove some random paths
        for (int x = 0; x < xSize; x++)
        {
            for (int z = 0; z < zSize; z++)
            {
                if (Mathf.PerlinNoise(((x + scale) * scale) + seed, ((z + scale) * scale) + seed) > removeChance)
                {
                    //Remove the linked corners
                    foreach (Corner AttachedCorner in corners[x, z].GetComponent <Corner>().connectedCorners)
                    {
                        AttachedCorner.connectedCorners.Remove(corners[x, z].GetComponent <Corner>());
                        //check if the attached corner has no more corners its attached too
                        if (AttachedCorner.connectedCorners.Count == 0)
                        {
                            //Remove the linked blocks from this corner
                            RemoveLinkedBlocks(AttachedCorner);
                            DestroyImmediate(AttachedCorner.gameObject);
                        }
                    }

                    RemoveLinkedBlocks(corners[x, z].GetComponent <Corner>());

                    DestroyImmediate(corners[x, z].gameObject);
                }
            }
        }
    }