Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Creating a geological model in a unity

Discussion in 'Scripting' started by akamzoldayeva, Apr 13, 2021.

?

How to create geological model with isolines and combine them with gradient?

  1. Maybe I should use Post Processing Stack

    0 vote(s)
    0.0%
  2. Mayve I should cobline isolies and gradient in script

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. akamzoldayeva

    akamzoldayeva

    Joined:
    Jun 6, 2020
    Posts:
    2
    Hi,

    I am trying to create a geological model with isolines in unity. But I have a problem with contours. I created the gradient using a script and a shader. But the problem is creating contour lines. Please tell me how can I create lines and control the height of the lines. Is it possible to combine the gradient and the created contours like that.

    upload_2021-4-13_6-16-42.png
    But this method of creating contours is not suitable because it only works with low-poly meshes.
    upload_2021-4-13_6-17-26.png
    On non-short meshes, not readable on meshes
    upload_2021-4-13_6-19-36.png
     
  2. akamzoldayeva

    akamzoldayeva

    Joined:
    Jun 6, 2020
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System.Linq;

    public class Color_Gradient : MonoBehaviour
    {
    public Gradient gradient = new Gradient();

    [SerializeField] Mesh mesh;
    [SerializeField] Vector3[] vertices;
    [SerializeField] List<Vector3> vertexWorldCoordinates = new List<Vector3>();
    [SerializeField] List<float> yValues = new List<float>();
    public float maxYValue = 0;
    public float minYValue = 0;
    [SerializeField] Color[] colors;

    public Vector3[] Vertices { get => vertices; set => vertices = value; }

    void Start()
    {
    if (GetComponent<MeshFilter>() != null)
    {
    mesh = GetComponent<MeshFilter>().mesh;
    }

    Vertices = mesh.vertices;
    foreach (Vector3 vertex in Vertices)
    {
    Vector3 worldPoint = transform.TransformPoint(vertex);
    vertexWorldCoordinates.Add(worldPoint);
    }
    foreach (Vector3 vertexWorldCoordinate in vertexWorldCoordinates)
    {
    yValues.Add(vertexWorldCoordinate.y);
    }
    maxYValue = yValues.Max();
    minYValue = yValues.Min();
    AssignColorToVertexByByItsRelativeHeight();
    }

    private void OnValidate()
    {
    AssignColorToVertexByByItsRelativeHeight();
    print("hello");
    }
    void AssignColorToVertexByByItsRelativeHeight()
    {
    colors = new Color[yValues.Count];
    for (int i = 0; i < yValues.Count; i++)
    {
    float heightIndexValue = Mathf.InverseLerp(minYValue, maxYValue, yValues);
    colors = gradient.Evaluate(heightIndexValue);
    }
    if (mesh != null)
    {
    mesh.colors = colors;
    }
    }
    }
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Do it in a shader imo.

    btw, please use code tags.
     
    akamzoldayeva likes this.
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    In the Shader Examples at Slices via World Space Position is an effect where pixels are left out based on their world space position. You could try this method by coloring pixels at a certain height.
     
    akamzoldayeva likes this.
  5. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Yes, use a shader definitely. This is very trivial to make in Shader Graph in case you are using URP or HDRP. And you can adapt from that mentioned example if you are writing the shader in code for built-in pipeline.
     
    akamzoldayeva likes this.