Rise
The Vieneo Province
Loading...
Searching...
No Matches
CitySignQuad.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4
5// Makeship city DAW records omit textureOverlay. For the full-image sign quads
6// specified by Jason, derive a SECOND coordinate set from geometry at load time.
7// Local +Z is up. The normal defines the front; base texture UVs are untouched.
8template<class Vertex> bool CitySignQuadUV(Vertex* vertices, unsigned count)
9{
10 if (!vertices || count != 6) return false;
11 struct V { float x,y,z; };
12 const auto dot = [](V a,V b) { return a.x*b.x+a.y*b.y+a.z*b.z; };
13 const auto cross = [](V a,V b) { return V{a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x}; };
14 const auto norm = [&](V a) { const float s=std::sqrt(dot(a,a)); return V{a.x/s,a.y/s,a.z/s}; };
15 const V a{vertices[1].x-vertices[0].x,vertices[1].y-vertices[0].y,vertices[1].z-vertices[0].z};
16 const V b{vertices[2].x-vertices[0].x,vertices[2].y-vertices[0].y,vertices[2].z-vertices[0].z};
17 V n=cross(a,b); // DAW triangle winding, before the client's normal conversion
18 if (!std::isfinite(dot(n,n)) || dot(n,n)<1e-20f) return false;
19 n=norm(n);
20 const V right=norm(cross(std::fabs(n.z)<0.99f ? V{0,0,1} : V{0,1,0},n));
21 const V up=cross(n,right);
22 float x[6],y[6],plane[6];
23 for (unsigned i=0;i<6;++i)
24 {
25 const V p{vertices[i].x,vertices[i].y,vertices[i].z};
26 x[i]=dot(p,right); y[i]=dot(p,up); plane[i]=dot(p,n);
27 if (!std::isfinite(x[i]) || !std::isfinite(y[i]) || !std::isfinite(plane[i])) return false;
28 }
29 const auto xb=std::minmax_element(x,x+6),yb=std::minmax_element(y,y+6);
30 const float width=*xb.second-*xb.first,height=*yb.second-*yb.first;
31 if (width<1e-7f || height<1e-7f) return false;
32 const float tolerance=(std::max)(width,height)*0.001f;
33 unsigned corners=0;
34 for (unsigned i=0;i<6;++i)
35 {
36 if (std::fabs(plane[i]-plane[0])>tolerance) return false;
37 const float u=(x[i]-*xb.first)/width,v=(*yb.second-y[i])/height;
38 const unsigned cx=u>0.5f,cy=v>0.5f;
39 if (std::fabs(u-cx)>0.001f || std::fabs(v-cy)>0.001f) return false;
40 corners |= 1u << (cx+2*cy);
41 }
42 if (corners!=15) return false;
43 for (unsigned i=0;i<6;++i)
44 {
45 vertices[i].tu2=(x[i]-*xb.first)/width;
46 vertices[i].tv2=(*yb.second-y[i])/height;
47 }
48 return true;
49}
bool CitySignQuadUV(Vertex *vertices, unsigned count)
Definition CitySignQuad.h:8
#define V(expression)