Spaces:
Running
Running
Update index.html
Browse files- index.html +56 -62
index.html
CHANGED
@@ -1,72 +1,66 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
-
|
4 |
-
<meta charset="
|
5 |
-
<title>
|
|
|
6 |
<style>
|
7 |
-
|
8 |
-
width: 100%;
|
9 |
-
height: 100%;
|
10 |
-
margin: 0;
|
11 |
-
padding: 0;
|
12 |
-
overflow: hidden;
|
13 |
-
}
|
14 |
-
canvas {
|
15 |
-
width: 100%;
|
16 |
-
height: 100%;
|
17 |
-
}
|
18 |
</style>
|
19 |
-
|
20 |
-
|
21 |
<canvas id="renderCanvas"></canvas>
|
22 |
-
<script src="https://cdn.babylonjs.com/babylon.js"></script>
|
23 |
-
<script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
|
24 |
<script>
|
25 |
-
window.addEventListener('DOMContentLoaded', () => {
|
26 |
-
// Get the canvas element and create the Babylon scene
|
27 |
const canvas = document.getElementById('renderCanvas');
|
28 |
const engine = new BABYLON.Engine(canvas, true);
|
29 |
-
const
|
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 |
-
window.addEventListener('resize', () => {
|
67 |
-
engine.resize();
|
68 |
-
});
|
69 |
-
});
|
70 |
-
</script>
|
71 |
-
</body>
|
72 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<title>Babylon.js L-system Fractal Example</title>
|
6 |
+
<script src="https://cdn.babylonjs.com/babylon.js"></script>
|
7 |
<style>
|
8 |
+
canvas { width: 100%; height: 100%; touch-action: none; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
</style>
|
10 |
+
</head>
|
11 |
+
<body>
|
12 |
<canvas id="renderCanvas"></canvas>
|
|
|
|
|
13 |
<script>
|
|
|
|
|
14 |
const canvas = document.getElementById('renderCanvas');
|
15 |
const engine = new BABYLON.Engine(canvas, true);
|
16 |
+
const createScene = () => {
|
17 |
+
const scene = new BABYLON.Scene(engine);
|
18 |
+
const camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
|
19 |
+
const light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
|
20 |
+
const texture = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor.png", scene);
|
21 |
+
const bumpMap = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor_n.jpg", scene);
|
22 |
+
const rules = {F: "F+F-F-F+F"};
|
23 |
+
const axiom = "F-F-F-F";
|
24 |
+
const angle = Math.PI / 2, length = 1, iterations = 3;
|
25 |
+
let lSystemString = axiom, position = BABYLON.Vector3.Zero(), direction = BABYLON.Vector3.Forward();
|
26 |
+
for (let i = 0; i < iterations; i++) {
|
27 |
+
let newString = "";
|
28 |
+
for (let j = 0; j < lSystemString.length; j++) {
|
29 |
+
const char = lSystemString.charAt(j);
|
30 |
+
newString += rules[char] || char;
|
31 |
+
}
|
32 |
+
lSystemString = newString;
|
33 |
+
}
|
34 |
+
for (let i = 0; i < lSystemString.length; i++) {
|
35 |
+
const char = lSystemString.charAt(i);
|
36 |
+
if (char === "F") {
|
37 |
+
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter: length, segments: 16 }, scene);
|
38 |
+
sphere.position = position.clone();
|
39 |
+
sphere.material = new BABYLON.StandardMaterial("texture", scene);
|
40 |
+
sphere.material.diffuseTexture = texture;
|
41 |
+
sphere.material.bumpTexture = bumpMap;
|
42 |
+
const textureAnimation = new BABYLON.Animation("textureAnimation", "material.diffuseTexture.uOffset", 60, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE, true);
|
43 |
+
textureAnimation.setKeys([{ frame: 0, value: 0 }, { frame: 100, value: 1 }]);
|
44 |
+
sphere.material.diffuseTexture.wrapU = sphere.material.diffuseTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
|
45 |
+
sphere.material.diffuseTexture.uScale = sphere.material.diffuseTexture.vScale = 10;
|
46 |
+
sphere.animations.push(textureAnimation);
|
47 |
+
scene.beginAnimation(sphere, 0, 100, true);
|
48 |
+
const animation = new BABYLON.Animation("myAnimation", "position", 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE, true);
|
49 |
+
animation.setKeys([{ frame: 0, value: position.clone() }, { frame: 100, value: position.addInPlace(direction.scale(length)).clone() }]);
|
50 |
+
sphere.animations.push(animation);
|
51 |
+
scene.beginAnimation(sphere, 0, 100, true);
|
52 |
+
} else if (char === "+") {
|
53 |
+
direction.rotateByQuaternionToRef(BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.Up(), -angle), direction);
|
54 |
+
} else if (char === "-") {
|
55 |
+
direction.rotateByQuaternionToRef(BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.Up(), angle), direction);
|
56 |
+
}
|
57 |
+
}
|
58 |
+
return scene;
|
59 |
+
};
|
60 |
+
const scene = createScene();
|
61 |
+
engine.runRenderLoop(() => scene.render());
|
62 |
+
window.addEventListener('resize', () => engine.resize());
|
63 |
+
</script>
|
64 |
|
65 |
+
</body>
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
</html>
|