This repository was archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPQSMod_HeightColorMap.cs
More file actions
123 lines (110 loc) · 3.8 KB
/
PQSMod_HeightColorMap.cs
File metadata and controls
123 lines (110 loc) · 3.8 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* libpqsmods - A standalone implementation of KSP's PQSMods
* Copyright (c) Thomas P. 2016
* Licensed under the terms of the MIT license
*/
using System;
using System.Linq;
using ProceduralQuadSphere.Unity;
namespace ProceduralQuadSphere
{
/// <summary>
/// A mod that colors the terrain based on it's altitude
/// </summary>
/// <seealso cref="PQSMod" />
public class PQSMod_HeightColorMap : PQSMod
{
/// <summary>
/// A definition for a landclass, basically a link between altitude and color.
/// </summary>
public class LandClass
{
/// <summary>
/// The name of the landclass
/// </summary>
public String name;
/// <summary>
/// Where the landclass starts
/// </summary>
public Double altStart;
/// <summary>
/// Where the landclass ends
/// </summary>
public Double altEnd;
/// <summary>
/// The color of the surface in this area
/// </summary>
public Color color;
/// <summary>
/// Whether the color should get lerped into the next landclass
/// </summary>
public Boolean lerpToNext;
/// <summary>
/// The difference between <see cref="altStart"/> and <see cref="altEnd"/>
/// </summary>
public Double fractalDelta
{
get { return altEnd - altStart; }
}
/// <summary>
/// Initializes a new instance of the <see cref="LandClass"/> class.
/// </summary>
public LandClass(String name, Double fractalStart, Double fractalEnd, Color baseColor)
{
this.name = name;
this.altStart = fractalStart;
this.altEnd = fractalEnd;
this.color = baseColor;
}
}
/// <summary>
/// The land classes the mod uses
/// </summary>
public LandClass[] landClasses;
/// <summary>
/// How much the colors should get lerped into each other
/// </summary>
public Single blend;
/// <summary>
/// How many landclasses exist
/// </summary>
public Int32 lcCount
{
get { return landClasses.Length; }
}
/// <summary>
/// Called when the parent sphere builds it's color
/// </summary>
public override void OnVertexBuild(VertexBuildData data)
{
Double vHeight = (data.vertHeight - sphere.radiusMin) / sphere.radiusDelta;
Int32 index;
LandClass lcSelected = SelectLandClassByHeight(vHeight, out index);
if (lcSelected.lerpToNext)
{
data.vertColor = Color.Lerp(data.vertColor,
Color.Lerp(lcSelected.color, landClasses[index + 1].color,
(Single)((vHeight - lcSelected.altStart) / (lcSelected.altEnd - lcSelected.altStart))), blend);
}
else
{
data.vertColor = Color.Lerp(data.vertColor, lcSelected.color, blend);
}
}
/// <summary>
/// Selects a landclass based on it's height
/// </summary>
/// <param name="height">The height of the landclass area.</param>
public LandClass SelectLandClassByHeight(Double height, out Int32 index)
{
for (Int32 itr = 0; itr < lcCount; itr++)
{
index = itr;
if (height >= landClasses[itr].altStart && height <= landClasses[itr].altEnd)
return landClasses[itr];
}
index = lcCount - 1;
return landClasses.Last();
}
}
}