-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
254 lines (243 loc) · 8.49 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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>unity-fursharder</title>
</head>
<body>
<h1>unity-fursharder</h1>
<article id="furdepthmap">
<h2>texture generator</h2>
<div>
<canvas
id="furdepthmap-canvas"
:width="canvasSize"
:height="canvasSize"
style="border: 1px solid #ccc; max-width: 256px;"
></canvas>
</div>
<div>
<button type="button" @click="download()">download</button>
<select v-model="canvasSize" :disabled="drawing">
<option v-for="v in options" :key="v" :value="v"> {{ v }} px </option>
</select>
: size<br />
<input
type="range"
min="0.05"
max="20"
step="0.05"
v-model="lineWeigth"
:disabled="drawing"
/>
<input type="number" v-model="lineWeigth" :disabled="drawing" />
: weight<br />
<input
type="range"
min="0.001"
max="1"
step="0.001"
v-model="density"
:disabled="drawing"
/>
<input type="number" v-model="density" :disabled="drawing" />
: density<br />
<input
type="range"
min="1"
max="255"
step="1"
v-model="length"
:disabled="drawing"
/>
<input type="number" v-model="length" :disabled="drawing" />
: length<br />
<input
type="range"
min="-0.5"
max="0.5"
step="0.001"
v-model="gravity.x"
:disabled="drawing"
/>
<input type="number" v-model="gravity.x" :disabled="drawing" />
: gravity.x<br />
<input
type="range"
min="-0.5"
max="0.5"
step="0.001"
v-model="gravity.y"
:disabled="drawing"
/>
<input type="number" v-model="gravity.y" :disabled="drawing" />
: gravity.y<br />
<input
type="range"
min="0"
max="0.5"
step="0.001"
v-model="randomness"
:disabled="drawing"
/>
<input type="number" v-model="randomness" :disabled="drawing" />
: randomness<br />
<button type="button" @click="draw()" v-if="!drawing">
draw
</button>
<button type="button" @click="cansel()" v-else>
cansel
</button>
<progress
:max="progress.total"
:value="progress.success"
v-if="drawing"
>
{{ (progress.success / progress.total) * 100 }}%
</progress>
</div>
<h3>how to use</h3>
<div>
小さいサイズで試した後に大きいサイズで試すと良い
</div>
</article>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function(callback, type, quality) {
var binStr = atob(this.toDataURL(type, quality).split(',')[1]),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
callback(new Blob([arr], { type: type || 'image/png' }));
}
});
}
</script>
<script>
new Vue({
el: '#furdepthmap',
data: {
options: [128, 256, 512, 1024, 2048, 4096],
canvasSize: 128,
drawing: false,
progress: { success: 1, total: 1 },
lineWeigth: 1,
density: 0.1,
length: 20,
gravity: { x: 0, y: 0 },
randomness: 0.1,
canvasId: '#furdepthmap-canvas'
},
methods: {
draw() {
const canvas = document.querySelector(this.canvasId);
const ctx = canvas.getContext('2d');
ctx.globalCompositeOperation = 'source-over';
ctx.fillRect(0, 0, this.canvasSize, this.canvasSize);
ctx.globalCompositeOperation = 'lighter';
const canvasSize = this.canvasSize;
const maxLength = this.length;
const lineWeigth = Number(this.lineWeigth);
const numByOneProc = 1000;
const canvasArea = this.canvasSize * this.canvasSize;
const density = Number(this.density);
const procNum = ((density * canvasArea) / numByOneProc) | 0;
const gravityX = Number(this.gravity.x);
const gravityY = Number(this.gravity.y);
const randomness = Number(this.randomness);
this.drawing = true;
this.progress.total = procNum;
this.progress.success = 0;
return (async () => {
for (let i = 0; i < procNum; i++) {
if (!this.drawing) return;
await new Promise(resolve => {
setTimeout(() => {
for (let k = 0; k < numByOneProc; k++) {
const length =
(Math.random() * maxLength * 0.5 + maxLength * 0.5) | 0;
const gravityDir = Math.random() * 2 * Math.PI;
const gravityPower = Math.random() * randomness;
const gravityForce = {
x: Math.cos(gravityDir) * gravityPower,
y: Math.sin(gravityDir) * gravityPower
};
const pos = {
x: canvasSize * Math.random(),
y: canvasSize * Math.random()
};
const ga = { x: 0, y: 0 };
for (let m = 0; m < length; m++) {
if (pos.x < 0) pos.x += canvasSize;
if (pos.y < 0) pos.y += canvasSize;
if (pos.x > canvasSize) pos.x -= canvasSize;
if (pos.y > canvasSize) pos.y -= canvasSize;
const { x: bx, y: by } = pos;
ga.x += gravityForce.x + gravityX;
ga.y += gravityForce.y + gravityY;
pos.x += ga.x;
pos.y += ga.y;
const { x: ax, y: ay } = pos;
const color8bit = (((m + 1) / length) * 255) | 0;
let color = color8bit.toString(16);
if (color.length === 1) {
color = `0${color}`;
}
ctx.strokeStyle = `#${color}${color}${color}`;
ctx.lineWidth = (1 - m / length) * lineWeigth;
ctx.beginPath();
ctx.moveTo(bx, by);
ctx.lineTo(ax, ay);
// はみ出し処理
if (ax < 0) {
ctx.moveTo(bx + canvasSize, by);
ctx.lineTo(ax + canvasSize, ay);
}
if (ay < 0) {
ctx.moveTo(bx, by + canvasSize);
ctx.lineTo(ax, ay + canvasSize);
}
if (ax > canvasSize) {
ctx.moveTo(bx - canvasSize, by);
ctx.lineTo(ax - canvasSize, ay);
}
if (ay > canvasSize) {
ctx.moveTo(bx, by - canvasSize);
ctx.lineTo(ax, ay - canvasSize);
}
ctx.stroke();
}
}
this.progress.success += 1;
resolve();
});
});
}
})().finally(() => {
this.drawing = false;
});
},
cansel() {
this.drawing = false;
},
download() {
const canvas = document.querySelector(this.canvasId);
canvas.toBlob(blob => {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'fur_texture.png';
link.click();
});
}
}
});
</script>
</body>
</html>