-
Notifications
You must be signed in to change notification settings - Fork 0
/
parabolic-004.html
216 lines (177 loc) · 9.89 KB
/
parabolic-004.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
<!doctype html>
<html>
<head>
<meta charset = "utf-8">
<title>BabylonJs - Basic Element-Creating Scene</title>
<script src = "babylon.max.js"></script>
<style>
canvas {width: 100%; height: 100%;}
</style>
</head>
<body>
<table>
<tr>
<td>
Function coefficients:<br>
_X_<input type="range" id = "sliderX" value = "0.2" min = "-2" max = "2" step = "0.01"><span id="Xval"></span><br>
_Z_<input type="range" id = "sliderZ" value = "0.1" min = "-2" max = "2" step = "0.01"><span id="Zval"></span><br>
Range:<br>
_X_<input type="range" id = "sliderXrange" value = "4" min = "1" max = "10" step = "0.5"><span id="XrangeVal"></span><br>
_Z_<input type="range" id = "sliderZrange" value = "4" min = "" max = "10" step = "0.5"><span id="ZrangeVal"></span><br>
Solar position (time):<br>
_H_<input type="range" id = "sliderHour" value = "0" min = "0" max = "360" step = "1"><span id="Hval"></span><br>
</td>
<td width = "800" height="600">
<canvas id = "renderCanvas"></canvas>
</td>
</tr>
</table>
<script type = "text/javascript">
parabolicMirror = null;
mainMaterial = null;
probe = null;
ray = null;
ray2 = null;
ray3 = null;
rayHelperSunToFocus = null;
rayHelperReflected = null;
focusSphere = null;
sunSphere = null;
var canvas = document.getElementById("renderCanvas");
engine = new BABYLON.Engine(canvas, true);
var createScene = function() {
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.ArcRotateCamera("camera1", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
camera.setPosition(new BABYLON.Vector3(4, 10, -25));
camera.attachControl(canvas, true);
camera.lowerRadiusLimit = 4;
var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = 0.7;
// Ground
//var ground = BABYLON.Mesh.CreateGround("ground", 20, 20, 1, scene);
// Axes
var size = 10;
var xAxis = BABYLON.Mesh.CreateLines("xAxis", [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(size, 0, 0),
new BABYLON.Vector3(size * 0.95, 0.05 * size, 0), new BABYLON.Vector3(size, 0, 0),
new BABYLON.Vector3(size * 0.95, -0.05 * size, 0)
], scene);
xAxis.color = new BABYLON.Color3(1, 0, 0); // X = RED
var yAxis = BABYLON.Mesh.CreateLines("yAxis", [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, size, 0),
new BABYLON.Vector3(-0.05 * size, size * 0.95, 0), new BABYLON.Vector3(0, size, 0),
new BABYLON.Vector3(0.05 * size, size * 0.95, 0)
], scene);
yAxis.color = new BABYLON.Color3(0, 1, 0); // Y = Green
var zAxis = BABYLON.Mesh.CreateLines("zAxis", [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, 0, size),
new BABYLON.Vector3(0, -0.05 * size, size * 0.95), new BABYLON.Vector3(0, 0, size),
new BABYLON.Vector3(0, 0.05 * size, size * 0.95)
], scene);
zAxis.color = new BABYLON.Color3(0, 0, 1); // Z = Blue
////////////////////////////////////
function getFunctionPoints(x, y) {
////// Define here any 3d function you want to turn into reflective surface
var z = parseFloat(sliderX.value) * x*x + parseFloat(sliderZ.value) * y*y; // paraboloid
//var y = Math.sin(parseFloat(sliderX.value) * x*x + parseFloat(sliderZ.value) * z*z); // paraboloid
return new BABYLON.Vector3(x, y, z);
}
function buildIt() {
var paths = [];
for (let x = -parseFloat(sliderXrange.value); x < parseFloat(sliderXrange.value); x +=1) {
let path = [];
for (var z = -parseFloat(sliderZrange.value); z <= parseFloat(sliderZrange.value); z += 1) {
var position = getFunctionPoints(x, z);
path.push(new BABYLON.Vector3(position.x, position.y, position.z));
}
paths.push(path);
}
focusSphere = new BABYLON.MeshBuilder.CreateSphere("focus", { diameter: 1 }, scene);
focusSphere.position.x = 0
focusSphere.position.y = 0;
focusSphere.position.z = parseFloat(sliderX.value) / (2 * parseFloat(sliderZ.value));
return (paths);
}
// Setup Sun
var yellowMaterial = new BABYLON.StandardMaterial("yellow", scene);
yellowMaterial.diffuseColor = new BABYLON.Color3(1, 1, 0);
sunSphere = BABYLON.MeshBuilder.CreateSphere("sun", { diameter: 1 }, scene);
sunSphere.position.x = 10; // debug
sunSphere.material = yellowMaterial;
axis = new BABYLON.Vector3(0,1,0).normalize(); // Y axis
scene.registerBeforeRender(function () {
var incl = BABYLON.Tools.ToRadians(45); // Inclinations of Sun rotation axis
var speed = 1; // Used in case Sun movement is automatic
// Rotate Y axis around X by "incl" radians:
var matrix2 = BABYLON.Matrix.RotationAxis(new BABYLON.Vector3(1,0,0).normalize(), incl);
var axis2 = BABYLON.Vector3.TransformCoordinates(axis, matrix2);
var angle = BABYLON.Tools.ToRadians(parseInt(sliderHour.value)); //50 * 1000000000/(new Date()).getTime() * speed;
// Rotate sun around the rotated axis:
var matrix = BABYLON.Matrix.RotationAxis(axis2, angle);
var newPosition = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(0,-15,10), matrix);
sunSphere.position.copyFrom(newPosition); // debug
// Recycle memory:
if (parabolicMirror) { if (typeof parabolicMirror !== "undefined") { parabolicMirror.dispose()}};
if (mainMaterial) {if (typeof mainMaterial !== "undefined") {mainMaterial.dispose()}};
if (probe) {if (typeof probe !== "undefined") {probe.dispose()}};
if (ray) { if (typeof ray !== "undefined") { ray.dispose()}};
if (focusSphere) { if (typeof focusSphere !== "undefined") { focusSphere.dispose()}};
if (typeof intersectionPoint !== "undefined") { intersectionPoin.dispose()};
if (rayHelperSunToFocus) {if (typeof rayHelperSunToFocus !== "undefined") { rayHelperSunToFocus.dispose()}};
//if (typeof rayHelperNormal !== "undefined") { rayHelperNormal.dispose()};
if (rayHelperReflected) { if (typeof rayHelperReflected !== "undefined") { rayHelperReflected.dispose()}};
// Rebuild mirror at each render, beacuse it can be customized by sliders
parabolicMirrorPaths = buildIt();
parabolicMirror = BABYLON.MeshBuilder.CreateRibbon("ribbon", {pathArray: parabolicMirrorPaths, sideOrientation: BABYLON.Mesh.DOUBLESIDE});
var origin = sunSphere.position;
var direction = focusSphere.position.subtract(origin).normalize();
var length = focusSphere.position.subtract( sunSphere.position).length()*2;
var raySunToFocus = new BABYLON.Ray(origin, direction, length);
rayHelperSunToFocus = new BABYLON.RayHelper(raySunToFocus);
rayHelperSunToFocus.show(scene);
try {
var pickInfo = scene.pickWithRay(raySunToFocus, function (mesh) {
return mesh == parabolicMirror;
});
if (pickInfo.hit) {
var intersectionPoint = pickInfo.pickedPoint;
var origin = intersectionPoint.clone();
var direction = parabolicMirror.getFacetNormal(parabolicMirror.getClosestFacetAtCoordinates(intersectionPoint.x,intersectionPoint.y,intersectionPoint.z));
var length = 100;
var rayNormal = new BABYLON.Ray(origin, direction, length);
rayHelperNormal = new BABYLON.RayHelper(rayNormal);
rayHelperNormal.show(scene, new BABYLON.Color3(1, 1, 0.1), 3,1);
reflectedRay = new BABYLON.Ray(intersectionPoint, BABYLON.Vector3.Reflect(raySunToFocus.direction, rayNormal.direction));
rayHelperReflected = new BABYLON.RayHelper(reflectedRay);
rayHelperReflected.show(scene);
} else {
console.log("Nessuna intersezione");
}
} catch(e) {
console.log("Reflection error:", e, intersectionPoint);
}
// Main material
mainMaterial = new BABYLON.StandardMaterial("main", scene);
parabolicMirror.material = mainMaterial;
probe = new BABYLON.ReflectionProbe("main", 512, scene);
probe.attachToMesh(parabolicMirror);
probe.renderList.push(sunSphere); // Add Sun to list of objects to be reflected by mirror
mainMaterial.diffuseColor = new BABYLON.Color3(0.8, 0.8, 1); // Mirror color
mainMaterial.reflectionTexture = probe.cubeTexture;
});
return scene;
};
var scene = createScene();
engine.runRenderLoop(function() {
scene.render();
Xval.innerHTML = sliderX.value;
Zval.innerHTML = sliderZ.value;
XrangeVal.innerHTML = sliderXrange.value;
ZrangeVal.innerHTML = sliderZrange.value;
//rotVal.innerHTML = sliderRot.value;
Hval.innerHTML = sliderHour.value;
//probe.dispose();
});
</script>
</body>
</html>3