Esempio n. 1
0
    private void paintTerrain(byte paintType)
    {
        Destroy2D terrain = (target as Destroy2D);

        if (terrain.brushSize < terrain.getNodeSize())
        {
            terrain.brushSize = terrain.getNodeSize();
        }

        HandleUtility.Repaint();
        Vector3 planePos = drawBrushGizmo();


        if (Event.current.button == 0)
        {
            if (Event.current.type == EventType.mouseDown)
            {
                Undo.RegisterSceneUndo("terrain paint");
            }

            if (Event.current.type == EventType.mouseDrag)
            {
                terrain.paintColorMask(paintType, planePos, terrain.brushSize, terrain.brushIntensity * 0.1f);

                Event.current.Use();
            }
        }
    }
Esempio n. 2
0
    static void createDestroy2DObject()
    {
        GameObject newObj  = new GameObject("Destroy2D Object");
        Destroy2D  terrain = newObj.AddComponent <Destroy2D>();

        terrain.rebuild(50, 50, 5, 5, 0, 0);

        Vector3 planePos = Vector3.zero;

        if (GameObject.Find("SceneCamera") != null)
        {
            Vector2 mousePos = new Vector2(GameObject.Find("SceneCamera").camera.pixelWidth / 2, GameObject.Find("SceneCamera").camera.pixelHeight / 2);

            Ray mouseRay = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos);

            Plane p = new Plane(Vector3.back, 0);

            float distance = 0;

            p.Raycast(mouseRay, out distance);

            mouseRay.direction = new Vector3(mouseRay.direction.x, mouseRay.direction.y, mouseRay.direction.z);

            planePos = mouseRay.origin + mouseRay.direction * distance - new Vector3(0.5f, -0.5f, 0) * terrain.terrainSize;
        }


        newObj.transform.position = planePos;
    }
Esempio n. 3
0
    private void drawTerrain(float val)
    {
        Destroy2D terrain = (target as Destroy2D);

        if (terrain.brushSize < terrain.getNodeSize())
        {
            terrain.brushSize = terrain.getNodeSize();
        }

        HandleUtility.Repaint();
        Vector3 planePos = drawBrushGizmo();

        if (Event.current.button == 0)
        {
            if (Event.current.type == EventType.mouseDown)
            {
                Undo.RegisterSceneUndo("terrain draw");
            }

            if (Event.current.type == EventType.mouseDrag)
            {
                terrain.destroyAtEditor(planePos, terrain.brushSize, terrain.brushIntensity * 0.1f * val);
                Event.current.Use();
            }
        }
    }
Esempio n. 4
0
 void OnCollisionEnter2D(Collision2D coll)
 {
     if (coll.gameObject.name == "Destroy2D Mesh")
     {
         Destroy2D terrain = GameObject.Find("Destroy2D Object").GetComponent <Destroy2D>();
         terrain.destroyAt(transform.position, 3, 0.5f);
         Destroy(this);
     }
 }
Esempio n. 5
0
    void OnEnable()
    {
        Destroy2D terrain = (target as Destroy2D);

        materialEditor      = terrain.material;
        terrainSizeEditor   = terrain.terrainSize;
        resolutionEditor    = terrain.resolution;
        nodesPerMeshEditor  = terrain.nodesPerMesh;
        colliderDepthEditor = terrain.colliderDepth;
        sortingLayerEditor  = terrain.sortingLayer;
        sortingOrderEditor  = terrain.sortingOrder;
    }
Esempio n. 6
0
    public override void OnInspectorGUI()
    {
        Destroy2D terrain = (target as Destroy2D);

        drawBox();

        EditorGUILayout.LabelField("Commands");

        GUILayout.BeginHorizontal();

        if (GUILayout.Button("Clear All"))
        {
            terrain.clearAll();
        }

        GUILayout.EndHorizontal();

        drawBox();

        EditorGUILayout.LabelField("Options");
        EditorGUI.indentLevel++;


        EditorGUIUtility.LookLikeInspector();
        materialEditor     = EditorGUILayout.ObjectField("Material", materialEditor, typeof(Material), false) as Material;
        terrainSizeEditor  = EditorGUILayout.FloatField("Size", terrainSizeEditor);
        resolutionEditor   = EditorGUILayout.IntField("Resolution", resolutionEditor);
        nodesPerMeshEditor = EditorGUILayout.IntField("Nodes Per Mesh", nodesPerMeshEditor);
        //colliderDepthEditor = EditorGUILayout.FloatField("Collider Depth", colliderDepthEditor);
        //sortingLayerEditor	= EditorGUILayout.IntField("Sorting Layer", sortingLayerEditor);
        //sortingOrderEditor	= EditorGUILayout.IntField("Sorting Order", sortingOrderEditor);


        if (terrain.nodesPerMesh < 1)
        {
            terrain.nodesPerMesh = 1;
        }
        if (terrain.nodesPerMesh > terrain.resolution)
        {
            terrain.nodesPerMesh = terrain.resolution;
        }

        EditorGUI.indentLevel--;

        if (materialEditor != terrain.material)
        {
            terrain.material = materialEditor;
            terrain.updateMaterial();
        }

        if ((terrainSizeEditor != terrain.terrainSize) ||
            (resolutionEditor != terrain.resolution) ||
            (nodesPerMeshEditor != terrain.nodesPerMesh) ||
            (colliderDepthEditor != terrain.colliderDepth) ||
            (sortingLayerEditor != terrain.sortingLayer) ||
            (sortingOrderEditor != terrain.sortingOrder))
        {
            if (GUILayout.Button("Commit"))
            {
                terrain.rebuild(terrainSizeEditor, resolutionEditor, nodesPerMeshEditor, colliderDepthEditor, sortingLayerEditor, sortingOrderEditor);
            }
        }


        drawBox();

        EditorGUILayout.LabelField("Paint");
        EditorGUI.indentLevel++;

        terrain.brushIntensity = EditorGUILayout.Slider("Brush Intensity", terrain.brushIntensity, 0.1f, 1.0f);
        terrain.brushSize      = EditorGUILayout.Slider("Brush Size", terrain.brushSize, terrain.getNodeSize(), terrain.terrainSize);


        string[] toolBarOptions = { "Add Terrain", "Remove Terrain", "Paint" };
        toolBarSelection = GUILayout.Toolbar(toolBarSelection, toolBarOptions);

        drawBrush  = (toolBarSelection == 0);
        eraseBrush = (toolBarSelection == 1);

        paint1 = paint2 = paint3 = paint4 = false;

        if (toolBarSelection == 2)
        {
            GUILayout.Label("Paint Options");

            string[] toolBarOptionsPaint = { "Mask 1", "Mask 2", "Mask 3", "Mask 4", "Erase" };
            //toolBarSelectionPaint = GUILayout.SelectionGrid(toolBarSelectionPaint, toolBarOptionsPaint, 5, new GUILayoutOption[]{GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth/5)});

            toolBarSelectionPaint = GUILayout.Toolbar(toolBarSelectionPaint, toolBarOptionsPaint);

            paint1     = (toolBarSelectionPaint == 0);
            paint2     = (toolBarSelectionPaint == 1);
            paint3     = (toolBarSelectionPaint == 2);
            paint4     = (toolBarSelectionPaint == 3);
            paintErase = (toolBarSelectionPaint == 4);
        }
    }
Esempio n. 7
0
    private Vector3 drawBrushGizmo()
    {
        Vector2 mousePos = new Vector2(Event.current.mousePosition.x, GameObject.Find("SceneCamera").camera.pixelHeight - Event.current.mousePosition.y);

        Destroy2D terrain = (target as Destroy2D);

        if (Event.current.type == EventType.layout)
        {
            HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
        }

        Ray mouseRay = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos);        // Camera.current.ScreenPointToRay(Event.current.mousePosition);

        Plane p = new Plane(Vector3.back, terrain.transform.position.z);

        float distance = 0;

        p.Raycast(mouseRay, out distance);

        mouseRay.direction = new Vector3(mouseRay.direction.x, mouseRay.direction.y, mouseRay.direction.z);

        Vector3 planePos = mouseRay.origin + mouseRay.direction * distance;

        if (planePos.x < terrain.transform.position.x)
        {
            planePos.x = terrain.transform.position.x;
        }
        if (planePos.x > (terrain.transform.position.x + terrain.terrainSize))
        {
            planePos.x = (terrain.transform.position.x + terrain.terrainSize);
        }
        if (planePos.y > terrain.transform.position.y)
        {
            planePos.y = terrain.transform.position.y;
        }
        if (planePos.y < (terrain.transform.position.y - terrain.terrainSize))
        {
            planePos.y = (terrain.transform.position.y - terrain.terrainSize);
        }

        for (float c = 0; c < 10; c++)
        {
            Handles.color = Color.Lerp(Color.white, Color.black, c / 10);
            for (int i = 0; i < 20; i++)
            {
                Vector3 pos1 = planePos;
                Vector3 pos2 = planePos;

                float delta  = Mathf.PI * 2 / 20;
                float radius = terrain.brushSize + c / 20;

                pos1.x += Mathf.Cos(i * delta) * radius;
                pos1.y += Mathf.Sin(i * delta) * radius;

                pos2.x += Mathf.Cos((i + 1) * delta) * radius;
                pos2.y += Mathf.Sin((i + 1) * delta) * radius;

                //Debug.DrawLine(pos1, pos2);
                //Gizmos.DrawLine(pos1, pos2);
                Handles.DrawLine(pos1, pos2);
            }
        }


        if ((Event.current.shift) && (Event.current.type.Equals(EventType.scrollWheel)))
        {
            terrain.brushSize += (Event.current.delta.x + Event.current.delta.y) * 0.1f;

            if (terrain.brushSize < terrain.getNodeSize())
            {
                terrain.brushSize = terrain.getNodeSize();
            }

            Event.current.Use();
        }

        return(planePos);
    }
Esempio n. 8
0
    private void buildMesh(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
    {
        if (GetComponent<MeshCollider>() != null) {
            DestroyImmediate(GetComponent<MeshCollider>());
        }

        MeshRenderer mr = gameObject.GetComponent<MeshRenderer>();
        MeshFilter mf = gameObject.GetComponent<MeshFilter>();
        PolygonCollider2D polyCollider = gameObject.GetComponent<PolygonCollider2D>();

        Mesh m;

        if (mr != null) {
            DestroyImmediate(mr);
        }

        if (mf != null) {
            if (mf.sharedMesh != null) {
                mf.sharedMesh.Clear();
            }

            DestroyImmediate(mf);
        }

        if (terrain == null) {
            //GameObject.DestroyImmediate(gameObject);
            return;
        }

        if (polyCollider != null) {
            DestroyImmediate(polyCollider);
            polyCollider = null;
        }

        mr = gameObject.AddComponent<MeshRenderer>();
        mf = gameObject.AddComponent<MeshFilter>();

        m = new Mesh();

        mf.sharedMesh = m;

        int vertexCount = 0;
        int indexCount = 0;
        int uvCount = 0;
        int uv1Count = 0;

        int colliderCount = 0;

        for (int x = 0; x < meshNodeCount; x++) {
            for (int y = 0; y < meshNodeCount; y++) {
                Destroy2DNode node = terrain.getNode(x+meshX, y+meshY);
                if (node != null) {
                    if (node.isFull || node.isGeometry) {
                        vertexCount 	+= node.vertices.Length;
                        indexCount 		+= node.indexes.Length;
                        uvCount 		+= node.uv.Length;
                        uv1Count		+= node.uv1.Length;

                        if (node.isGeometry) {
                            colliderCount++;

                            if (node.hasSecondNormal) {
                                colliderCount++;
                            }
                        }
                    }
                }
            }
        }

        Vector3[] vertices 	= new Vector3[vertexCount];
        Vector3[] normals   = new Vector3[vertexCount];
        Vector4[] tangents 	= new Vector4[vertexCount];
        int[] indexes		= new int[indexCount];
        Vector2[] uv		= new Vector2[uvCount];
        Vector2[] uv1		= new Vector2[uv1Count];

        vertexCount = 0;
        indexCount = 0;
        uvCount = 0;
        uv1Count = 0;

        colliderCount = 0;

        for (int x = 0; x < meshNodeCount; x++) {
            for (int y = 0; y < meshNodeCount; y++) {
                Destroy2DNode node = terrain.getNode(x+meshX, y+meshY);
                if (node != null) {
                    if (node.isFull || node.isGeometry) {
                        Vector3 nodePos = node.nodePosition - gameObject.transform.localPosition;

                        for (int i=0; i<node.vertices.Length; i++) {
                            vertices[vertexCount + i] = node.vertices[i] + nodePos;
                            normals[vertexCount + i] = Vector3.back;
                            tangents[vertexCount+i] = new Vector4(1,0,0,-1);
                        }

                        for (int i=0; i<node.indexes.Length; i++)
                            indexes[indexCount + i] = node.indexes[i]+vertexCount;

                        for (int i=0; i<node.uv.Length; i++)
                            uv[uvCount + i] = node.uv[i];

                        for (int i=0; i<node.uv1.Length; i++)
                            uv1[uv1Count + i] = node.uv1[i];

                        vertexCount 	+= node.vertices.Length;
                        indexCount 		+= node.indexes.Length;
                        uvCount 		+= node.uv.Length;
                        uv1Count		+= node.uv1.Length;

                        if (node.isGeometry) {

                            if (polyCollider == null) {
                                polyCollider = gameObject.AddComponent<PolygonCollider2D>();
                            }

                            node.addPolygonCollider(polyCollider);

                            /*node.addToCollisionMesh(collVertices, collIndexes, collNormals, colliderCount, colliderDepth, nodePos);

                            if (node.hasSecondNormal) {
                                colliderCount++;
                            }

                            colliderCount++;*/
                        }
                    }
                }
            }
        }

        m.vertices 	= vertices;
        m.triangles = indexes;
        m.uv 		= uv;
        m.uv1 		= uv1;
        m.normals 	= normals;
        m.tangents 	= tangents;

        m.Optimize();

        renderer.material = material;

        gameObject.layer = terrain.gameObject.layer;

        isDirty = false;
    }
Esempio n. 9
0
 public void commitEditor(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
 {
     buildMesh(material, colliderDepth, sortingLayer, sortingOrder, terrain);
 }
Esempio n. 10
0
 public void commit(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
 {
     StartCoroutine(buildMeshCoroutine(material, colliderDepth, sortingLayer, sortingOrder, terrain));
 }
Esempio n. 11
0
 private IEnumerator buildMeshCoroutine(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
 {
     yield return new WaitForEndOfFrame();
     buildMesh(material, colliderDepth, sortingLayer, sortingOrder, terrain);
 }
Esempio n. 12
0
    private void buildMesh(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
    {
        if (GetComponent <MeshCollider>() != null)
        {
            DestroyImmediate(GetComponent <MeshCollider>());
        }

        MeshRenderer      mr           = gameObject.GetComponent <MeshRenderer>();
        MeshFilter        mf           = gameObject.GetComponent <MeshFilter>();
        PolygonCollider2D polyCollider = gameObject.GetComponent <PolygonCollider2D>();

        Mesh m;

        if (mr != null)
        {
            DestroyImmediate(mr);
        }

        if (mf != null)
        {
            if (mf.sharedMesh != null)
            {
                mf.sharedMesh.Clear();
            }

            DestroyImmediate(mf);
        }

        if (terrain == null)
        {
            //GameObject.DestroyImmediate(gameObject);
            return;
        }

        if (polyCollider != null)
        {
            DestroyImmediate(polyCollider);
            polyCollider = null;
        }

        mr = gameObject.AddComponent <MeshRenderer>();
        mf = gameObject.AddComponent <MeshFilter>();

        m = new Mesh();

        mf.sharedMesh = m;

        int vertexCount = 0;
        int indexCount  = 0;
        int uvCount     = 0;
        int uv1Count    = 0;

        int colliderCount = 0;

        for (int x = 0; x < meshNodeCount; x++)
        {
            for (int y = 0; y < meshNodeCount; y++)
            {
                Destroy2DNode node = terrain.getNode(x + meshX, y + meshY);
                if (node != null)
                {
                    if (node.isFull || node.isGeometry)
                    {
                        vertexCount += node.vertices.Length;
                        indexCount  += node.indexes.Length;
                        uvCount     += node.uv.Length;
                        uv1Count    += node.uv1.Length;

                        if (node.isGeometry)
                        {
                            colliderCount++;

                            if (node.hasSecondNormal)
                            {
                                colliderCount++;
                            }
                        }
                    }
                }
            }
        }

        Vector3[] vertices = new Vector3[vertexCount];
        Vector3[] normals  = new Vector3[vertexCount];
        Vector4[] tangents = new Vector4[vertexCount];
        int[]     indexes  = new int[indexCount];
        Vector2[] uv       = new Vector2[uvCount];
        Vector2[] uv1      = new Vector2[uv1Count];

        vertexCount = 0;
        indexCount  = 0;
        uvCount     = 0;
        uv1Count    = 0;

        colliderCount = 0;

        for (int x = 0; x < meshNodeCount; x++)
        {
            for (int y = 0; y < meshNodeCount; y++)
            {
                Destroy2DNode node = terrain.getNode(x + meshX, y + meshY);
                if (node != null)
                {
                    if (node.isFull || node.isGeometry)
                    {
                        Vector3 nodePos = node.nodePosition - gameObject.transform.localPosition;

                        for (int i = 0; i < node.vertices.Length; i++)
                        {
                            vertices[vertexCount + i] = node.vertices[i] + nodePos;
                            normals[vertexCount + i]  = Vector3.back;
                            tangents[vertexCount + i] = new Vector4(1, 0, 0, -1);
                        }

                        for (int i = 0; i < node.indexes.Length; i++)
                        {
                            indexes[indexCount + i] = node.indexes[i] + vertexCount;
                        }

                        for (int i = 0; i < node.uv.Length; i++)
                        {
                            uv[uvCount + i] = node.uv[i];
                        }

                        for (int i = 0; i < node.uv1.Length; i++)
                        {
                            uv1[uv1Count + i] = node.uv1[i];
                        }

                        vertexCount += node.vertices.Length;
                        indexCount  += node.indexes.Length;
                        uvCount     += node.uv.Length;
                        uv1Count    += node.uv1.Length;

                        if (node.isGeometry)
                        {
                            if (polyCollider == null)
                            {
                                polyCollider = gameObject.AddComponent <PolygonCollider2D>();
                            }

                            node.addPolygonCollider(polyCollider);

                            /*node.addToCollisionMesh(collVertices, collIndexes, collNormals, colliderCount, colliderDepth, nodePos);
                             *
                             * if (node.hasSecondNormal) {
                             *      colliderCount++;
                             * }
                             *
                             * colliderCount++;*/
                        }
                    }
                }
            }
        }

        m.vertices  = vertices;
        m.triangles = indexes;
        m.uv        = uv;
        m.uv1       = uv1;
        m.normals   = normals;
        m.tangents  = tangents;

        m.Optimize();

        renderer.material = material;

        gameObject.layer = terrain.gameObject.layer;

        isDirty = false;
    }
Esempio n. 13
0
    private IEnumerator buildMeshCoroutine(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
    {
        yield return(new WaitForEndOfFrame());

        buildMesh(material, colliderDepth, sortingLayer, sortingOrder, terrain);
    }
Esempio n. 14
0
 public void commitEditor(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
 {
     buildMesh(material, colliderDepth, sortingLayer, sortingOrder, terrain);
 }
Esempio n. 15
0
 public void commit(Material material, float colliderDepth, int sortingLayer, int sortingOrder, Destroy2D terrain)
 {
     StartCoroutine(buildMeshCoroutine(material, colliderDepth, sortingLayer, sortingOrder, terrain));
 }