awacke1 commited on
Commit
ab110b8
·
1 Parent(s): 6e8ecd2

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +56 -62
index.html CHANGED
@@ -1,72 +1,66 @@
1
  <!DOCTYPE html>
2
  <html>
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>DNA Fractal</title>
 
6
  <style>
7
- html, body {
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
- </head>
20
- <body>
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 scene = new BABYLON.Scene(engine);
30
-
31
- // Set up the camera
32
- const camera = new BABYLON.ArcRotateCamera('Camera', 0, 0, 10, BABYLON.Vector3.Zero(), scene);
33
- camera.attachControl(canvas, true);
34
-
35
- // Create the DNA fractal shape
36
- const shape = BABYLON.MeshBuilder.CreateBox('Shape', {size: 1}, scene);
37
- shape.scaling = new BABYLON.Vector3(0.5, 1, 0.5);
38
- shape.rotation = new BABYLON.Vector3(Math.PI / 4, 0, 0);
39
-
40
- // Create the fractal by cloning and rotating the shape
41
- const numLevels = 5;
42
- const angle = Math.PI / 4;
43
- const scale = 0.5;
44
- const fractal = new BABYLON.Mesh('Fractal', scene);
45
- fractal.addChild(shape);
46
- for (let i = 0; i < numLevels; i++) {
47
- const child = shape.clone(`Level ${i}`);
48
- child.scaling = child.scaling.scale(scale);
49
- child.rotation = child.rotation.add(new BABYLON.Vector3(0, angle, 0));
50
- fractal.addChild(child);
51
- shape = child;
52
- }
53
-
54
- // Apply the DNA texture to the fractal
55
- const dnaTexture = new BABYLON.Texture('https://i.imgur.com/f5GcE7Y.jpg', scene);
56
- const material = new BABYLON.StandardMaterial('Material', scene);
57
- material.diffuseTexture = dnaTexture;
58
- fractal.material = material;
59
-
60
- // Start the Babylon engine and render the scene
61
- engine.runRenderLoop(() => {
62
- scene.render();
63
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- // Handle window resizing
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>