-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
330 lines (283 loc) · 9.79 KB
/
index.html
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
<html>
<head>
<title>Minecraft Block Renderer</title>
<link rel="shortcut icon" href="/favicon.ico" />
<script type='text/javascript' src="https://stats.herbix.me/js/fs.js.php?site_id=4"></script>
<script type="text/vsh" id="drawv">
attribute vec3 position;
attribute vec3 normal;
attribute vec2 textureCoord;
uniform mat4 mvMatrix;
uniform mat4 rtMatrix;
uniform mat4 pMatrix;
varying vec3 vNormal;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = pMatrix * mvMatrix * rtMatrix * vec4(position, 1.0);
vNormal = normalize((rtMatrix * vec4(normal, 1.0)).xyz);
vTextureCoord = textureCoord;
}
</script>
<script type="text/fsh" id="drawf">
precision mediump float;
uniform sampler2D texture;
uniform vec3 backColor;
varying vec3 vNormal;
varying vec2 vTextureCoord;
void main(void) {
vec4 color = texture2D(texture, vTextureCoord.st);
vec3 normal = normalize(vNormal);
vec3 light = normalize(vec3(-1, 1.732, 1));
float lightWeight = dot(normal, light);
if(lightWeight < 0.0)
lightWeight = 0.0;
lightWeight = 0.5 + 0.5 * lightWeight;
gl_FragColor = vec4(backColor * color.rgb * lightWeight, color.a);
}
</script>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript" src="Oak3D_v_0_5_9.js"></script>
<script type="text/javascript" src="blockModel.js"></script>
<script type="text/javascript" src="gllib.js"></script>
<script type="text/javascript" src="presets.js"></script>
<script type="text/javascript">
var gl;
var currentTop; // For the downloaded file's name
function load() {
loadPresets();
loadModels();
loadSaved();
gl = loadGl('canvas');
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LESS);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.BACK);
gl.backColor = {};
var colorstr = $.cookie("colorstr");
if(colorstr == undefined) {
colorstr = "FFFFFF";
}
$("#color-input").val(colorstr);
$("#color-input").parent().children('span').css('background','#'+colorstr);
changeColor(colorstr);
var done = 0;
loadShader(gl, "#drawv", "#drawf", function(sp) {
gl.dsp = sp;
gl.linkProgram(sp);
if (!gl.getProgramParameter(sp, gl.LINK_STATUS)) {
alert("Could not link shaders: \n" + gl.getProgramInfoLog(sp));
return;
}
sp.position = gl.getAttribLocation(sp, "position");
sp.textureCoord = gl.getAttribLocation(sp, "textureCoord");
sp.normal = gl.getAttribLocation(sp, "normal");
sp.mvMatrix = gl.getUniformLocation(sp, "mvMatrix");
sp.rtMatrix = gl.getUniformLocation(sp, "rtMatrix");
sp.pMatrix = gl.getUniformLocation(sp, "pMatrix");
sp.texture = gl.getUniformLocation(sp, "texture");
sp.backColor = gl.getUniformLocation(sp, "backColor");
done |= 1;
doneThen(done, 3, gl);
});
var themodel = models[$(".models").val()];
if(!themodel) {
themodel = models.cube;
}
themodel.textures = defaultTextures;
loadModel(gl, themodel, function(lm) {
gl.model = lm;
themodel.textures = [];
done |= 2;
doneThen(done, 3, gl);
});
}
function doneThen(done, max, gl) {
if(done != max)
return;
var scene = {};
scene.mvMat = okMat4Trans(0, -8*1.732/2, 0).toArray();
scene.rtMat = okMat4RotY(45).rotX(OAK.SPACE_WORLD, 30).toArray();
scene.pMat = okMat4Ortho(-13, 13, 13, -13, -20, 20).toArray();
gl.scene = scene;
draw(scene, gl);
}
function draw(scene, gl) {
var sp2 = gl.dsp;
gl.useProgram(sp2);
drawModel(gl, gl.model, {
position: sp2.position,
normal: sp2.normal,
textureCoord: sp2.textureCoord,
texture: sp2.texture,
otherActions: function(gl, texture, polyhedron) {
var sp = gl.dsp;
gl.uniform3f(sp.backColor, gl.backColor.r, gl.backColor.g, gl.backColor.b);
gl.uniformMatrix4fv(sp.pMatrix, false, scene.pMat);
gl.uniformMatrix4fv(sp.rtMatrix, false, scene.rtMat);
gl.uniformMatrix4fv(sp.mvMatrix, false, scene.mvMat);
}
});
}
function loadPresets() {
var optstr = "";
for(var i=0; i<presets.length; i++) {
optstr += "<option value='" + presets[i] + "'" + (presets[i] == "bedrock" ? "selected='true'" : "") + ">" + presets[i] + "</option>";
}
$(".presets").html(optstr);
}
function loadModels() {
var optstr = "";
for(var k in models) {
optstr += "<option value='" + k + "'>" + k + "</option>";
}
$(".models").html(optstr);
}
function loadSaved() {
if($.cookie("resultsize"))
$("#size").val($.cookie("resultsize"));
if($.cookie("selectedmodel"))
$(".models").val($.cookie("selectedmodel"));
}
function changeModel(modelname) {
$.cookie("selectedmodel", modelname);
loadModel(gl, models[modelname], function(lm) {
lm.textures = gl.model.textures;
gl.model = lm;
draw(gl.scene, gl);
});
}
function changeColor(colorstr, repaint) {
$.cookie("colorstr", colorstr);
if(colorstr.length == 3) {
gl.backColor.r = parseInt(colorstr[0], 16) / 15;
gl.backColor.g = parseInt(colorstr[1], 16) / 15;
gl.backColor.b = parseInt(colorstr[2], 16) / 15;
} else if(colorstr.length == 6) {
gl.backColor.r = parseInt(colorstr.substring(0, 2), 16) / 255;
gl.backColor.g = parseInt(colorstr.substring(2, 4), 16) / 255;
gl.backColor.b = parseInt(colorstr.substring(4, 6), 16) / 255;
}
if(repaint) {
draw(gl.scene, gl);
}
}
function changeTextureFromFile(o, i) {
if(o.files.length < 0) {
return;
}
var file = o.files[0];
var reader = new FileReader();
reader.onloadend = function() {
if (reader.error) {
alert("Error reading file.");
} else {
var s = reader.result;
loadTexture(gl, {image:s, type:"solid", minFilter:"nearest", magFilter:"nearest"}, function(tx) {
gl.model.textures[i] = tx;
draw(gl.scene, gl);
});
}
}
reader.readAsDataURL(file);
}
function changeTextureFromPresets(o, i) {
var s = "presets/" + $(o).val() + ".png";
currentTop = $(o).val();
loadTexture(gl, {image:s, type:"solid", minFilter:"nearest", magFilter:"nearest"}, function(tx) {
gl.model.textures[i] = tx;
draw(gl.scene, gl);
});
}
function changeTextureFromOther(i, f) {
gl.model.textures[i] = gl.model.textures[f];
draw(gl.scene, gl);
}
function getCanvasData() {
var canvas = $("#canvas");
var size = $("#size").val();
canvas.attr("width", 32);
canvas.attr("height", 32);
draw(gl.scene, gl);
var result = canvas.get(0).toDataURL("image/png");
canvas.attr("width", 32);
canvas.attr("height", 32);
draw(gl.scene, gl);
return result;
}
function shouldAddModelNameToFile(name) {
let whitelist = ["stairs", "slab", "fence", "wall"];
return whitelist.includes(name);
}
function output() {
$("#result").get(0).href = getCanvasData();
let currModel = $(".models").val();
let currTop = $(".presets").val();
$("#result").get(0).download = `${currTop}${(shouldAddModelNameToFile(currModel)) ? `_${currModel}` : ""}.png`;
}
function outputImage() {
$('#result-img').get(0).src = getCanvasData();
}
</script>
</head>
<body>
<h1>Render minecraft blocks using WebGL.</h1>
<canvas id="canvas" width="32" height="32" style="border:1px solid ccc;">
Your browser doesn't support WebGL, or you need to enable it.
</canvas>
<p>All these textures' width and height must be power of 2 (e.g. 16, 32, 64, ...).</p>
<table>
<tr>
<td>Model:</td>
<td><select class="models" onchange="changeModel($(this).val())"></select></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Color:</td>
<td>
<input type="input" id="color-input" value="FFFFFF" onkeyup="$(this).parent().children('span').css('background','#'+$(this).val())"/>
<span style="display:block;float:left;width:20px;border:1px solid black;margin-top:3px"> </span>
</td>
<td><a href="javascript:;" onclick="changeColor($('#color-input').val(),true)">Change Color</a></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Top Side:</td>
<td><select class="presets" onchange="changeTextureFromPresets(this, 0);"></select></td>
<td><input type="file" onchange="changeTextureFromFile(this, 0);" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Left Side:</td>
<td><select class="presets" onchange="changeTextureFromPresets(this, 1);"></select></td>
<td><input type="file" onchange="changeTextureFromFile(this, 1);" /></td>
<td><a href="javascript:;" onclick="changeTextureFromOther(1, 0);" >Same as Top</a></td>
<td> </td>
</tr>
<tr>
<td>Right Side:</td>
<td><select class="presets" onchange="changeTextureFromPresets(this, 2);" ></select></td>
<td><input type="file" onchange="changeTextureFromFile(this, 2);" /></td>
<td><a href="javascript:;" onclick="changeTextureFromOther(2, 0);" >Same as Top</a></td>
<td><a href="javascript:;" onclick="changeTextureFromOther(2, 1);" >Same as Left</a></td>
</tr>
</table>
<p>
<a id="result" download="result.png" onclick="output()" href="javascript:;" target="__blank" type="application/octet-stream" >Save Result</a>
</p>
<p><img id="result-img" /></p>
<hr />
<p>Original Author: Chaos <a href="http://static.chaofan.io/blockRenderer/" target="_blank">link</a></p>
<p>Modified by FlamingoBike#6228</p>
<script type="text/javascript">
try{load();}catch(e){alert(e.message);}
</script>
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1000435278'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/z_stat.php%3Fid%3D1000435278%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
</body>
</html>