Taking help of this example from pre3d js lib, I am trying to create a cuboid container which will contain smaller cuboids inside.
Now, what I'm not getting properly, is how to place the internal cuboid in a predefined co-ordinate. What will be the generalized formula here for transform2.translate
? This formula should work for any cuboid I want to place inside.
The following is what I've done till now. If you put this two files inside demos directory, they should work instantly.
// experiment.js
window.addEventListener('load', function() {
var screen_canvas = document.getElementById('canvas');
var renderer = new Pre3d.Renderer(screen_canvas);
var transform1 = new Pre3d.Transform();
transform1.translate(-0.5, -0.5, -0.5);
var transform2 = new Pre3d.Transform();
// **** This is where i need your help ****//
transform2.translate(10 - 10/2, 10 - 10/2 - 2, 10 - 10/2);
// **** **** **** ****//
var cubes = [
{ //container
shape: Pre3d.ShapeUtils.makeBox(10, 10, 10),
color: new Pre3d.RGBA(9 / 10, 9 / 10, 9 / 10, 0.3),
trans: transform1
},
{ //axis
shape: Pre3d.ShapeUtils.makeBox(10, 0.01, 0.01),
color: new Pre3d.RGBA(0, 0, 0, 0.3),
trans: transform1
},
{//axis
shape: Pre3d.ShapeUtils.makeBox(0.01, 10, 0.01),
color: new Pre3d.RGBA(0, 0, 0, 0.3),
trans: transform1
},
{//axis
shape: Pre3d.ShapeUtils.makeBox(0.01, 0.01, 10),
color: new Pre3d.RGBA(0, 0, 0, 0.3),
trans: transform1
},
{
shape: Pre3d.ShapeUtils.makeBox(10.0, 2, 2),
color: new Pre3d.RGBA(1.2, 0, 0, 0.3),
trans: transform2
}
];
var num_cubes = cubes.length;
var cur_white = false; // Default to black background.
function draw() {
for (var i = 0; i < num_cubes; ++i) {
cube = cubes[i];
renderer.fill_rgba = cube.color;
renderer.transform = cube.trans;
renderer.bufferShape(cube.shape);
}
renderer.ctx.setFillColor(1, 1, 1, 1);
renderer.drawBackground();
renderer.drawBuffer();
renderer.emptyBuffer();
}
renderer.camera.focal_length = 1.5;
DemoUtils.autoCamera(renderer, 0, 0, -30, 0, 0, 0, draw);
draw();
}, false);
Sample HTML goes here:
// experiment.html
<html>
<head>
<title>experiment Front end</title>
<style>
body * {
font-family: sans-serif;
font-size: 14px;
}
body.white {
background-color: white;
color: black;
}
body.black {
background-color: black;
color: white;
}
span.spaceyspan { margin-right: 20px; }
div.centeredDiv { text-align: center; }
li { list-style: none; }
td { padding-right: 10px; }
</style>
<script src="../pre3d.js"></script>
<script src="../pre3d_shape_utils.js"></script>
<script src="demo_utils.js"></script>
<script src="experiment.js"></script>
</head>
<body>
<div class="centeredDiv">
<canvas id="canvas" width="800" height="600">
Sorry, this demo requires a web browser which supports HTML5 canvas!
</canvas>
</div>
</body>
</html>
Use a concentric constraint to make this work. A concentric constraint makes two concentric geometries concentric with respect to each other. Here is the process:
References
Add a Concentric Constraint
Constraints 3D