Skip to content

Commit 3d4ecb0

Browse files
committed
pre push rewrite
1 parent e2a6e30 commit 3d4ecb0

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

src/components/materials/planetMaterial.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import * as THREE from 'three'
33
export class PlanetMaterial extends THREE.ShaderMaterial {
44

55
constructor(parameters) {
6+
67
super();
78

89
this.uniforms = {
910
resolution: { value: parameters.resolution || 256 },
1011
// textures
1112
cubeNormal: { value: parameters.cubeNormal },
13+
cubeColor: { value: parameters.cubeColor },
1214
cubeDisplacements: { value: parameters.cubeDisplacements },
13-
cubeColor: { value: parameters.cubeColor },
1415
// check if textures are provided
1516
hasNormals: { value: parameters.cubeNormal ? true : false },
1617
hasColors: { value: parameters.cubeColor ? true : false },
@@ -37,12 +38,57 @@ export class PlanetMaterial extends THREE.ShaderMaterial {
3738
vec4 baseColor;
3839
vec4 baseNormal;
3940
41+
void tangentBitangent(inout vec3 tangent, inout vec3 bitangent, vec3 dir){
42+
// Determine which face we're on and calculate appropriate tangent vectors
43+
vec3 absDir = abs(dir);
44+
if (absDir.x >= absDir.y && absDir.x >= absDir.z) {
45+
// X face (left/right)
46+
tangent = normalize(cross(dir, vec3(0.0, 1.0, 0.0)));
47+
bitangent = normalize(cross(dir, tangent));
48+
} else if (absDir.y >= absDir.x && absDir.y >= absDir.z) {
49+
// Y face (top/bottom)
50+
tangent = normalize(cross(dir, vec3(1.0, 0.0, 0.0)));
51+
bitangent = normalize(cross(dir, tangent));
52+
} else {
53+
// Z face (front/back)
54+
tangent = normalize(cross(dir, vec3(0.0, 1.0, 0.0)));
55+
bitangent = normalize(cross(dir, tangent));
56+
}
57+
}
58+
59+
vec3 localNormal(vec3 dir, vec3 tangent, vec3 bitangent, float heightScale) {
60+
float eps = 1./uRes;
61+
62+
float right = texture(uTex, normalize(dir + tangent * eps)).r;
63+
float left = texture(uTex, normalize(dir - tangent * eps)).r;
64+
float top = texture(uTex, normalize(dir + bitangent * eps)).r;
65+
float bottom = texture(uTex, normalize(dir - bitangent * eps)).r;
66+
67+
vec2 gradient = vec2(
68+
(right - left) * heightScale,
69+
(top - bottom) * heightScale
70+
);
71+
return normalize(vec3(-gradient.x, -gradient.y, 1.0));
72+
}
73+
74+
75+
vec3 localNormal(vec3 dir) { return texture(uTex, normalize(dir)).rgb; }
76+
77+
vec3 worldNormal(vec3 dir, vec3 tangent, vec3 bitangent, float heightScale) {
78+
vec3 localNor = localNormal(vWorldPosition, tangent, bitangent, heightScale);
79+
return normalize(
80+
localNor.x * tangent +
81+
localNor.y * bitangent +
82+
localNor.z * normalize(dir)
83+
);
84+
}
85+
4086
void main() {
41-
baseColor = vec4(1.0);
42-
baseNormal = vec4(1.0);
43-
if(hasNormals){ baseNormal = textureCube(cubeNormal, vPosition );}
44-
if(hasColors){ baseColor = textureCube(cubeColor, vPosition );}
45-
gl_FragColor = baseColor;
87+
baseColor = vec4(1.0);
88+
baseNormal = vec4(1.0);
89+
if(hasNormals){ baseNormal = texture( cubeNormal, vPosition );}
90+
if(hasColors) { baseColor = texture( cubeColor, vPosition );}
91+
gl_FragColor = baseColor;
4692
}`
4793
}
4894

0 commit comments

Comments
 (0)