-
Notifications
You must be signed in to change notification settings - Fork 19
/
TriplanarMaterial.js
285 lines (238 loc) · 10.8 KB
/
TriplanarMaterial.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { MeshStandardMaterial, RepeatWrapping } from "three";
import React from "react";
class TriplanarStandardMaterialImpl extends MeshStandardMaterial {
// _splats = { value: [] };
_verticalMap = { value: [] };
_triplanar = { value: true };
// parameters = {};
// _diffuseMaps = { value: [] };
// _detailMaps = { value: [] };
// _normalMaps = { value: [] };
// _normalWeights = { value: [] };
// _scale = { value: [] };
// _detailScale = { value: [] };
// _saturation = { value: [] };
// _brightness = { value: [] };
// _noise = { value: undefined };
constructor(parameters) {
super(parameters);
// this._verticalMap = parameters.verticalMap;
// this.setValues(parameters);
this.parameters = parameters;
// console.log(parameters)
this._verticalMap.value = parameters.verticalMap
this._triplanar.value = parameters.triplanar;
// this._normalWeights.value = this._normalWeights.value.length > 0 ? this._normalWeights.value : new Array(12).fill("0.75");
// todo estimate scale
}
onBeforeCompile(shader) {
shader.extensions = {
derivatives: true,
shaderTextureLOD: true,
};
// console.log(this.parameters.verticalMap);
// const { normalMaps, normalMap, diffuseMaps, splats, noise } = this.parameters;
// shader.uniforms["splats"] = this._splats;
shader.uniforms["verticalMap"] = this._verticalMap;
shader.uniforms["triplanar"] = this._triplanar;
// shader.uniforms["diffuseMaps"] = this._diffuseMaps;
// shader.uniforms["normalMaps"] = this._normalMaps;
// shader.uniforms["detailMaps"] = this._detailMaps;
// shader.uniforms["normalWeights"] = this._normalWeights;
// shader.uniforms["scale"] = this._scale;
// shader.uniforms["detailScale"] = this._detailScale;
// shader.uniforms["saturation"] = this._saturation;
// shader.uniforms["brightness"] = this._brightness;
// if (noise) shader.uniforms["noise"] = { value: noise };
// make sure that these textures tile correctly
// [...(normalMaps || []), ...splats, ...diffuseMaps, normalMap, noise]
// .filter((d) => d !== null && d !== undefined)
// .forEach((t) => {
// t.wrapS = RepeatWrapping;
// t.wrapT = RepeatWrapping;
// });
shader.vertexShader = shader.vertexShader
.replace(
`varying vec3 vViewPosition;`,
`
varying vec3 vViewPosition;
varying mat4 vModelMatrix;
varying vec3 vPosition;
varying vec3 vNorm;
varying vec3 vTransformed;
varying vec3 vWorldPos;
`
)
.replace(
`#include <worldpos_vertex>`,
`
#include <worldpos_vertex>
vModelMatrix = modelMatrix;
vPosition = position.xyz;
vNorm = normal;
vTransformed = transformed;
vWorldPos = (modelMatrix * vec4(vTransformed, 1)).xyz;
`
);
shader.fragmentShader = shader.fragmentShader
.replace(
"uniform float opacity;",
`
uniform float opacity;
varying mat4 vModelMatrix;
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
varying vec3 vPosition;
varying vec3 vNorm;
uniform sampler2D displacementMap;
varying vec3 vTransformed;
uniform sampler2D verticalMap;
uniform bool triplanar;
varying vec3 vWorldPos;
vec4 triplanarSample(vec3 pos, vec3 normal, vec3 ds, sampler2D map, sampler2D verticalMap) {
vec4 dx = (texture2D(verticalMap, pos.zy *2.0)) * 0.9;
vec4 dy = (texture2D(verticalMap, pos.xz *2.0)) * 0.9;
vec4 dz = texture2D(map, pos.xy);
// vec4 dx = vec4(1.0, 0.0, 0.0, 1.0);
// vec4 dy = vec4(0.0, 1.0, 0.0, 1.0);
// vec4 dz = vec4(0.0, 0.0, 1.0, 1.0);
vec3 weights = pow(abs(normal.xyz), vec3(1.0));
weights = weights / (weights.x + weights.y + weights.z);
// float d = 0.75;
// float v = ds.x < d? (d-ds.x)/5.0 : 0.0;
// return abs(dx * weights.x + dy * weights.y + dz * weights.z
// - vec4(v,v,v, 0.0));
return abs(dx * weights.x + dy * weights.y + dz * weights.z);
}
vec4 triplanarNormal(vec3 pos, sampler2D map) {
return texture2D(map, pos.xy);
}
`
)
.replace(
"#include <map_fragment>",
`
#include <map_fragment>
vec3 ds = texture2D(displacementMap, vUv).xyz;
vec3 n = texture2D(normalMap, vUv).xyz * 2.0 - 1.0;
vec3 worldPosition = (modelMatrix * vec4(vPosition, 1)).xyz;
vec3 worldTransform = (modelMatrix * vec4(vTransformed, 1)).xyz;
worldTransform += vec3(0.5, 0.5, 0.0);
vec3 worldSpaceNormal = (modelMatrix * vec4(vNorm, 0.0)).xyz;
vec3 dFdxPos = dFdx( vWorldPos );
vec3 dFdyPos = dFdy( vWorldPos );
vec3 facenormal = normalize( cross(dFdxPos,dFdyPos ));
// idea: checkign dF gives slope on face and a faceted look
// For the purposes of identifying top, left, right sides its more about total vertical distance traveled
// it if this pixel has traveled more in the y then its' top projected x distance its not vertical.
// sample all surrounding pixels in the hightmap,
// work out the x distance associated with one pixel width.
// if the height change > the width then its not vertical
// this should give accurate results down to the resolution of the heightmap.
/// or not.
// this is in object space
// idea: modulate accuracy based on
float o = 0.0015;
float h = dot(texture2D(displacementMap, vUv), vec4(1,0,0,1));
float hx = dot(texture2D(displacementMap, vUv + vec2( o, 0.0 )), vec4(1,0,0,1));
float hy = dot(texture2D(displacementMap, vUv + vec2( 0.0, o )), vec4(1,0,0,1));
float dScale = 25.0;
float dx = (hx - h) * dScale;
float dy = (hy - h) * dScale;
vec3 heightNormal = (cross(vec3(1.0,0.0,dx), vec3(0.0,1.0,dy)));
// diffuseColor = vec4(1.0,0.0,dx,1.0);
// diffuseColor = vec4(0.0,1.0,dy,1.0);
// diffuseColor = vec4(facenormal, 1.0);
// diffuseColor = vec4(heightNormal, 1.0);
// diffuseColor = vec4(n, 1.0);
// diffuseColor= vec4(worldTransform.z*100.0, 0,0.0, 1.0);
// diffuseColor= vec4(worldTransform.rg, 0.0, 1.0);
// diffuseColor = texture2D(map, vUv);
if(facenormal.z > 0.5 && n.b > 0.5) {
// n.b = 0.0;
// n.r = 1.0;
}
// diffuseColor = vec4(heightNormal,1.0);
// diffuseColor = vec4(n,1.0);
if (triplanar) {
// diffuseColor = triplanarSample(worldTransform, n, map, verticalMap);
diffuseColor = triplanarSample(worldTransform, heightNormal, ds, map, verticalMap);
// diffuseColor = triplanarSample(worldTransform, facenormal, normalMap, normalMap);
// diffuseColor = triplanarSample(worldTransform, facenormal, map, verticalMap);
}
`
)
// .replace(
// "#include <normal_fragment_maps>",
// `
// #ifdef OBJECTSPACE_NORMALMAP
// // normal = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals
// // normal = triplanarNormal( vUv, normalMap ).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals
// #ifdef FLIP_SIDED
// normal = - normal;
// #endif
// #ifdef DOUBLE_SIDED
// normal = normal * faceDirection;
// #endif
// normal = normalize( normalMatrix * normal );
// #elif defined( TANGENTSPACE_NORMALMAP )
// // vec3 mapN = triplanarNormal( vUv, normalMap ).xyz * 2.0 - 1.0;
// // vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
// vec3 mapN = heightNormal.xyz;
// mapN.xy *= normalScale;
// #ifdef USE_TANGENT
// normal = normalize( vTBN * mapN );
// #else
// normal = perturbNormal2Arb( - vViewPosition, normal, mapN, faceDirection );
// #endif
// #elif defined( USE_BUMPMAP )
// normal = perturbNormalArb( - vViewPosition, normal, dHdxy_fwd(), faceDirection );
// #endif
// `
// );
}
set triplanar(v) {
this._triplanar.value = v;
}
// set splats(v) {
// this._splats.value = v;
// }
// set normalMaps(v) {
// this._normalMaps.value = v;
// }
// set normalWeights(v) {
// this._normalWeights.value = v;
// }
// set detailMaps(v) {
// this._detailMaps.value = v;
// }
// set diffuseMaps(v) {
// this._diffuseMaps.value = v;
// }
// set scale(v) {
// this._scale.value = v;
// }
// set detailScale(v) {
// this._detailScale.value = v;
// }
// set saturation(v) {
// this._saturation.value = v;
// }
// set brightness(v) {
// this._brightness.value = v;
// }
// set noise(v) {
// this._noise.value = v;
// }
}
const TriplanarStandardMaterial = React.forwardRef((props = {}, ref) => {
// const material = React.useMemo(() => new TriplanarStandardMaterialImpl(props), props);
const [material] = React.useState(() => new TriplanarStandardMaterialImpl(props));
const { verticalMap } = props;
[verticalMap].forEach(t => {
t.wrapS = RepeatWrapping;
t.wrapT = RepeatWrapping;
})
return <primitive dispose={undefined} object={material} ref={ref} attach="material" {...props} />;
});
export default TriplanarStandardMaterial;