Grass chunks

This commit is contained in:
Timo Eberl 2024-09-26 22:41:42 +02:00
parent 5447db5147
commit 2a75b8aa27
Signed by: Timo
SSH Key Fingerprint: SHA256:swVjhbVzKCLQZNtwPqMEmtOUG3FTydzVrpIKpUZYTQw
16 changed files with 7571 additions and 286 deletions

View File

@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

8
Assets/Prefabs.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d22fe26927ae36242bf398620ebf771b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,104 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &3358322397150752750
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7199769165755445272}
- component: {fileID: 8216369470827376369}
- component: {fileID: 5936879447658928899}
- component: {fileID: 6414307754184109852}
m_Layer: 0
m_Name: GrassChunk
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7199769165755445272
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3358322397150752750}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &8216369470827376369
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3358322397150752750}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &5936879447658928899
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3358322397150752750}
m_Mesh: {fileID: 0}
--- !u!114 &6414307754184109852
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3358322397150752750}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 54ba144eac404724ab9ca2a3e30e101f, type: 3}
m_Name:
m_EditorClassIdentifier:
bounds:
m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 0, y: 0, z: 0}
extendedBounds:
m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 0, y: 0, z: 0}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: abb47bb4e9622f7428302dfa4730d722
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because one or more lines are too long

8
Assets/Scripts.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: aa31b79c832c190499d4da4117b13f2f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3b4e1af1f1e786a4d8b2df8c78148a13
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,16 @@
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[CustomEditor(typeof(GrassField))]
public class GrassFieldEditor : Editor {
public override void OnInspectorGUI() {
base.OnInspectorGUI();
if (GUILayout.Button("Generate")) {
((GrassField) target).GenerateGrassField();
// Scene may have been modified
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 40ba82550d5e2e9d4a1a27ed9e99307c
guid: 02ad1dbbec5250f469ad3925cfcead3f
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,12 @@
using System;
using UnityEngine;
[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class GrassChunk : MonoBehaviour {
void OnDrawGizmos() {
Gizmos.color = Color.yellow;
var renderer = GetComponent<MeshRenderer>();
Gizmos.DrawWireCube(renderer.bounds.center, renderer.bounds.size);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 54ba144eac404724ab9ca2a3e30e101f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,93 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
public class GrassField : MonoBehaviour {
public int size = 100;
public int numChunks = 5;
public GrassChunk chunkPrefab;
private struct BladeData {
public Vector3 rootPosition;
public Vector3 tipPosition;
}
public void GenerateGrassField() {
Debug.Log("Generating grass field... ");
var blades = GenerateBlades();
CreateChunks(blades);
Debug.Log("Generating grass field done");
}
private BladeData[] GenerateBlades() {
var blades = new BladeData[size * size];
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++) {
var i = x + y * size;
blades[i].rootPosition = new Vector3(x + 0.5f, 0.0f, y + 0.5f);
blades[i].tipPosition = blades[i].rootPosition + new Vector3(0.0f, 1.0f, 0.0f);
}
return blades;
}
private void CreateChunks(BladeData[] blades) {
GameObject chunks;
// If a child called "Chunks" exists, destroy it -> children are also destroyed
var chunksTransform = gameObject.transform.Find("Chunks");
if (chunksTransform != null) {
// Destroy(obj) does not work in edit mode
DestroyImmediate(chunksTransform.gameObject);
}
// create GameObject "Chunks" as a container for all GrassChunk objects
chunks = new GameObject("Chunks");
chunks.transform.SetParent(this.transform, false);
var chunkSize = ((float)size) / numChunks;
for (int x = 0; x < numChunks; x++)
for (int y = 0; y < numChunks; y++) {
var chunkPos = new Vector3(x, 0.0f, y) * chunkSize;
// bounds in local space of grass field
var bounds = new Bounds(
new Vector3(0.5f * chunkSize, 0.5f, 0.5f * chunkSize),
new Vector3(chunkSize, 1.01f, chunkSize)
);
List<Vector3> rootPositions = new();
List<Vector3> tipPositions = new();
List<int> indices = new();
foreach (var blade in blades) {
var localRootPos = blade.rootPosition - chunkPos;
if (bounds.Contains(localRootPos)) {
rootPositions.Add(localRootPos);
var localTipPos = blade.tipPosition - chunkPos;
tipPositions.Add(localTipPos);
indices.Add(rootPositions.Count - 1);
}
}
var mesh = new Mesh();
// 16 bit (default) supports 65536 vertices, 32 bit supports 4 billion
// mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
mesh.name = "grass_chunk[" + x + "," + y + "]";
mesh.SetVertices(rootPositions);
// do not calculate bounding box - will be set manually
mesh.SetIndices(indices, MeshTopology.Points, 0, false);
// Vector3 UVs are possible
mesh.SetUVs(0, tipPositions);
var chunk = Instantiate(chunkPrefab, chunks.transform);
chunk.name = "Chunk [" + x + ", " + y +"]";
chunk.transform.localPosition = chunkPos;
var meshFilter = chunk.GetComponent<MeshFilter>();
meshFilter.sharedMesh = mesh;
var meshRenderer = chunk.GetComponent<MeshRenderer>();
meshRenderer.localBounds = bounds;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 29a97b88a66e0814b96ef80f3f643e1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences:
- chunkPrefab: {fileID: 6414307754184109852, guid: abb47bb4e9622f7428302dfa4730d722, type: 3}
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -5,6 +5,7 @@
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.6",
"com.unity.toolchain.linux-x86_64": "2.0.9",
"com.unity.toolchain.win-x86_64-linux-x86_64": "2.0.9",
"com.unity.ugui": "1.0.0",
"com.unity.visualscripting": "1.9.4",
"com.unity.modules.ai": "1.0.0",

View File

@ -142,6 +142,16 @@
},
"url": "https://packages.unity.com"
},
"com.unity.toolchain.win-x86_64-linux-x86_64": {
"version": "2.0.9",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.sysroot": "2.0.10",
"com.unity.sysroot.linux-x86_64": "2.0.9"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": {
"version": "1.0.0",
"depth": 0,

View File

@ -0,0 +1,121 @@
{
"templatePinStates": [],
"dependencyTypeInfos": [
{
"userAdded": false,
"type": "UnityEngine.AnimationClip",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEditor.Animations.AnimatorController",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.AnimatorOverrideController",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEditor.Audio.AudioMixerController",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.ComputeShader",
"defaultInstantiationMode": 1
},
{
"userAdded": false,
"type": "UnityEngine.Cubemap",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.GameObject",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEditor.LightingDataAsset",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.LightingSettings",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.Material",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEditor.MonoScript",
"defaultInstantiationMode": 1
},
{
"userAdded": false,
"type": "UnityEngine.PhysicMaterial",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.PhysicsMaterial2D",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.Rendering.PostProcessing.PostProcessProfile",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.Rendering.PostProcessing.PostProcessResources",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.Rendering.VolumeProfile",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEditor.SceneAsset",
"defaultInstantiationMode": 1
},
{
"userAdded": false,
"type": "UnityEngine.Shader",
"defaultInstantiationMode": 1
},
{
"userAdded": false,
"type": "UnityEngine.ShaderVariantCollection",
"defaultInstantiationMode": 1
},
{
"userAdded": false,
"type": "UnityEngine.Texture",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.Texture2D",
"defaultInstantiationMode": 0
},
{
"userAdded": false,
"type": "UnityEngine.Timeline.TimelineAsset",
"defaultInstantiationMode": 0
}
],
"defaultDependencyTypeInfo": {
"userAdded": false,
"type": "<default_scene_template_dependencies>",
"defaultInstantiationMode": 1
},
"newSceneOverride": 0
}