-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_inline.html
199 lines (191 loc) · 4.13 KB
/
sample_inline.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>WWG (inline canvas)</title>
<script>
</script>
<style>
body {
text-align:center ;
}
div.bgcanvas {
position:relative ;
}
div.bgcanvas div {
position:absolute ;
width:100%;
}
</style>
</head>
<body>
<h1>Inline Sample</h1>
<div>
<script src="js/WWG.js"></script>
<script src="js/CanvasMatrix.js"></script>
<script src="js/WWModel.js"></script>
<script>
(function() {
// create canvas
var can = document.createElement("canvas") ;
can.width = 200 ;
can.height = 200 ;
document.currentScript.parentNode.appendChild(can) ;
var render = {
env:{clear_color:[1.0,1.0,1.0,0.0]},
vshader:{
text:[
"uniform mat4 mvpMatrix;",
"attribute vec3 position;",
"attribute vec3 norm;",
"attribute vec2 uv;",
"varying vec3 pcolor;",
"void main() { ",
" pcolor = (norm+1.0)/2.0; uv;",
" gl_Position = mvpMatrix * vec4(position, 1.0) ;",
"}"
].join("\n")
},
fshader:{
text:[
"precision mediump float;",
"varying vec3 pcolor;",
"void main() {",
" gl_FragColor = vec4(pcolor,1.0) ;",
"}"
].join("\n")
},
vs_uni:{
},
model:[
{geo:new WWModel().primitive("box").objModel()}
]
}
var wwg = new WWG() ;
if(!wwg.init(can)) {
var ct = can.getContext("2d") ;
ct.font = "16px Sans-serif" ;
ct.fillText("not support",can.width/2-40,can.height/2) ;
return ;
}
//create render unit
var r = wwg.createRender() ;
//parse render data
r.setRender(render).then(function() {
console.log(r) ;
//draw loop
var rotY = 0 ;
var vpm = new CanvasMatrix4().
rotate(30,1,0,0).
translate(0,0,-5).
perspective(40,1.0,0.1,10)
var lt = new Date().getTime() ;
(function loop(){
window.requestAnimationFrame(loop) ;
var ct = new Date().getTime() ;
var tint = ct - lt ;
if(tint<1000/60) return ;
lt = ct ;
rotY += tint/20 ;
r.draw({vs_uni:{mvpMatrix:new CanvasMatrix4().
rotate(rotY,1,1,0).multRight(vpm).getAsWebGLFloatArray()}}) ;
// console.log(Math.floor(1000/(ct - lt)+0.5)) ;
})();
}).catch(function(err){
console.log(err) ;
})
})()
</script>
</div>
<div>
inline canvas generated
</div>
<h2>another webGL canvas</h2>
<div class=bgcanvas>
<div>
<br/><br/>
bakground canvas<br/>
overwrapped!
</div>
<script>
(function() {
// create canvas
var can = document.createElement("canvas") ;
can.width = 200 ;
can.height = 200 ;
document.currentScript.parentNode.appendChild(can) ;
var render = {
env:{clear_color:[1.0,1.0,1.0,0.0]},
vshader:{
text:[
"uniform mat4 mvpMatrix;",
"uniform vec3 col;",
"attribute vec3 position;",
"attribute vec3 norm;",
"attribute vec2 uv;",
"varying vec3 pcolor;",
"void main() { ",
" norm;",
" float dx = mod((uv.x+0.01)*20.0,1.0) ;",
" float t = (dx<0.5)?1.0:0.0 ;",
" pcolor = vec3(col.x*t,col.y*t,col.z*t) ;",
" gl_Position = mvpMatrix * vec4(position, 1.0) ;",
"}"
].join("\n")
},
fshader:{
text:[
"precision mediump float;",
"varying vec3 pcolor;",
"void main() {",
" gl_FragColor = vec4(pcolor,1.0) ;",
"}"
].join("\n")
},
vs_uni:{
col:[1.0,0.5,1.0]
},
model:[
{geo:new WWModel().primitive("sphere",{wx:0.5,wy:0.5,wz:0.5,div:20}).objModel()}
]
}
var wwg = new WWG() ;
if(!wwg.init(can)) {
var ct = can.getContext("2d") ;
ct.font = "16px Sans-serif" ;
ct.fillText("not support",can.width/2-40,can.height/2) ;
return ;
}
//create render unit
var r = wwg.createRender() ;
//parse render data
r.setRender(render).then(function() {
console.log(r) ;
//draw loop
var rotY = 0 ;
var vpm = new CanvasMatrix4().
rotate(40,1,0,0).
translate(0,0,-6).
perspective(40,1.0,0.1,10)
var lt = new Date().getTime() ;
(function loop(){
window.requestAnimationFrame(loop) ;
var ct = new Date().getTime() ;
var tint = ct - lt ;
if(tint<1000/60) return ;
lt = ct ;
rotY += tint/20 ;
var t = Math.PI*rotY/180 ;
r.draw({vs_uni:{mvpMatrix:new CanvasMatrix4().
translate(Math.sin(t*3),Math.cos(t*2),Math.cos(t)).
multRight(vpm).getAsWebGLFloatArray()}}) ;
// console.log(Math.floor(1000/(ct - lt)+0.5)) ;
})();
}).catch(function(err){
console.log(err) ;
})
})()
</script>
</div>
</body>