-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_cantex.html
139 lines (125 loc) · 3.08 KB
/
sample_cantex.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WWG (canvas texture)</title>
<script src="js/WWG.js"></script>
<script src="js/CanvasMatrix.js"></script>
<script>
onload = function() {
var wwg = new WWG() ;
if(!wwg.init(document.getElementById('screen1'))) {
alert("not supported") ;
return ;
}
var texcanvas = document.getElementById('texcanvas') ;
// draw on canvas
var ctx = texcanvas.getContext("2d") ;
ctx.lineWidth = 4 ;
ctx.fillStyle = "rgba(0,0,0,0)"
ctx.fillRect(0,0,400,400)
ctx.strokeStyle = "rgba(0,0,0,255)"
ctx.beginPath();
ctx.moveTo(10,100) ;
ctx.lineTo(500,500) ;
ctx.lineTo(10,500);
ctx.stroke() ;
ctx.fillStyle = "rgba(255,0,0,255)"
ctx.font = "50px SourceHanSans-Regular" ;
ctx.fillText("テキストを描きます",10,100);
var render = {
env:{clear_color:[0.5,0.5,0.6,1.0]},
vshader:{text: document.getElementById('vshader').innerText},
fshader:{text: document.getElementById('fshader').innerText},
vs_uni:{
mvpMatrix:[0.5,0,0,0, 0,0.5,0,0, 0,0,1,0, 0,0,0,1],
},
texture:[
{canvas:texcanvas,opt:{flevel:2,anisotropy:4}}
],
model:[
{ //model1
geo:{mode:"tri_strip",
vtx_at:["position","uv"],
vtx:[
1,1,0,//pos
1.0,1.0,//uv
1,-1,0,
1.0,0,
-1,1,0,
0,1.0,
-1,-1,0,
0,0
],
idx:[0,1,2,3]},
fs_uni:{
tex1:0
},
}
]
}
//create render unit
render.model[0].preFunction = function(gl,m) {
gl.enable(gl.BLEND) ;
gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA)
// gl.blendFuncSeparate(gl.SRC_COLOR, gl.DST_COLOR, gl.ONE, gl.ONE);
// gl.blendColor(1.0,1.0,1.0,0.3) ;
}
var r = wwg.createRender() ;
//parse render data
r.setRender(render).then(function() {
drawloop() ;
}).catch(function(err){
console.log(err) ;
})
function drawloop() {
var st = new Date().getTime() ;
var lt = st ;
(function loop(){
window.requestAnimationFrame(loop) ;
var ct = new Date().getTime() ;
var tint = (ct - st) ;
if((ct - lt)<60) return ;
r.draw({vs_uni:{mvpMatrix:new CanvasMatrix4().
rotate(tint/20-90,0,1,0).
lookat(0,0,5, 0,0,0, 0,1,0).
perspective(40,1.0, 0.1, 1000).getAsWebGLFloatArray()}}
) ;
// console.log(Math.floor(1000/(ct - lt)+0.5)) ;
lt = ct ;
})();
}
} //onload
</script>
<! --------------- Vertex Shader ---------------- ->
<script id="vshader" type="x-shader/x-vertex">
uniform mat4 mvpMatrix;
attribute vec3 position;
attribute vec2 uv;
varying vec2 texCoord;
void main() {
texCoord = vec2(uv.x,1.0-uv.y);
gl_Position = mvpMatrix * vec4(position, 1.0) ;
}
</script>
<! --------------- Fragment Shader ---------------- ->
<script id="fshader" type="x-shader/x-fragment">
precision highp float;
uniform sampler2D tex1;
varying vec2 texCoord;
void main() {
gl_FragColor = texture2D(tex1, texCoord);
}
</script>
<style>
body {
}
#texcanvas {
background-color:none;
font-size:100px ;
}
</style>
</head>
<body>
<canvas id="screen1" width="400px" height="400px"></canvas>
<canvas id="texcanvas" width="512px" height="512px"></canvas>