-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatReceptor.cs
92 lines (80 loc) · 3 KB
/
HeatReceptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public enum Hand
{
Left,
Right
}
public class HeatReceptor : MonoBehaviour
{
public Hand whichHand;
[Range(25, 35)]
public int initialTemperature = 33; // The temperature of the heat receptor.
[Range(0f, 10f)]
public float maxDistance = 5f; // The maximum distance within which heat transfer occurs.
public TMP_Text temperatureText; // Reference to the TextMeshPro component.
private int currentTemperature;
private float distance;
private void Update()
{
// Find all HeatSource objects in the scene.
HeatSource[] heatSources = FindObjectsOfType<HeatSource>();
int sourceCount = 0;
int cumTemperature = 0;
int conductTemperature = initialTemperature;
int averageTemperature = 0;
// Calculate the temperature based on radiative heat transfer from each HeatSource.
foreach (HeatSource heatSource in heatSources)
{
distance = Vector3.Distance(heatSource.transform.position, transform.position);
if (heatSource.heatTransferType.Equals(HeatTransferType.Conduction) && distance < 0.13f)
{
conductTemperature = heatSource.temperature;
break;
}
else if (distance <= maxDistance && heatSource.heatTransferType.Equals(HeatTransferType.Radiation))
{
sourceCount++;
int radiativeHeat = (int)CalculateRadiativeHeat(heatSource.temperature, distance, heatSource.emissivity);
cumTemperature += radiativeHeat;
}
}
if (sourceCount > 0)
{
averageTemperature = (int)cumTemperature/sourceCount;
}
if (conductTemperature > averageTemperature || conductTemperature < initialTemperature)
{
currentTemperature = conductTemperature;
}
else if (averageTemperature < initialTemperature)
{
currentTemperature = initialTemperature;
}
else
{
currentTemperature = averageTemperature;
}
// Update the temperature display on the TextMeshPro object.
temperatureText.text = currentTemperature.ToString() + "°C";
}
public float GetCurrentTemperature()
{
return currentTemperature;
}
// Calculate radiative heat transfer between the heat source and receptor using the Stefan-Boltzmann law.
private float CalculateRadiativeHeat(int heatSourceTemperature, float distance, float emissivity)
{
float radiativeHeat = heatSourceTemperature * Mathf.Pow(emissivity, 0.25f) * ((maxDistance - Mathf.Pow(distance + 0.5f, 4f)) / maxDistance);
if (radiativeHeat < initialTemperature)
{
return initialTemperature;
}
else
{
return radiativeHeat;
}
}
}