-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOBJLoaderHelper.cs
More file actions
103 lines (92 loc) · 3.48 KB
/
Copy pathOBJLoaderHelper.cs
File metadata and controls
103 lines (92 loc) · 3.48 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
using System.Globalization;
using UnityEngine;
namespace Dummiesman
{
public static class OBJLoaderHelper
{
/// <summary>
/// Enables transparency mode on standard materials
/// </summary>
public static void EnableMaterialTransparency(Material mtl)
{
mtl.SetFloat("_Mode", 3f);
mtl.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
mtl.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
mtl.SetInt("_ZWrite", 0);
mtl.DisableKeyword("_ALPHATEST_ON");
mtl.EnableKeyword("_ALPHABLEND_ON");
mtl.DisableKeyword("_ALPHAPREMULTIPLY_ON");
mtl.renderQueue = 3000;
}
/// <summary>
/// Modified from https://codereview.stackexchange.com/a/76891. Faster than float.Parse
/// </summary>
public static float FastFloatParse(string input)
{
if (input.Contains("e") || input.Contains("E"))
return float.Parse(input, CultureInfo.InvariantCulture);
float result = 0;
int pos = 0;
int len = input.Length;
if (len == 0) return float.NaN;
char c = input[0];
float sign = 1;
if (c == '-')
{
sign = -1;
++pos;
if (pos >= len) return float.NaN;
}
while (true) // breaks inside on pos >= len or non-digit character
{
if (pos >= len) return sign * result;
c = input[pos++];
if (c < '0' || c > '9') break;
result = (result * 10.0f) + (c - '0');
}
if (c != '.' && c != ',') return float.NaN;
float exp = 0.1f;
while (pos < len)
{
c = input[pos++];
if (c < '0' || c > '9') return float.NaN;
result += (c - '0') * exp;
exp *= 0.1f;
}
return sign * result;
}
/// <summary>
/// Modified from http://cc.davelozinski.com/c-sharp/fastest-way-to-convert-a-string-to-an-int. Faster than int.Parse
/// </summary>
public static int FastIntParse(string input)
{
int result = 0;
bool isNegative = (input[0] == '-');
for (int i = (isNegative) ? 1 : 0; i < input.Length; i++)
result = result * 10 + (input[i] - '0');
return (isNegative) ? -result : result;
}
public static Material CreateNullMaterial()
{
return new Material(Shader.Find("Standard (Specular setup)"));
}
public static Vector3 VectorFromStrArray(string[] cmps)
{
float x = FastFloatParse(cmps[1]);
float y = FastFloatParse(cmps[2]);
if (cmps.Length == 4)
{
float z = FastFloatParse(cmps[3]);
return new Vector3(x, y, z);
}
return new Vector2(x, y);
}
public static Color ColorFromStrArray(string[] cmps, float scalar = 1.0f)
{
float Kr = FastFloatParse(cmps[1]) * scalar;
float Kg = FastFloatParse(cmps[2]) * scalar;
float Kb = FastFloatParse(cmps[3]) * scalar;
return new Color(Kr, Kg, Kb);
}
}
}