- Home /
Question by
gulle909 · Aug 10, 2019 at 03:29 PM ·
shadermaterialsharedmaterialsharedmesh
Problem with possibly shared meshes
I can't figure out why my problem occurs. It isn't necessarily a problem with shared meshed. I have followed a tutorial by Sebastian Lague on how to make procedually generated planets. (link: https://www.youtube.com/watch?v=OULxvDLojic&t=)
My problem is that I cannot have multiple planets at the same time, with different settings (using ScriptableObject). I've tried making individual materials and shaders for the other planet, but with no luck. When a planet is generated, the other one follows.
Bellow, I've pasted the main script that generates the planet. The sphere is made from a box. I don't really know what to do. Does anyone have a suggestion?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Planet : MonoBehaviour
{
[Range(2, 256)]
public int resolution = 10;
public bool autoUpdate = true;
public enum FaceRenderMask { All, Top, Bottom, Left, Right, Front, Back };
public FaceRenderMask faceRenderMask;
public ShapeSettings shapeSettings;
public ColorSettings colorSettings;
[HideInInspector]
public bool shapeSettingsFoldout;
[HideInInspector]
public bool colorSettingsFoldout;
ShapeGenerator shapeGenerator = new ShapeGenerator();
ColorGenerator colorGenerator = new ColorGenerator();
[SerializeField, HideInInspector]
MeshFilter[] meshFilters;
TerrainFace[] terrainFaces;
void Initialize()
{
shapeGenerator.UpdateSettings(shapeSettings);
colorGenerator.UpdateSettings(colorSettings);
if (meshFilters == null || meshFilters.Length == 0)
{
meshFilters = new MeshFilter[6];
}
terrainFaces = new TerrainFace[6];
Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };
for (int i = 0; i < 6; i++)
{
if (meshFilters[i] == null)
{
GameObject meshObj = new GameObject("mesh");
meshObj.transform.parent = transform;
meshObj.AddComponent<MeshRenderer>();
meshFilters[i] = meshObj.AddComponent<MeshFilter>();
meshFilters[i].sharedMesh = new Mesh();
}
meshFilters[i].GetComponent<MeshRenderer>().sharedMaterial = colorSettings.planetMaterial;
terrainFaces[i] = new TerrainFace(shapeGenerator, meshFilters[i].sharedMesh, resolution, directions[i]);
bool renderFace = faceRenderMask == FaceRenderMask.All || (int)faceRenderMask - 1 == i;
meshFilters[i].gameObject.SetActive(renderFace);
}
}
public void Start() {
GeneratePlanet();
}
public void GeneratePlanet()
{
Initialize();
GenerateMesh();
GenerateColors();
}
public void OnShapeSettingsUpdated()
{
if (autoUpdate)
{
Initialize();
GenerateMesh();
}
}
public void OnColorSettingsUpdated()
{
if (autoUpdate)
{
Initialize();
GenerateColors();
}
}
void GenerateMesh()
{
for (int i = 0; i < 6; i++)
{
if (meshFilters[i].gameObject.activeSelf)
{
terrainFaces[i].ConstructMesh();
}
}
colorGenerator.UpdateElevation(shapeGenerator.elevationMinMax);
}
void GenerateColors()
{
colorGenerator.UpdateColors();
for (int i = 0; i < 6; i++)
{
if (meshFilters[i].gameObject.activeSelf)
{
terrainFaces[i].UpdateUVs(colorGenerator);
}
}
}
}
Comment