-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloth.html
149 lines (124 loc) · 5.97 KB
/
Cloth.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Babylon.js sample code</title>
<!-- Babylon.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.2/dat.gui.min.js"></script>
<script src="https://assets.babylonjs.com/generated/Assets.js"></script>
<script src="https://preview.babylonjs.com/ammo.js"></script>
<script src="https://preview.babylonjs.com/cannon.js"></script>
<script src="https://preview.babylonjs.com/Oimo.js"></script>
<script src="https://preview.babylonjs.com/earcut.min.js"></script>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
<script src="https://preview.babylonjs.com/proceduralTexturesLibrary/babylonjs.proceduralTextures.min.js"></script>
<script src="https://preview.babylonjs.com/postProcessesLibrary/babylonjs.postProcess.min.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.js"></script>
<script src="https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js"></script>
<script src="https://preview.babylonjs.com/gui/babylon.gui.min.js"></script>
<script src="https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js"></script>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script>
var canvas = document.getElementById("renderCanvas");
var startRenderLoop = function (engine, canvas) {
engine.runRenderLoop(function () {
if (sceneToRender && sceneToRender.activeCamera) {
sceneToRender.render();
}
});
}
var engine = null;
var scene = null;
var sceneToRender = null;
var createDefaultEngine = function() { return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true, disableWebGL2Support: false}); };
var createScene = async function () {
// This creates a basic Babylon Scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
//enable physics
await Ammo();
scene.enablePhysics(null, new BABYLON.AmmoJSPlugin());
var camera = new BABYLON.ArcRotateCamera("camera", BABYLON.Tools.ToRadians(-90), BABYLON.Tools.ToRadians(65), 40, BABYLON.Vector3.Zero(), scene);
// This attaches the camera to the canvas
camera.attachControl(canvas, true);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
var light = new BABYLON.DirectionalLight("sun", new BABYLON.Vector3(1, -1, 1), scene);
light.position = new BABYLON.Vector3(0,5,10);
//we allow babylon to automatically calculate the appropriate shadow z values
light.autoCalcShadowZBounds = true;
//create ground floor
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 40, height: 40, depth: 0.1}, scene);
let groundMaterial = new BABYLON.StandardMaterial("Ground Material", scene);
ground.material = groundMaterial;
ground.receiveShadows = true;
//set cloth subdivision and size
var subdivisions = 16;
var clothWidth = 10;
var clothHeight = 10;
//create cloth material
var clothMat = new BABYLON.StandardMaterial("cloth material", scene);
clothMat.diffuseTexture = new BABYLON.Texture("https://raw.githubusercontent.com/mmmovania/BabylonPhysicsTutorials/main/textures/cloth_pattern.png", scene);
clothMat.backFaceCulling = false;
//we create the cloth piece using the CreateGround function
var cloth = BABYLON.MeshBuilder.CreateGround("cloth", { width: clothWidth,
height: clothHeight,
subdivisions: subdivisions,
}, scene);
//place the cloth piece a bit high so its visible to the viewer
cloth.position.y = 12;
cloth.position.z -= clothHeight/2;
cloth.material = clothMat;
//make physics impostor for cloth and assign properties
cloth.physicsImpostor = new BABYLON.PhysicsImpostor(cloth, BABYLON.PhysicsImpostor.ClothImpostor, { mass: 1, damping: 0.006, margin:0.1, fixedPoints:3}, scene);
cloth.physicsImpostor.velocityIterations = 10;
cloth.physicsImpostor.positionIterations = 10;
cloth.physicsImpostor.stiffness = 1;
//make shadow generator
var shadowGenerator = new BABYLON.ShadowGenerator(512, light);
shadowGenerator.addShadowCaster(cloth);
shadowGenerator.setDarkness(0.5);
//make our collision sphere
var collidingSphere = BABYLON.MeshBuilder.CreateSphere("collidingSphere", { diameter: 4, segments: 16 }, scene);
collidingSphere.position.y = 4;
collidingSphere.physicsImpostor = new BABYLON.PhysicsImpostor(collidingSphere, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 0 }, scene);
shadowGenerator.addShadowCaster(collidingSphere);
return scene;
};
window.initFunction = async function() {
var asyncEngineCreation = async function() {
try {
return createDefaultEngine();
} catch(e) {
console.log("the available createEngine function failed. Creating the default engine instead");
return createDefaultEngine();
}
}
window.engine = await asyncEngineCreation();
if (!engine) throw 'engine should not be null.';
startRenderLoop(engine, canvas);
window.scene = createScene();};
initFunction().then(() => {scene.then(returnedScene => { sceneToRender = returnedScene; });
});
// Resize
window.addEventListener("resize", function () {
engine.resize();
});
</script>
</body>
</html>