public override void OnMouseDown(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
    {
        if (button != 0)
        {
            return;
        }


        int centerX   = (int)data.terrainHitPos.x;
        int centerY   = (int)data.terrainHitPos.y;
        int radiusInt = (int)Mathf.Ceil(toolRadius);
        int diameter  = radiusInt * 2 + 1;
        int lenX      = diameter;
        int lenY      = diameter;

        int gridX = centerX - radiusInt;

        if (gridX < 0)
        {
            lenX += gridX;
            gridX = 0;
        }
        if (gridX + lenX >= data.terrain.terrainData.heightmapWidth)
        {
            lenX = data.terrain.terrainData.heightmapWidth - gridX - 1;
        }
        int gridY = centerY - radiusInt;

        if (gridY < 0)
        {
            lenY += gridY;
            gridY = 0;
        }
        if (gridY + lenY >= data.terrain.terrainData.heightmapHeight)
        {
            lenY = data.terrain.terrainData.heightmapHeight - gridY - 1;
        }

        Vector2 loopCenter = new Vector2(centerX, centerY);



        float[,] heights = data.terrain.terrainData.GetHeights(gridX, gridY, lenX, lenY);

        Vector2 loopPos;

        for (int x = gridX; x < gridX + lenX; ++x)
        {
            loopPos.x = x;
            for (int y = gridY; y < gridY + lenY; ++y)
            {
                loopPos.y = y;
                float dist = Vector2.Distance(loopPos, loopCenter);
                if (dist <= toolRadius)
                {
                    WorldManager.Instance.ClearPlacedObjects(x, y);
                }
            }
        }
    }
Beispiel #2
0
    private void RaiseTerrainCircleLerpBrush(TerrainHitData data, float dt, float radius)
    {
        int centerX   = (int)data.terrainHitPos.x;
        int centerY   = (int)data.terrainHitPos.y;
        int radiusInt = (int)Mathf.Ceil(radius);
        int diameter  = radiusInt * 2 + 1;
        int lenX      = diameter;
        int lenY      = diameter;

        int gridX = centerX - radiusInt;

        if (gridX < 0)
        {
            lenX += gridX;
            gridX = 0;
        }
        if (gridX + lenX >= data.terrain.terrainData.heightmapWidth)
        {
            lenX = data.terrain.terrainData.heightmapWidth - gridX - 1;
        }
        int gridY = centerY - radiusInt;

        if (gridY < 0)
        {
            lenY += gridY;
            gridY = 0;
        }
        if (gridY + lenY >= data.terrain.terrainData.heightmapHeight)
        {
            lenY = data.terrain.terrainData.heightmapHeight - gridY - 1;
        }

        Vector2 loopCenter = new Vector2(centerX, centerY);



        float[,] heights = data.terrain.terrainData.GetHeights(gridX, gridY, lenX, lenY);

        Vector2 loopPos;

        for (int x = gridX; x < gridX + lenX; ++x)
        {
            loopPos.x = x;
            for (int y = gridY; y < gridY + lenY; ++y)
            {
                loopPos.y = y;
                float dist = Vector2.Distance(loopPos, loopCenter);
                if (dist <= radius)
                {
                    heights[y - gridY, x - gridX] += Mathf.Lerp(0, speed * dt, Mathf.InverseLerp(radius, 0, dist));
                }
            }
        }

        data.terrain.terrainData.SetHeights(gridX, gridY, heights);

        //gc.UpdatePlacedObjectPositions(gridX, gridY, lenX, lenY);
        WorldManager.Instance.UpdatePlacedObjectPositions(gridX, gridY, lenX, lenY);
    }
 public override void OnMouseUp(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
 {
     if (button == 0)
     {
         particleSystem.Stop();
         particleSystem.Clear();
     }
 }
 public override void OnMouseHeld(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
 {
     if (button == 0)
     {
         LowerTerrainCircleLerpBrush(data, dt, toolRadius);
         if (!particleSystem.isPlaying)
         {
             particleSystem.Play();
         }
     }
 }
 public override void OnMouseDown(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
 {
     if (button == 0)
     {
         GameObject obj = Instantiate(placablePrefab);
         obj.transform.position    = ghost.transform.position;
         obj.transform.eulerAngles = new Vector3(0, Random.Range(0f, 360f), 0);
         WorldManager.Instance.AddPlacedObject(obj);
         particleSystem.transform.position = ghost.transform.position;
         particleSystem.Play();
     }
 }
    public override void OnMouseHeld(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
    {
        if (button != 0)
        {
            return;
        }


        RaycastHit hit;
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray.origin, ray.direction, out hit, 10000f, LayerMask.GetMask("PlacableObject")))
        {
            WorldManager.Instance.DeletePlacedObject(hit.collider.transform.root.gameObject);
            particleSystem.transform.position = data.physicalHitPoint;
            particleSystem.Play();
        }
    }
Beispiel #7
0
    public override void OnMouseDown(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
    {
        if (button == 0)
        {
            float area     = Mathf.PI * (toolRadius * toolRadius);
            int   objCount = Mathf.RoundToInt(objectDensity * area);

            for (int i = 0; i < objCount; ++i)
            {
                GameObject obj = Instantiate(placablePrefab);
                obj.transform.position    = data.floorHitPos;
                obj.transform.eulerAngles = new Vector3(0, Random.Range(0f, 360f), 0);
                obj.transform.position   += obj.transform.forward * Random.Range(0f, toolRadius);
                obj.transform.position    = new Vector3(obj.transform.position.x,
                                                        data.terrain.SampleHeight(obj.transform.position),
                                                        obj.transform.position.z);
                obj.transform.eulerAngles = new Vector3(0, Random.Range(0f, 360f), 0);
                WorldManager.Instance.AddPlacedObject(obj);
            }
        }
    }
 public virtual void OnMouseScroll(float amt, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
 {
 }
 public virtual void OnMouseUp(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
 {
 }
 public virtual void ToolUpdate(TerrainHitData data, float dt)
 {
 }
    public override void OnMouseHeld(int button, TerrainHitData data, float dt, float toolRadius, GameObject placablePrefab)
    {
        if (button != 0)
        {
            return;
        }


        int centerX   = (int)data.terrainHitPos.x;
        int centerY   = (int)data.terrainHitPos.y;
        int radiusInt = (int)Mathf.Ceil(toolRadius);
        int diameter  = radiusInt * 2 + 1;
        int lenX      = diameter;
        int lenY      = diameter;

        int gridX = centerX - radiusInt;

        if (gridX < 0)
        {
            lenX += gridX;
            gridX = 0;
        }
        if (gridX + lenX >= data.terrain.terrainData.alphamapWidth)
        {
            lenX = data.terrain.terrainData.alphamapWidth - gridX - 1;
        }
        int gridY = centerY - radiusInt;

        if (gridY < 0)
        {
            lenY += gridY;
            gridY = 0;
        }
        if (gridY + lenY >= data.terrain.terrainData.alphamapHeight)
        {
            lenY = data.terrain.terrainData.alphamapHeight - gridY - 1;
        }

        Vector2 loopCenter = new Vector2(centerX, centerY);



        float[,,] splats = data.terrain.terrainData.GetAlphamaps(gridX, gridY, lenX, lenY);
        int layers = data.terrain.terrainData.alphamapLayers;

        if (alphas == null || alphas.Length != layers)
        {
            alphas = new float[layers];
        }

        Vector2 loopPos;

        for (int x = gridX; x < gridX + lenX; ++x)
        {
            loopPos.x = x;
            for (int y = gridY; y < gridY + lenY; ++y)
            {
                loopPos.y = y;
                float dist = Vector2.Distance(loopPos, loopCenter);
                if (dist <= toolRadius)
                {
                    float texAlpha      = splats[y - gridY, x - gridX, texture];
                    float leftoverAlpha = 1f - texAlpha;

                    //Here alphas is used to store ratios
                    alphas[texture] = 0;
                    for (int i = 0; i < layers; ++i)
                    {
                        if (i != texture)
                        {
                            alphas[i] = splats[y - gridY, x - gridX, i] / leftoverAlpha;
                        }
                    }

                    if (dist < toolRadius * solidTexRadPercent)
                    {
                        texAlpha = 1f;
                    }
                    else
                    {
                        texAlpha = Mathf.Lerp(0f, 1f,
                                              Mathf.InverseLerp(toolRadius, toolRadius * solidTexRadPercent, dist));
                    }

                    //texAlpha = Mathf.Lerp(0, 1, Mathf.InverseLerp(toolRadius + (toolRadius * solidTexRadPercent), 0, dist));
                    if (splats[y - gridY, x - gridX, texture] < texAlpha)
                    {
                        leftoverAlpha = 1f - texAlpha;
                        splats[y - gridY, x - gridX, texture] = texAlpha;

                        for (int i = 0; i < layers; ++i)
                        {
                            if (i != texture)
                            {
                                splats[y - gridY, x - gridX, i] = leftoverAlpha * alphas[i];
                            }
                        }
                    }
                }
            }
        }

        data.terrain.terrainData.SetAlphamaps(gridX, gridY, splats);
    }
 public override void ToolUpdate(TerrainHitData data, float dt)
 {
     ghost.transform.position = data.physicalHitPoint;
 }
Beispiel #13
0
    private void Update()
    {
        RaycastHit hit;
        Ray        ray         = Camera.main.ScreenPointToRay(Input.mousePosition);
        bool       impact      = Physics.Raycast(ray.origin, ray.direction, out hit, 10000f, LayerMask.GetMask("TerrainFloor"));
        Vector2    terrainPos  = Vector2.zero;
        float      tHeight     = 0;
        Vector3    floorHitPos = Vector3.zero;

        if (impact)
        {
            floorHitPos = hit.point;
            Vector3 v3pos = hit.point - terrain.transform.position;
            v3pos.x   /= terrain.terrainData.size.x;
            v3pos.y   /= terrain.terrainData.size.y;
            v3pos.z   /= terrain.terrainData.size.z;
            terrainPos = new Vector2(v3pos.x * terrain.terrainData.heightmapWidth,
                                     v3pos.z * terrain.terrainData.heightmapHeight);

            targetCircle.gameObject.SetActive(true);
            tHeight = terrain.SampleHeight(hit.point);
            targetCircle.position    = new Vector3(hit.point.x, tHeight + targetCircleFloatHeight, hit.point.z);
            targetCircle.eulerAngles = Vector3.zero;
            targetCircle.localScale  = new Vector3(toolRadius * 2, 1, toolRadius * 2);
            if (toolRadius != prevRadius)
            {
                prevRadius = toolRadius;
                activeTool.OnBrushRadiusChange(toolRadius);
            }
        }
        else
        {
            targetCircle.gameObject.SetActive(false);
        }



        impact = Physics.Raycast(ray.origin, ray.direction, out hit, 10000f, LayerMask.GetMask("Terrain"));
        Vector3        terrainHitPoint = impact ? hit.point : Vector3.zero;
        TerrainHitData data            = new TerrainHitData();

        data.physicalHitPoint    = terrainHitPoint;
        data.terrain             = terrain;
        data.terrainHitPos       = terrainPos;
        data.heightAtFloorPos    = tHeight;
        data.floorHitPos         = floorHitPos;
        data.floorHitPlusTHeight = new Vector3(floorHitPos.x, terrain.SampleHeight(floorHitPos), floorHitPos.z);
        float dt = Time.deltaTime;

        if (activeTool != null && !EventSystem.current.IsPointerOverGameObject())
        {
            float scrollAmt = Input.GetAxis("Mouse ScrollWheel");
            if (scrollAmt != 0f)
            {
                toolRadius += toolRadius * scrollAmt * toolRadiusScrollMult;
                activeTool.OnMouseScroll(scrollAmt, data, dt, toolRadius, selectedObject);
            }

            #region MouseButtonCalls
            if (Input.GetMouseButtonDown(0))
            {
                activeTool.OnMouseDown(0, data, dt, toolRadius, selectedObject);
            }
            else if (Input.GetMouseButton(0))
            {
                activeTool.OnMouseHeld(0, data, dt, toolRadius, selectedObject);
            }
            else if (Input.GetMouseButtonUp(0))
            {
                activeTool.OnMouseUp(0, data, dt, toolRadius, selectedObject);
            }

            if (Input.GetMouseButtonDown(1))
            {
                activeTool.OnMouseDown(1, data, dt, toolRadius, selectedObject);
            }
            else if (Input.GetMouseButton(1))
            {
                activeTool.OnMouseHeld(1, data, dt, toolRadius, selectedObject);
            }
            else if (Input.GetMouseButtonUp(1))
            {
                activeTool.OnMouseUp(1, data, dt, toolRadius, selectedObject);
            }

            if (Input.GetMouseButtonDown(2))
            {
                activeTool.OnMouseDown(2, data, dt, toolRadius, selectedObject);
            }
            else if (Input.GetMouseButton(2))
            {
                activeTool.OnMouseHeld(2, data, dt, toolRadius, selectedObject);
            }
            else if (Input.GetMouseButtonUp(2))
            {
                activeTool.OnMouseUp(2, data, dt, toolRadius, selectedObject);
            }
            #endregion
        }

        if (Input.GetMouseButton(1))
        {
            Cursor.lockState       = CursorLockMode.Locked;
            transform.eulerAngles += Vector3.up * Input.GetAxis("Mouse X")
                                     * lookSensitivity;                          // * Time.deltaTime;
            xRotOffset           += -Input.GetAxis("Mouse Y") * lookSensitivity; // * Time.deltaTime;
            xRotOffset            = Mathf.Clamp(xRotOffset, -90f, 90f);
            transform.eulerAngles = new Vector3(xRotOffset, transform.eulerAngles.y, transform.eulerAngles.z);
        }
        else
        {
            Cursor.lockState = CursorLockMode.None;
        }

        transform.position += transform.forward * Input.GetAxis("ForeBack") * Time.deltaTime * moveSpeed
                              + transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed
                              + transform.up * Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        activeTool.ToolUpdate(data, dt);
    }