-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMappable.java
383 lines (307 loc) · 10.5 KB
/
Mappable.java
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
_________ ____ .-. _________/ ____ .-. ____
__|__ (_)_/(_)( )____< \| ( ) (_)
`-' `-'
art & game engines
____________________________________ ? ____________________________________
(._.)
This file is part of FlatMapper
For the latest info, see http://polymorph.cool/
Copyright (c) 2018 polymorph.cool
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
___________________________________( ^3^)_____________________________________
ascii font: rotated by MikeChat & myflix
have fun and be cool :)
*/
import processing.core.PVector;
import processing.core.PApplet;
import processing.core.PImage;
import processing.data.JSONObject;
import processing.data.JSONArray;
import java.util.ArrayList;
import oscP5.*;
public class Mappable implements java.io.Serializable {
public static final int MESH_SUBDIVIDE = 5;
public static final int DELTA_DEFAULT = 0;
public static final int DELTA_LEFT = 1;
public static final int DELTA_RIGHT = 2;
protected transient FlatMapper parent;
protected transient PImage texture;
/* array of 4 2D PVector (x:u,y:v) storing the location of
* the corners of the texture uv - generated in clockwise order,
* starting from top left, normalised between 0 and 1
*/
protected PVector[] uv_quad;
protected transient float[][] geom_uv;
protected PVector selected;
protected String osc_address;
protected String texture_path;
protected float[] rgba;
protected int msubdivide;
protected int msubdivide_request;
protected boolean display;
protected boolean edited;
protected boolean debugged;
protected int texture_warp;
public JSONObject json() {
JSONObject data = new JSONObject();
data.setString("type", "Mappable" );
data.setJSONArray("uv_quad", FlatMap.obj2json( uv_quad ) );
data.setJSONObject("rgba", FlatMap.obj2json( rgba ) );
data.setString("osc_address", osc_address );
data.setString("texture_path", texture_path );
data.setInt("msubdivide", msubdivide );
data.setBoolean("display", display );
return data;
}
public boolean json( JSONObject data ) {
uv_quad = FlatMap.json2pvector_array( data.getJSONArray( "uv_quad" ) );
rgba = FlatMap.json2float_array( data.getJSONObject( "rgba" ) );
osc_address = data.getString( "osc_address" );
texture_path = data.getString( "texture_path" );
msubdivide = data.getInt( "msubdivide" );
msubdivide_request = msubdivide;
display = data.getBoolean( "display" );
return true;
}
public Mappable() {
parent = null;
texture = null;
geom_uv = null;
selected = null;
osc_address = null;
texture_path = null;
uv_quad = new PVector[4];
uv_quad[0] = new PVector( 0,0 ); // top left
uv_quad[1] = new PVector( 1,0 ); // top right
uv_quad[2] = new PVector( 1,1 ); // bottom right
uv_quad[3] = new PVector( 0,1 ); // bottom left
rgba = new float[] {1,1,1,1};
display = true;
edited = false;
debugged = false;
msubdivide = MESH_SUBDIVIDE;
msubdivide_request = MESH_SUBDIVIDE;
texture_warp = 0;
}
public void reset() {
texture = null;
selected = null;
geom_uv = null;
}
public PVector get_uv( int i ) {
return uv_quad[i];
}
public void set_uv( int i, float x, float y ) {
uv_quad[i].set( x,y,0 );
generate_geometry();
}
public PVector get_vertex( int i ) {
System.out.println( "Mappable.get_vertex(i), overload to disable this comment... " + i );
return null;
}
public void set_vertex( int i, float x, float y ) {
set_vertex( i, new PVector( x,y,0 ) );
}
public void set_vertex( int i, float x, float y, float z ) {
set_vertex( i, new PVector( x,y,z ) );
}
public void set_vertex( int i, PVector v ) {
System.out.println( "Mappable.set_vertex(int, PVector), overload to disable this comment... " + i + " : " + v );
}
public int get_subdivision() {
return msubdivide;
}
public void set_subdivision( int i ) {
if ( i > 0 ) {
msubdivide_request = i;
}
}
public void load_texture() {
if ( parent == null ) return;
if ( texture_path.equals("") ) {
texture = null;
return;
}
texture = parent.get_texture( texture_path );
}
public PImage get_texture() {
return texture;
}
public String get_texture_path() {
if ( texture_path == null ) texture_path = "";
return texture_path;
}
public void set_texture_path( String s ) {
texture_path = s;
}
public float get_rgba( int i ) {
return rgba[i];
}
public void set_rgba( int i, float f ) {
rgba[i] = f;
}
public float get_opacity() {
return rgba[3];
}
public void set_opacity( float o ) {
rgba[3] = o;
}
public void set_parent( FlatMapper p ) {
if ( parent == null ) {
parent = p;
update_vectors();
generate_geometry();
load_texture();
}
}
public String get_osc_address() {
if ( osc_address == null ) osc_address = "/M";
return osc_address;
}
public void set_osc_address( String s ) {
osc_address = s;
}
public void set_display( boolean enable ) {
display = enable;
}
public boolean is_debug() {
return debugged;
}
public void enable_debug( boolean enable ) {
debugged = enable;
}
public boolean is_edit() {
return edited;
}
public void enable_edit( boolean enable ) {
edited = enable;
}
protected void update_vectors() {
System.out.println( "Mappable.update_vectors(), overload to disable this comment..." );
}
protected void generate_geometry() {
System.out.println( "Mappable.generate_geometry(), overload to disable this comment..." );
}
public void tri() {
if (
parent == null ||
geom_uv == null ||
( !display && parent.editmappable != this )
) { return; }
if ( msubdivide != msubdivide_request ) {
msubdivide = msubdivide_request;
generate_geometry();
}
parent.noStroke();
if ( texture != null ) {
parent.tint( 255*rgba[0], 255*rgba[1], 255*rgba[2], 255*rgba[3] );
parent.texture(texture);
} else {
parent.fill( 255*rgba[0], 255*rgba[1], 255*rgba[2], 255*rgba[3] );
}
if ( edited && debugged ) {
parent.stroke( parent.frameCount % 255 );
}
parent.beginShape( parent.TRIANGLES );
if ( texture != null ) {
parent.texture(texture);
parent.textureWrap(texture_warp);
}
int len = geom_uv.length;
for( int i = 0; i < len; ++i ) {
parent.vertex( geom_uv[i][0],geom_uv[i][1],geom_uv[i][2],geom_uv[i][3],geom_uv[i][4] );
}
parent.endShape();
if ( texture != null ) {
parent.noTint();
}
}
public void edit() {
System.out.println( "Mappable.edit(), overload to disable this comment..." );
}
public void drag( float x, float y ) {
if ( selected == null ) {
return;
}
selected.x = x;
selected.y = y;
update_vectors();
generate_geometry();
}
public PVector hit( int mx, int my ) {
System.out.println( "Mappable.hit(int,int), overload to disable this comment..." );
return null;
}
public void parse(OscMessage msg) {
if ( !msg.checkAddrPattern(osc_address) ) return;
byte[] types = msg.getTypetagAsBytes();
int argnum = types.length;
try {
boolean parsed = true;
for( int i = 0; i < argnum; ++i ) {
if ( types[i] == 's' ) {
String argname = msg.get(i).stringValue();
if ( argname.equals("opacity") ) {
++i; rgba[3] = msg.get(i).floatValue();
} else if ( argname.equals("red") ) {
++i; rgba[0] = msg.get(i).floatValue();
} else if ( argname.equals("green") ) {
++i; rgba[1] = msg.get(i).floatValue();
} else if ( argname.equals("blue") ) {
++i; rgba[2] = msg.get(i).floatValue();
} else if ( argname.equals("rgb") ) {
++i; rgba[0] = msg.get(i).floatValue();
++i; rgba[1] = msg.get(i).floatValue();
++i; rgba[2] = msg.get(i).floatValue();
} else if ( argname.equals("rgba") ) {
++i; rgba[0] = msg.get(i).floatValue();
++i; rgba[1] = msg.get(i).floatValue();
++i; rgba[2] = msg.get(i).floatValue();
++i; rgba[3] = msg.get(i).floatValue();
} else if ( argname.equals("pos") ) {
++i; int posi = msg.get(i).intValue();
++i; float x = msg.get(i).floatValue();
++i; float y = msg.get(i).floatValue();
set_vertex( posi, x, y );
} else if ( argname.equals("div") ) {
++i; set_subdivision( msg.get(i).intValue() );
} else if ( argname.equals("tex") ) {
++i; set_texture_path( msg.get(i).stringValue() );
load_texture();
} else if ( argname.equals("osc") ) {
++i; set_osc_address( msg.get(i).stringValue() );
} else if ( argname.equals("hide") ) {
set_display( false );
} else if ( argname.equals("show") ) {
set_display( true );
} else {
parsed = false;
}
}
}
if ( !parsed ) {
parse_custom( msg );
}
} catch( Exception e ) {
e.printStackTrace();
}
}
protected void parse_custom(OscMessage msg) {
System.out.println( "Mappable.parse_custom(msg), overload to disable this comment... " + msg.addrPattern() );
}
}