-
Notifications
You must be signed in to change notification settings - Fork 121
/
test5.html
237 lines (219 loc) · 8.94 KB
/
test5.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Phoria - Dev test page 5</title>
<script src="scripts/gl-matrix.js"></script>
<script src="scripts/phoria-util.js"></script>
<script src="scripts/phoria-entity.js"></script>
<script src="scripts/phoria-scene.js"></script>
<script src="scripts/phoria-renderer.js"></script>
<script src='scripts/dat.gui.min.js'></script>
<script>
var requestAnimFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame || window.msRequestAnimationFrame ||
function(c) {window.setTimeout(c, 15)};
/**
Phoria
pho·ri·a (fôr-)
n. The relative directions of the eyes during binocular fixation on a given object
*/
// bind to window onload event
window.addEventListener('load', onloadHandler, false);
var bitmaps = [];
function onloadHandler()
{
// load the wall texture before anything else
var loader = new Phoria.Preloader();
bitmaps.push(new Image());
loader.addImage(bitmaps[0], 'images/texture-wall.png');
loader.onLoadCallback(init);
}
function init()
{
// generate blob image sprite
var buffer = document.createElement('canvas'),
size = 32,
width = size << 1;
buffer.width = buffer.height = width;
var ctx = buffer.getContext('2d');
// TODO: load image of blob rather than generate it?
var radgrad = ctx.createRadialGradient(size, size, size >> 1, size, size, size);
radgrad.addColorStop(0.7, "rgba(0,64,255,1)");
radgrad.addColorStop(1.0, "rgba(0,64,255,0)");
ctx.fillStyle = radgrad;
ctx.fillRect(0, 0, width, width);
var img = new Image();
img.src = buffer.toDataURL("image/png");
bitmaps.push(img);
// get the canvas DOM element and the 2D drawing context
var canvas = document.getElementById('canvas');
// create the scene and setup camera, perspective and viewport
var scene = new Phoria.Scene();
scene.camera.position = {x:-10, y:12, z:-22.0};
scene.camera.lookat = {x:6, y:4, z:-1};
scene.perspective.aspect = canvas.width / canvas.height;
scene.viewport.width = canvas.width;
scene.viewport.height = canvas.height;
// create a canvas renderer
var renderer = new Phoria.CanvasRenderer(canvas);
// add a grid to help visualise camera position etc.
var plane = Phoria.Util.generateTesselatedPlane(8,8,0,20);
scene.graph.push(Phoria.Entity.create({
points: plane.points,
edges: plane.edges,
style: {
drawmode: "wireframe",
shademode: "plain",
linewidth: 0.5,
objectsortmode: "back"
}
}));
// wall plane object - constructed in-place to avoid rotations of texture coordinates
var wall = Phoria.Entity.create({
points: [{x:10, y:0, z:-5},{x:10, y:10, z:-5},{x:10, y:10, z:5},{x:10, y:0, z:5}],
polygons: [{vertices:[2,1,0,3]}],
style: {
shademode: "plain",
doublesided: true
}
});
wall.textures.push(bitmaps[0]);
wall.polygons[0].texture = 0;
scene.graph.push(wall);
// use offscreen canvas buffer for our wall texture
var TEXTURESIZE = bitmaps[0].width;
buffer.width = buffer.height = TEXTURESIZE;
ctx = buffer.getContext('2d');
ctx.drawImage(bitmaps[0], 0, 0);
// visible object to be parent of the particle emitter
var c = Phoria.Util.generateUnitCube(1);
var cube = Phoria.Entity.create({
points: c.points,
polygons: c.polygons,
style: {
color: [0,64,255],
shademode: "plain"
}
});
cube.translateX(-2);
scene.graph.push(cube);
// liquid particle emitter
var texture = null;
var emitter = Phoria.EmitterEntity.create({
position: {x:0.0, y:0.0, z:0.0},
rate: 20,
velocity: {x:0.12, y:0.2, z:0.0},
velocityRnd: {x:0.02, y:0.07, z:0.07},
lifetime: 5000,
style: {
objectsortmode: "sorted",
shademode: "sprite",
sprite: 0
},
onParticle: function (p) {
// style references the parent emitter config - so clone a new one and modify it
p.style = Phoria.Util.clone(p.style);
p.style.linewidth = 12 + (Math.random()*8 - 4);
},
onScene: function()
{
// if particle impacts the wall then "splat" onto the wall texture
for (var i=0, p; i<this.children.length; i++)
{
p = this.children[i];
if (p._worldcoords && p._worldcoords[0][0] > 10)
{
// apply decal to the texture used by the wall - map particle coordinate to a texture coordinate on the plane
// - the texture size is a hack due to perspectice scaling etc. that
// is applied to the linewidth during rendering of the particle sprite
var size = p.style.linewidth<<2;
// calc the offset given the texture image size and knowing that; the plane is size 10 units centred on Z axis
// and the position of the particle is the centre point of the blob image, then invert the offset as
// canvas draws from top left corner and our coordinate system is from the bottom corner
ctx.drawImage(
bitmaps[1],
TEXTURESIZE-(p._worldcoords[0][2]+5)*(TEXTURESIZE/10)-(size/2),
TEXTURESIZE-(p._worldcoords[0][1])*(TEXTURESIZE/10)-(size/2),
size, size
);
wall.textures[0] = buffer;
// remove particle from scene
this.children.splice(i, 1);
}
}
}
});
emitter.textures.push(bitmaps[1]);
cube.children.push(emitter);
var pause = false;
var fnAnimate = function() {
if (!pause)
{
// execute the model view 3D pipeline and render the scene
scene.modelView();
renderer.render(scene);
}
requestAnimFrame(fnAnimate);
};
// key binding
document.addEventListener('keydown', function(e) {
switch (e.keyCode)
{
case 27:
if (pause) scene._lastTime = Date.now();
pause = !pause;
break;
case 32:
// reset texture image
ctx.drawImage(bitmaps[0], 0, 0);
break;
}
}, false);
// add GUI controls
var gui = new dat.GUI();
var f = gui.addFolder('Perspective');
f.add(scene.perspective, "fov").min(5).max(175);
f.add(scene.perspective, "near").min(1).max(100);
f.add(scene.perspective, "far").min(1).max(1000);
f = gui.addFolder('Camera LookAt');
f.add(scene.camera.lookat, "x").min(-100).max(100);
f.add(scene.camera.lookat, "y").min(-100).max(100);
f.add(scene.camera.lookat, "z").min(-100).max(100);
f = gui.addFolder('Camera Position');
f.add(scene.camera.position, "x").min(-100).max(100);
f.add(scene.camera.position, "y").min(-100).max(100);
f.add(scene.camera.position, "z").min(-100).max(100);
f.open();
f = gui.addFolder('Camera Up');
f.add(scene.camera.up, "x").min(-10).max(10).step(0.1);
f.add(scene.camera.up, "y").min(-10).max(10).step(0.1);
f.add(scene.camera.up, "z").min(-10).max(10).step(0.1);
f = gui.addFolder('Emitter');
f.add(emitter, "rate").min(0).max(250);
f.add(emitter, "lifetime").min(0).max(5000);
f.add(emitter, "lifetimeRnd").min(0).max(5000);
f.add(emitter, "gravity");
f.add(emitter.position, "x").min(-10).max(10).step(0.1).name("position.x");
f.add(emitter.position, "y").min(-10).max(10).step(0.1).name("position.y");
f.add(emitter.position, "z").min(-10).max(10).step(0.1).name("position.z");
f.add(emitter.positionRnd, "x").min(0).max(5).step(0.05).name("positionRnd.x");
f.add(emitter.positionRnd, "y").min(0).max(5).step(0.05).name("positionRnd.y");
f.add(emitter.positionRnd, "z").min(0).max(5).step(0.05).name("positionRnd.z");
f.add(emitter.velocity, "x").min(-1).max(1).step(0.01).name("velocity.x");
f.add(emitter.velocity, "y").min(-1).max(1).step(0.01).name("velocity.y");
f.add(emitter.velocity, "z").min(-1).max(1).step(0.01).name("velocity.z");
f.add(emitter.velocityRnd, "x").min(0).max(1).step(0.01).name("velocityRnd.x");
f.add(emitter.velocityRnd, "y").min(0).max(1).step(0.01).name("velocityRnd.y");
f.add(emitter.velocityRnd, "z").min(0).max(1).step(0.01).name("velocityRnd.z");
f.open();
// start animation
requestAnimFrame(fnAnimate);
}
</script>
</head>
<body style="background-color: #bfbfbf">
<canvas id="canvas" width="768" height="512" style="background-color: #eee"></canvas>
<div><a style="color:#225588;text-decoration:none;" href="./index.html"><< Phoria demos</a><span style="margin-left:8em">Press SPACE to reset wall texture</span></div>
</body>
</html>