This commit is contained in:
Timo Eberl 2024-09-28 03:53:49 +02:00
parent 253bdbf854
commit d4397c6532
Signed by: Timo
SSH Key Fingerprint: SHA256:swVjhbVzKCLQZNtwPqMEmtOUG3FTydzVrpIKpUZYTQw
11 changed files with 33793 additions and 21583 deletions

View File

@ -7,11 +7,12 @@ Material:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Grass
m_Name: GrassLOD0
m_Shader: {fileID: 4800000, guid: eebe2969663fc40449d6052c661a6c2e, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_ValidKeywords:
- _LOD_LOD_0
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
@ -51,10 +52,14 @@ Material:
- _BladeWidth: 0.05
- _ExtraSawnOnCutGras: 6
- _FieldSize: 5
- _LOD: 0
- _Metallic: 0
- _NumSegments_LOD_0: 11
- _NumSegments_LOD_1: 5
- _NumSegments_LOD_2: 1
- _Roughness: 0.3
- _SSSStrength: 1
- _SegmentsMinusOne: 10
- _SegmentsMinusOne: 11
- _Translucency: 1
m_Colors:
- _BoxSize: {r: 0, g: 0, b: 0, a: 0}

View File

@ -0,0 +1,72 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: GrassLOD1
m_Shader: {fileID: 4800000, guid: eebe2969663fc40449d6052c661a6c2e, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords:
- _LOD_LOD_1
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BladeWidthTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _FrabVarianz:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _HeightMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MaskMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _WindTextur:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AOStrength: 1
- _BendStrength: 1
- _BladeBow: 0.3
- _BladeWidth: 0.05
- _ExtraSawnOnCutGras: 6
- _FieldSize: 5
- _LOD: 1
- _Metallic: 0
- _NumSegments_LOD_0: 11
- _NumSegments_LOD_1: 3
- _NumSegments_LOD_2: 1
- _Roughness: 0.3
- _SSSStrength: 1
- _SegmentsMinusOne: 11
- _Translucency: 1
m_Colors:
- _BoxSize: {r: 0, g: 0, b: 0, a: 0}
- _BoxWorldCenter: {r: 0, g: 0, b: 0, a: 0}
- _Braun: {r: 0.33999997, g: 0.85, b: 0.92, a: 1}
- _Farbe: {r: 0.33999997, g: 0.85, b: 0.92, a: 1}
- _InnerColor: {r: 1, g: 1, b: 1, a: 1}
- _SpecularColor: {r: 1, g: 1, b: 1, a: 1}
- _WindOffset: {r: 0, g: 0, b: 0, a: 0}
m_BuildTextureStacks: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 826d52a86cbdbd148ac471a227237ffe
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,72 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: GrassLOD2
m_Shader: {fileID: 4800000, guid: eebe2969663fc40449d6052c661a6c2e, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords:
- _LOD_LOD_2
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BladeWidthTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _FrabVarianz:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _HeightMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MaskMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _WindTextur:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AOStrength: 1
- _BendStrength: 1
- _BladeBow: 0.3
- _BladeWidth: 0.05
- _ExtraSawnOnCutGras: 6
- _FieldSize: 5
- _LOD: 2
- _Metallic: 0
- _NumSegments_LOD_0: 11
- _NumSegments_LOD_1: 5
- _NumSegments_LOD_2: 1
- _Roughness: 0.3
- _SSSStrength: 1
- _SegmentsMinusOne: 11
- _Translucency: 1
m_Colors:
- _BoxSize: {r: 0, g: 0, b: 0, a: 0}
- _BoxWorldCenter: {r: 0, g: 0, b: 0, a: 0}
- _Braun: {r: 0.33999997, g: 0.85, b: 0.92, a: 1}
- _Farbe: {r: 0.33999997, g: 0.85, b: 0.92, a: 1}
- _InnerColor: {r: 1, g: 1, b: 1, a: 1}
- _SpecularColor: {r: 1, g: 1, b: 1, a: 1}
- _WindOffset: {r: 0, g: 0, b: 0, a: 0}
m_BuildTextureStacks: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f7693e3a8eaaa814480ba4085681ed02
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,50 @@ using UnityEngine;
[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class GrassChunk : MonoBehaviour {
[Header("LOD 0")]
public Material grassMaterialLOD0;
[Header("LOD 1")]
public Material grassMaterialLOD1;
[Header("LOD 2")]
public Material grassMaterialLOD2;
private int lod = -1;
[SerializeField]
public int LOD {
get { return lod; }
set {
if (lod == value) return;
lod = value;
var meshRenderer = GetComponent<MeshRenderer>();
switch (lod) {
case 0: meshRenderer.sharedMaterial = grassMaterialLOD0; break;
case 1: meshRenderer.sharedMaterial = grassMaterialLOD1; break;
case 2: meshRenderer.sharedMaterial = grassMaterialLOD2; break;
}
}
}
private void Update() {
UpdateLOD();
}
public void UpdateLOD() {
var meshRenderer = GetComponent<MeshRenderer>();
if (Camera.main) {
var sqrCamDist = meshRenderer.bounds.SqrDistance(Camera.main.transform.position);
if (sqrCamDist > 20f * 20f) {
LOD = 2;
}
else if (sqrCamDist > 5f * 5f) {
LOD = 1;
}
else {
LOD = 0;
}
}
}
private void OnDrawGizmos() {
Gizmos.color = new Color(1f, 0.7f, 0f, 0.5f);
var meshRenderer = GetComponent<MeshRenderer>();

View File

@ -7,8 +7,13 @@ using Random = UnityEngine.Random;
public class GrassField : MonoBehaviour {
public int size = 100;
public int numChunks = 5;
public Material grassMaterial;
public GrassChunk chunkPrefab;
[Header("LOD 0")]
public Material grassMaterialLOD0;
[Header("LOD 1")]
public Material grassMaterialLOD1;
[Header("LOD 2")]
public Material grassMaterialLOD2;
private struct BladeData {
public Vector3 rootPosition;
@ -19,7 +24,7 @@ public class GrassField : MonoBehaviour {
public void GenerateGrassField() {
Debug.Log("Generating grass field... ");
var blades = GenerateBladesRandom(100000);
var blades = GenerateBladesRandom(1000000);
CreateChunks(blades);
Debug.Log("Generating grass field done");
@ -30,9 +35,9 @@ public class GrassField : MonoBehaviour {
for (int i = 0; i < num; i++) {
blades[i].rootPosition = new Vector3(
Random.Range(0f, size), Random.Range(-0.5f, 0.5f), Random.Range(0f, size)
Random.Range(0f, size), Random.Range(-0.25f, 0f), Random.Range(0f, size)
);
blades[i].tipOffset = new Vector3(0.01f, 1.0f, 0.01f);
blades[i].tipOffset = new Vector3(Random.Range(-.8f, .8f), Random.Range(0.7f, 1f), Random.Range(-.8f, .8f));
}
return blades;
@ -87,9 +92,12 @@ public class GrassField : MonoBehaviour {
}
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 + "]";
if (rootPositions.Count > 65536) {
// 16 bit (default) supports 65536 vertices, 32 bit supports 4 billion
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
Debug.LogWarning(mesh.name + " has more than 65536 grass blades. Mesh.indexFormat set to UInt32.");
}
mesh.SetVertices(rootPositions);
mesh.SetUVs(0, tipOffsets); // Vector3 UVs
// AABB gets calculated automatically (considers all root positions)
@ -98,23 +106,24 @@ public class GrassField : MonoBehaviour {
// use mesh.bounds rather than meshRenderer.localBounds,
// because the latter gets overwritten automatically
var previousBounds = mesh.bounds;
float width = 1f;
float height = 1f;
float width = 1f; // a blade should never bend sideways farther than this
float height = 1f; // a blade should not be higher than this
mesh.bounds = new Bounds(
previousBounds.center + new Vector3(0f, height * 0.5f, 0f),
previousBounds.size + new Vector3(width, height, width)
previousBounds.size + new Vector3(2f*width, height, 2f*width)
);
var chunk = Instantiate(chunkPrefab, chunks.transform);
chunk.name = "Chunk [" + x + ", " + y +"]";
chunk.transform.localPosition = chunkPos;
chunk.grassMaterialLOD0 = grassMaterialLOD0;
chunk.grassMaterialLOD1 = grassMaterialLOD1;
chunk.grassMaterialLOD2 = grassMaterialLOD2;
var meshFilter = chunk.GetComponent<MeshFilter>();
meshFilter.sharedMesh = mesh;
var meshRenderer = chunk.GetComponent<MeshRenderer>();
meshRenderer.sharedMaterial = grassMaterial;
chunk.UpdateLOD();
}
}

View File

@ -1,8 +1,17 @@
Shader "Grass/GrassBlade" {
Properties {
[KeywordEnum(LOD 0, LOD 1, LOD 2)] _LOD("Level of Detail", int) = 0
_BladeWidth("Breite", Range(0.003, .1)) = 0.01
_SegmentsMinusOne("Segments Minus One", Range(1, 60)) = 20
_BendStrength("Bend Strength", Range(0, 1)) = 1
[Header(LOD 0)]
[IntRange] _NumSegments_LOD_0 ("Segments", Range(1,11)) = 11
[Header(LOD 1)]
[IntRange] _NumSegments_LOD_1 ("Segments", Range(1,5)) = 5
[Header(LOD 2)]
[IntRange] _NumSegments_LOD_2 ("Segments", Range(1,1)) = 1
}
SubShader {
LOD 100
@ -17,6 +26,7 @@
#pragma geometry geom
#pragma multi_compile _ SHADOWS_SCREEN
#pragma shader_feature _LOD_LOD_0 _LOD_LOD_1 _LOD_LOD_2
#include "UnityCG.cginc"
#include "UnityPBSLighting.cginc"
@ -41,6 +51,8 @@
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#pragma shader_feature _LOD_LOD_0 _LOD_LOD_1 _LOD_LOD_2
#include "UnityCG.cginc"

View File

@ -1,4 +1,5 @@
float _BladeWidth, _SegmentsMinusOne, _BendStrength;
float _BladeWidth, _BendStrength;
int _NumSegments_LOD_0, _NumSegments_LOD_1, _NumSegments_LOD_2;
struct MeshData {
float4 vertex : POSITION;
@ -11,7 +12,7 @@ struct v2g {
struct g2f {
float4 pos : SV_POSITION;
#if !defined(IS_IN_SHADOW_PASS)
#ifdef IS_IN_BASE_PASS
float2 uv : TEXCOORD0; // bottom left 0.0 top right 1.1 (values converge - 0 and 1 meet at the tip)
SHADOW_COORDS(1) // put shadows data into TEXCOORD1
#endif
@ -24,10 +25,14 @@ v2g vert (MeshData v) {
return o;
}
#if defined(IS_IN_SHADOW_PASS)
[MaxVertexCount(24)]
#else
[MaxVertexCount(24)]
const int ns = 11;
#if _LOD_LOD_0
[MaxVertexCount(4 + ( 11 - 1 ) * 2)]
#elif _LOD_LOD_1
[MaxVertexCount(4 + ( 5 - 1 ) * 2)]
#elif _LOD_LOD_2
[MaxVertexCount(3)]
#endif
void geom(point v2g IN[1], inout TriangleStream<g2f> triStream) {
const float3 basePos = IN[0].vertex.xyz;
@ -47,14 +52,22 @@ void geom(point v2g IN[1], inout TriangleStream<g2f> triStream) {
float3 previousSegmentCenter = basePos - float3(0,-1,0); // point beneath the basePos
int segmentsMinusOne = max(1, (int)(_SegmentsMinusOne) - ((cameraDistance) / 8.0) + N21(basePos));
// int numSegments = max(1, (int)(11) - ((cameraDistance) / 8.0) + N21(basePos));
for (int i = 0; i < segmentsMinusOne; i++) {
#if _LOD_LOD_0
int numSegments = _NumSegments_LOD_0;
#elif _LOD_LOD_1
int numSegments = _NumSegments_LOD_1;
#elif _LOD_LOD_2
int numSegments = _NumSegments_LOD_2;
#endif
for (int i = 0; i < numSegments; i++) {
// in "blade space"
const float lowerThickness = 0.8;
const float segmentWidth = halfWidth * pow(sin(lowerThickness * 3.141 * ((int)_SegmentsMinusOne - i) / (int)_SegmentsMinusOne), .8);
const float segmentHeightNormalized = i / _SegmentsMinusOne;
// float nextSegmentHeightNormalized = (i+1) / _SegmentsMinusOne;
const float segmentWidth = halfWidth * pow(sin(lowerThickness * 3.141 * (numSegments - i) / numSegments), .8);
const float segmentHeightNormalized = i / (float)numSegments;
// float nextSegmentHeightNormalized = (i+1) / (float)numSegments;
// generate new verts
const float3 widthOffset = getWidthOffset(segmentWidth, tipPosBladeSpace.xz) * max(1, cameraDistance / 8);
@ -65,7 +78,7 @@ void geom(point v2g IN[1], inout TriangleStream<g2f> triStream) {
const float3 vertRight = segmentCenter - widthOffset;
// const float3 nextSegmentCenter = basePos + bendParabula(tipPos, nextSegmentHeight01);
#if !defined(IS_IN_SHADOW_PASS)
#ifdef IS_IN_BASE_PASS
// calculate normal
const float3 surfTangent1 = previousSegmentCenter - segmentCenter;
const float3 surfTangent2 = previousSegmentCenter - vertLeft;
@ -81,14 +94,14 @@ void geom(point v2g IN[1], inout TriangleStream<g2f> triStream) {
}
#endif
o.pos = UnityObjectToClipPos(vertLeft);
#if !defined(IS_IN_SHADOW_PASS)
#ifdef IS_IN_BASE_PASS
o.uv = float2(0, segmentHeightNormalized);
TRANSFER_SHADOW(o)
#endif
triStream.Append(o);
o.pos = UnityObjectToClipPos(vertRight);
#if !defined(IS_IN_SHADOW_PASS)
#ifdef IS_IN_BASE_PASS
o.uv = float2(1, segmentHeightNormalized);
TRANSFER_SHADOW(o)
#endif
@ -98,7 +111,7 @@ void geom(point v2g IN[1], inout TriangleStream<g2f> triStream) {
}
o.pos = UnityObjectToClipPos(basePos + tipPosBladeSpace * float3(_BendStrength,1,_BendStrength));
#if !defined(IS_IN_SHADOW_PASS)
#ifdef IS_IN_BASE_PASS
o.uv = float2(.5, 1);
TRANSFER_SHADOW(o)
#endif
@ -106,19 +119,24 @@ void geom(point v2g IN[1], inout TriangleStream<g2f> triStream) {
}
fixed4 frag(g2f i) : SV_Target{
#if defined(IS_IN_SHADOW_PASS)
#ifdef IS_IN_SHADOW_PASS
return 0;
#else
float atten = 1.0;
const fixed shadow = SHADOW_ATTENUATION(i);
float3 lightFinal = float3(i.uv.yyy);
lightFinal *= max(.35, shadow);
float3 light = float3(i.uv.yyy);
light *= max(.35, shadow);
fixed4 albedo = fixed4(0.3,1.0,0.3,1.0);
#if _LOD_LOD_0
float3 albedo = float3(0.3,1.0,0.3);
#elif _LOD_LOD_1
float3 albedo = float3(1.0,0.7,0.3);
#elif _LOD_LOD_2
float3 albedo = float3(1.0,0.3,0.1);
#endif
fixed3 color = fixed3(lightFinal * albedo);
return fixed4(color, 1);
return float4(light * albedo, 1);
#endif
}