-
Notifications
You must be signed in to change notification settings - Fork 39
/
flow.js
220 lines (171 loc) · 6.13 KB
/
flow.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
/**
* Prepares texture for storing positions and normals for spline
*/
function initTexture() {
if ( ! renderer.extensions.get( "OES_texture_float" ) ) {
console.log("No OES_texture_float support for float textures.");
}
if ( renderer.capabilities.maxVertexTextures === 0 ) {
console.log("No support for vertex shader textures.");
}
const height = 4;
const dataArray = new Float32Array( TEXTURE_WIDTH * height * BITS );
const dataTexture = new THREE.DataTexture(
dataArray,
TEXTURE_WIDTH,
height,
THREE.RGBFormat,
THREE.FloatType
);
dataTexture.wrapS = THREE.RepeatWrapping;
dataTexture.wrapY = THREE.RepeatWrapping;
dataTexture.magFilter = THREE.LinearFilter;
dataTexture.needsUpdate = true;
return dataTexture;
}
function setTextureValue(index, x, y, z, o) {
const image = texture.image;
const { width, height, data } = image;
const i = BITS * width * (o || 0);
data[index * BITS + i + 0] = x;
data[index * BITS + i + 1] = y;
data[index * BITS + i + 2] = z;
}
function modifyShader( material ) {
if (material.__ok) return;
material.__ok = true;
material.onBeforeCompile = ( shader ) => {
if (shader.__modified) return;
shader.__modified = true;
uniforms = Object.assign(shader.uniforms, {
texture: { value: texture },
pathOffset: { type: 'f', value: 0 }, // time of path curve
pathSegment: { type: 'f', value: 1 }, // fractional length of path
spineOffset: { type: 'f', value: 161 },
spineLength: { type: 'f', value: 400 },
flow: { type: 'i', value: 1 },
});
for (var k in bufferUniforms) {
updateUniform(k, bufferUniforms[k]);
}
vertexShader = `
uniform sampler2D texture;
uniform float pathOffset;
uniform float pathSegment;
uniform float spineOffset;
uniform float spineLength;
uniform int flow;
float textureLayers = 4.; // look up takes (i + 0.5) / textureLayers
${shader.vertexShader}
`
vertexShader = vertexShader.replace(
'#include <defaultnormal_vertex>',
`
vec4 worldPos = modelMatrix * vec4(position, 1.);
bool bend = flow > 0;
float spinePortion = bend ? (worldPos.x + spineOffset) / spineLength : 0.;
float xWeight = bend ? 0. : 1.;
float mt = spinePortion * pathSegment + pathOffset;
vec3 spinePos = texture2D(texture, vec2(mt, (0.5) / textureLayers)).xyz;
vec3 a = texture2D(texture, vec2(mt, (1. + 0.5) / textureLayers)).xyz;
vec3 b = texture2D(texture, vec2(mt, (2. + 0.5) / textureLayers)).xyz;
vec3 c = texture2D(texture, vec2(mt, (3. + 0.5) / textureLayers)).xyz;
mat3 basis = mat3(a, b, c);
vec3 transformed = basis
* vec3(worldPos.x * xWeight, worldPos.y * 1., worldPos.z * 1.)
+ spinePos;
vec3 transformedNormal = normalMatrix * (basis * objectNormal);
`
).replace(
'#include <begin_vertex>',
''
).replace(
'#include <project_vertex>',
`
vec4 mvPosition = viewMatrix * vec4( transformed, 1.0 );
// vec4 mvPosition = viewMatrix * worldPos;
gl_Position = projectionMatrix * mvPosition;
`
)
shader.vertexShader = vertexShader
console.log('Current shader template', vertexShader);
}
}
function initPathShader() {
customMaterial = new THREE.MeshPhongMaterial({
// color: 0x000000
color: 0xffffff,
// emission: 0xffffff
});
customMaterial.map = new THREE.TextureLoader().load( "orca.png" );
modifyShader( customMaterial );
texture = initTexture();
activate(null, (moo) => {
console.log(moo);
onLoad(moo);
});
var loader = new THREE.OBJLoader();
loader.load(
// resource URL
'ORCA.OBJ',
// called when resource is loaded
onLoad,
// called when loading is in progresses
function onProgress( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function onError( error ) {
console.log( 'An error happened' );
}
);
}
function onLoad( object ) {
if (orca) scene.remove(orca);
orca = object;
/*
transMat = new THREE.Matrix4()
rotMat = new THREE.Matrix4()
scaleMat = new THREE.Matrix4()
// transMat.makeTranslation(00, 0, 125);
rotMat.makeRotationY( Math.PI / 2);
// rotMat.makeRotationZ( -Math.PI / 2);
scaleMat.makeScale(50, 50, 50)
child.geometry.applyMatrix(rotMat);
child.geometry.applyMatrix(scaleMat);
child.geometry.applyMatrix(transMat);
*/
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
console.log('old material', child.material);
// just for smooth shading
var geo = new THREE.Geometry().fromBufferGeometry( child.geometry );
geo.mergeVertices()
geo.computeVertexNormals()
child.geometry = new THREE.BufferGeometry().fromGeometry( geo );
// modifyShader( child.material );
child.material = customMaterial
child.castShadow = true
// child.material.map = texture;
// child.material.color = 0x000000;
}
} );
geoms = orca.children.map(child => child.geometry);
bbs = geoms.map(geo => {
geo.computeBoundingBox();
return geo.boundingBox;
});
console.log('boundingbox', bbs);
referenceGeometry.vertices[0].set(
Math.min(...bbs.map(bb => bb.min.x)),
Math.min(...bbs.map(bb => bb.min.y)),
Math.min(...bbs.map(bb => bb.min.z))
)
referenceGeometry.vertices[1].set(
Math.max(...bbs.map(bb => bb.max.x)),
Math.max(...bbs.map(bb => bb.max.y)),
Math.max(...bbs.map(bb => bb.max.z))
)
updateModel();
scene.add( object );
}