//============================================================================== // Script: TerrainPainter // Author: Victor NP // Date Created: [2023-07-22] // Description: This script controls a terrain painting tool in Unity. It // allows the user to 'paint' onto the terrain using a brush. The size and shape of the // brush is controlled by a decal projector and a 2D texture. // // © [2023] Vairondor. All rights reserved. //============================================================================== using UnityEngine; using UnityEngine.Rendering.HighDefinition; using UnityEngine; using UnityEngine.Rendering.HighDefinition; namespace WorldForge { /// /// Enum for different types of terrain modification. /// public enum TerrainModificationMode { RaiseLower, Flatten, Smooth, Paint } public class TerrainPainter : MonoBehaviour { public Terrain terrain; public Texture2D brushTexture; public DecalProjector brushDecalProjector; // Reference to a DecalProjector component public float brushScale = 1f; // Adjust this to change the size of the brush public TerrainModificationMode mode = TerrainModificationMode.RaiseLower; public int layerIndex = -1; private TerrainData terrainData; // Add a dictionary to store the modified coordinates and their strengths private Dictionary<Vector2Int, float> selectedCoordinates = new Dictionary<Vector2Int, float>(); void Start() { terrain = GameManager.Instance.terrain.current; terrainData = terrain.terrainData; } private void Update() { // Clear the dictionary at the beginning of each frame selectedCoordinates.Clear(); // Check if the user is clicking the left mouse button (button index 0) if (Input.GetMouseButton(0)) { HandleTerrainModification(); } } /// <summary> /// Handles terrain modification when the left mouse button is clicked. /// Raycasts from the camera to the mouse position to determine if the mouse is hitting the terrain. /// If so, it calculates the brush strength and updates the selected coordinates for modification. /// Finally, applies the modifications to the terrain's heightmap based on the selected coordinates. /// </summary> private void HandleTerrainModification() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Terrain terrain = hit.collider.GetComponent<Terrain>(); if (terrain != null) { float brushStrength = 1f; UpdateSelectedCoordinates(hit.point, brushStrength); } } } /// <summary> /// Update the dictionary with the modified coordinates and strengths based on the brush position and strength. /// </summary> /// <param name = "brushPosition">The position of the brush in world space.</param> /// <param name = "brushStrength">The strength of the brush.</param> private void UpdateSelectedCoordinates(Vector3 brushPosition, float brushStrength) { Vector2Int startIndices = GetHeightmapStartIndices(brushPosition); Vector2Int brushAreaSize = GetBrushAreaSize(); startIndices = ClampHeightmapIndices(startIndices, brushAreaSize); // Loop through the brush area and update the dictionary with the modified coordinates and strengths for (int x = startIndices.x; x < startIndices.x + brushAreaSize.x; x++) { for (int y = startIndices.y; y < startIndices.y + brushAreaSize.y; y++) { Vector2Int coordinate = new Vector2Int(x, y); float strength = GetBrushStrength(x - startIndices.x, y - startIndices.y) * brushStrength; if (selectedCoordinates.ContainsKey(coordinate)) { // If the coordinate is already in the dictionary, add the new strength to the existing one selectedCoordinates[coordinate] += strength; } else { // If the coordinate is not in the dictionary, add it with the strength selectedCoordinates.Add(coordinate, strength); } } } } /// <summary> /// Converts a point in world space to a position on the heightmap. /// </summary> /// <param name = "point">A point in world space.</param> /// <returns>The corresponding position on the heightmap.</returns> private Vector2Int GetHeightmapStartIndices(Vector3 point) { // Convert the hit position from world space to terrain-local space Vector3 terrainLocalPos = terrain.transform.InverseTransformPoint(point); // Calculate which part of the heightmap we want to modify int heightmapX = (int)((terrainLocalPos.x / terrainData.size.x * terrainData.heightmapResolution) - (brushTexture.width * brushScale / 2f)); int heightmapZ = (int)((terrainLocalPos.z / terrainData.size.z * terrainData.heightmapResolution) - (brushTexture.height * brushScale / 2f)); return new Vector2Int(heightmapX, heightmapZ); } /// <summary> /// Calculates the size of the brush in heightmap coordinates. /// </summary> /// <returns>The size of the brush in heightmap coordinates.</returns> private Vector2Int GetBrushAreaSize() { // Define the width and height of the area around the hit point we want to modify int width = (int)(brushTexture.width * brushScale); int height = (int)(brushTexture.height * brushScale); return new Vector2Int(width, height); } /// <summary> /// Ensures the start indices don't exceed the heightmap boundaries and handles edge cases. /// </summary> /// <param name = "startIndices">The start indices of the brush on the heightmap.</param> /// <param name = "brushAreaSize">The size of the brush in heightmap coordinates.</param> /// <returns>The clamped start indices.</returns> private Vector2Int ClampHeightmapIndices(Vector2Int startIndices, Vector2Int brushAreaSize) { // Calculate the end indices of the brush area Vector2Int endIndices = startIndices + brushAreaSize; // Ensure we don't exceed the heightmap boundaries startIndices.x = Mathf.Clamp(startIndices.x, 0, terrainData.heightmapResolution - brushAreaSize.x); startIndices.y = Mathf.Clamp(startIndices.y, 0, terrainData.heightmapResolution - brushAreaSize.y); // Check if the end indices go beyond the terrain's edge if (endIndices.x > terrainData.heightmapResolution) { // Calculate the excess amount beyond the terrain's edge int excessX = endIndices.x - terrainData.heightmapResolution; // Adjust the start index to clip the brush startIndices.x -= excessX; } if (endIndices.y > terrainData.heightmapResolution) { // Calculate the excess amount beyond the terrain's edge int excessY = endIndices.y - terrainData.heightmapResolution; // Adjust the start index to clip the brush startIndices.y -= excessY; } return startIndices; } /// <summary> /// Calculates the strength of the brush at a specific point within the brush texture. /// </summary> /// <param name = "x">The x coordinate within the brush texture.</param> /// <param name = "y">The y coordinate within the brush texture.</param> /// <returns>The strength of the brush at the specified point.</returns> private float GetBrushStrength(int x, int y) { // Get the color of the pixel at this point in the brush texture Color pixelColor = brushTexture.GetPixel(x, y); // The strength of the brush is determined by the alpha value of the pixel return pixelColor.a; } } }
Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C#
and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
Data Type | Description | Range | size |
---|---|---|---|
int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
char | to store single characters | - | 2 bytes |
string | to stores text | - | 2 bytes per character |
bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}