public void Init(Buffer <bool> map, bool updateMeshEachFrame, bool createPhysics)
    {
        this.updateMeshEachFrame = updateMeshEachFrame;
        blinkSpeed = Random.Range(1.0f, 2.5f);

        mutableMesh = new MutableMesh();

        SquareGrid grid = new SquareGrid(
            map.width * ShapeAnalizer.scale,
            map.height * ShapeAnalizer.scale,
            mutableMesh
            );

        shapeAnalizer = GenerateMap(map, grid);

        var meshFilter = gameObject.GetComponent <MeshFilter> ();

        Vector3[] vertices      = mutableMesh.GetVertexes().ToArray();
        float     tileAmount    = 0.5f;
        float     textureWidth  = ShapeAnalizer.scale * map.width;
        float     textureHeight = ShapeAnalizer.scale * map.height;

        Vector2[] uvs = new Vector2[vertices.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            float percentX = Mathf.InverseLerp(0, textureWidth, vertices[i].x) * tileAmount;
            float percentY = Mathf.InverseLerp(0, textureHeight, vertices[i].y) * tileAmount;
            uvs[i] = new Vector2(percentX, percentY);
        }
        var mesh = new Mesh()
        {
            vertices  = vertices,
            triangles = mutableMesh.GetTriangles().ToArray(),
            uv        = uvs,
            uv2       = uvs,
            colors    = mutableMesh.GetColors().ToArray(),
        };

        mesh.RecalculateTangents();
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        meshFilter.sharedMesh = mesh;

        GetComponent <Renderer> ().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);

        if (createPhysics)
        {
            CreatePhysics(map);
        }
        transform.localScale = new Vector3(0.25f, 0.25f, 0.25f);

        var pupil = Instantiate(eyePrefab, shapeAnalizer.eye.center, Quaternion.identity, transform);

        pupil.transform.localScale = new Vector3(2.0f, 2.0f, 1.0f);

        shapeAnalizer.eye.SetPupil(pupil, 0.12f);
        shapeAnalizer.eye.MovePupil(Vector3.zero);

        StartCoroutine(BlinkPeriodically());
    }
Beispiel #2
0
    // Use this for initialization
    void Start()
    {
        var map = ShapeGeneration.CreateRandomShape();

        mutableMesh = new MutableMesh();

        SquareGrid grid = new SquareGrid(
            map.width * ShapeAnalizer.scale,
            map.height * ShapeAnalizer.scale,
            mutableMesh
            );

        shapeAnalizer = new ShapeAnalizer(map, grid);

        var meshFilter = gameObject.GetComponent <MeshFilter> ();

        Vector3[] vertices      = mutableMesh.GetVertexes().ToArray();
        float     tileAmount    = 0.5f;
        float     textureWidth  = ShapeAnalizer.scale * map.width;
        float     textureHeight = ShapeAnalizer.scale * map.height;

        Vector2[] uvs = new Vector2[vertices.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            float percentX = Mathf.InverseLerp(0, textureWidth, vertices[i].x) * tileAmount;
            float percentY = Mathf.InverseLerp(0, textureHeight, vertices[i].y) * tileAmount;
            uvs[i] = new Vector2(percentX, percentY);
        }
        var mesh = new Mesh()
        {
            vertices  = vertices,
            triangles = mutableMesh.GetTriangles().ToArray(),
            uv        = uvs,
            uv2       = uvs,
        };

        mesh.RecalculateTangents();
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        meshFilter.sharedMesh = mesh;

        var renderer = gameObject.GetComponent <MeshRenderer> ();

        renderer.material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);

        var pupil = Instantiate(eyePrefab, shapeAnalizer.eye.center, Quaternion.identity, transform);

        pupil.transform.localScale = new Vector3(2.0f, 2.0f, 0);

        shapeAnalizer.eye.SetPupil(pupil, 0.12f);
        shapeAnalizer.eye.MovePupil(Vector3.zero);
    }