Updated nutrient lookup to include radius around plant based on size of plant

This commit is contained in:
2025-08-06 22:58:01 -07:00
parent a30cbca5c9
commit 7097c8ce53
7 changed files with 63 additions and 35 deletions
@@ -20,20 +20,24 @@ Material:
m_CorrespondingSourceObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_Name: test m_Name: nutrientMap
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3} m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
m_Parent: {fileID: 0} m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0 m_ModifiedSerializedProperties: 0
m_ValidKeywords: [] m_ValidKeywords:
- _ALPHAPREMULTIPLY_ON
- _SURFACE_TYPE_TRANSPARENT
m_InvalidKeywords: [] m_InvalidKeywords: []
m_LightmapFlags: 4 m_LightmapFlags: 4
m_EnableInstancingVariants: 0 m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0 m_DoubleSidedGI: 0
m_CustomRenderQueue: -1 m_CustomRenderQueue: 3000
stringTagMap: stringTagMap:
RenderType: Opaque RenderType: Transparent
disabledShaderPasses: disabledShaderPasses:
- MOTIONVECTORS - MOTIONVECTORS
- DepthOnly
- SHADOWCASTER
m_LockedProperties: m_LockedProperties:
m_SavedProperties: m_SavedProperties:
serializedVersion: 3 serializedVersion: 3
@@ -63,7 +67,7 @@ Material:
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MainTex: - _MainTex:
m_Texture: {fileID: 2800000, guid: 97c1df594a1bc8447a058044bea5a739, type: 3} m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MetallicGlossMap: - _MetallicGlossMap:
@@ -108,8 +112,8 @@ Material:
- _Cutoff: 0.5 - _Cutoff: 0.5
- _DetailAlbedoMapScale: 1 - _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1 - _DetailNormalMapScale: 1
- _DstBlend: 0 - _DstBlend: 10
- _DstBlendAlpha: 0 - _DstBlendAlpha: 10
- _EnvironmentReflections: 1 - _EnvironmentReflections: 1
- _GlossMapScale: 0 - _GlossMapScale: 0
- _Glossiness: 0 - _Glossiness: 0
@@ -124,10 +128,10 @@ Material:
- _SpecularHighlights: 1 - _SpecularHighlights: 1
- _SrcBlend: 1 - _SrcBlend: 1
- _SrcBlendAlpha: 1 - _SrcBlendAlpha: 1
- _Surface: 0 - _Surface: 1
- _WorkflowMode: 1 - _WorkflowMode: 1
- _XRMotionVectorsPass: 1 - _XRMotionVectorsPass: 1
- _ZWrite: 1 - _ZWrite: 0
m_Colors: m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1} - _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1} - _Color: {r: 1, g: 1, b: 1, a: 1}
+34 -23
View File
@@ -1,3 +1,4 @@
using System;
using UnityEditor.TextCore.Text; using UnityEditor.TextCore.Text;
using UnityEngine; using UnityEngine;
using static UnityEngine.GraphicsBuffer; using static UnityEngine.GraphicsBuffer;
@@ -29,15 +30,43 @@ public class NutrientController : MonoBehaviour
} }
public float GetNutrients(float x, float y) public float GetNutrients(float x, float y, float radius)
{ {
int locX = Mathf.FloorToInt(x * sizeX); int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY); int locY = Mathf.FloorToInt(y * sizeY);
return nutrientMap[locX, locY]; int ceilRad = Mathf.CeilToInt(radius);
float totalNutrients = 0;
for(int r = -ceilRad; r < ceilRad; r++)
{
for (int c = -ceilRad; c < ceilRad; c++)
{
if((locX + ceilRad > 0 && locX + ceilRad < sizeX) && (locY + ceilRad > 0 && locY + ceilRad < sizeY))
{
int fraction = Mathf.Abs(r) + Mathf.Abs(c);
if(fraction == 0)
{
totalNutrients += nutrientMap[locX + ceilRad, locY + ceilRad];
}
else
{
totalNutrients += nutrientMap[locX + ceilRad, locY + ceilRad] / (fraction * 2);
}
}
}
} }
public void UseNutrients(float x, float y, float used) if(totalNutrients == 0)
{
float thing = nutrientMap[locX, locY];
Debug.Log("HERE" + thing);
}
return totalNutrients;
}
public void UseNutrients(float x, float y, float radius, float used)
{ {
int locX = Mathf.FloorToInt(x * sizeX); int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY); int locY = Mathf.FloorToInt(y * sizeY);
@@ -61,26 +90,13 @@ public class NutrientController : MonoBehaviour
for (int c = 0; c < sizeY; c++) for (int c = 0; c < sizeY; c++)
{ {
float colorValue = nutrientMap[r, c]; float colorValue = nutrientMap[r, c];
texture.SetPixel(r, c, new Color(colorValue, colorValue, colorValue)); texture.SetPixel(r, c, new Color(colorValue, colorValue, colorValue, 0.5f));
} }
} }
texture.Apply(); texture.Apply();
meshRenderer.material.mainTexture = texture; meshRenderer.material.mainTexture = texture;
} meshRenderer.gameObject.SetActive(true);
private void ConsoleDump()
{
string toPrint = "\n";
for (int r = 0; r < sizeX; r++)
{
for (int c = 0; c < sizeY; c++)
{
toPrint += nutrientMap[r, c] + " ";
}
toPrint += "\n";
}
Debug.Log(toPrint);
} }
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -95,11 +111,6 @@ public class NutrientController : MonoBehaviour
{ {
(target as NutrientController).Render(); (target as NutrientController).Render();
} }
if(GUILayout.Button("Dump it"))
{
(target as NutrientController).ConsoleDump();
}
} }
} }
#endif #endif
+7 -2
View File
@@ -42,9 +42,14 @@ public class Plant : MonoBehaviour
float x = transform.position.x / terrain.terrainData.size.x; float x = transform.position.x / terrain.terrainData.size.x;
float y = transform.position.z / terrain.terrainData.size.z; float y = transform.position.z / terrain.terrainData.size.z;
float size = visual.transform.localScale.x;
float nutrientsAvailable = nutrientCont.GetNutrients(x, y, size);
Debug.Log($"Nutrients available {nutrientsAvailable}");
float nutrientsAvailable = nutrientCont.GetNutrients(x, y);
float nutrientsNeededFrame = (nutrientNeed * Time.deltaTime); float nutrientsNeededFrame = (nutrientNeed * Time.deltaTime);
if (nutrientsNeededFrame > nutrientsAvailable) if (nutrientsNeededFrame > nutrientsAvailable)
{ {
healthCurrent -= Time.deltaTime * 0.1f * healthMax; healthCurrent -= Time.deltaTime * 0.1f * healthMax;
@@ -52,7 +57,7 @@ public class Plant : MonoBehaviour
else else
{ {
visual.transform.localScale += visual.transform.localScale * (growthRate * Time.deltaTime); visual.transform.localScale += visual.transform.localScale * (growthRate * Time.deltaTime);
nutrientCont.UseNutrients(x, y, nutrientsNeededFrame); nutrientCont.UseNutrients(x, y, size, nutrientsNeededFrame);
nutrientsConsumed += nutrientsNeededFrame; nutrientsConsumed += nutrientsNeededFrame;
} }
+1 -1
View File
@@ -18,7 +18,7 @@ public class PlantController : MonoBehaviour
xBound = terrain.GetComponent<Terrain>().terrainData.size.x; xBound = terrain.GetComponent<Terrain>().terrainData.size.x;
zBound = terrain.GetComponent<Terrain>().terrainData.size.z; zBound = terrain.GetComponent<Terrain>().terrainData.size.z;
for (int i = 0; i < 100; i++) for (int i = 0; i < 1; i++)
{ {
GameObject newPlant = Instantiate(plantPrefab); GameObject newPlant = Instantiate(plantPrefab);
newPlant.GetComponent<Plant>().SetVisual(plantModels[Random.Range(0, plantModels.Count - 1)]); newPlant.GetComponent<Plant>().SetVisual(plantModels[Random.Range(0, plantModels.Count - 1)]);
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a76965b09359b2745aeaf339a493e753
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 15600000
userData:
assetBundleName:
assetBundleVariant: