JSFiddle - React, Tailwind, and code Playground
by realhunts
HTML
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
JavaScript
// a basic three.js scene
var container, renderer, scene, camera, controls;
init();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer({
antialias: false
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xccccff);
container = document.createElement('div');
document.body.appendChild(container);
container.appendChild(renderer.domElement);
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(0, 200, 800);
camera.lookAt(scene.position);
// (camera) controls
// mouse controls: left button to rotate,
// mouse wheel to zoom, right button to pan
controls = new THREE.OrbitControls(camera, renderer.domElement);
// light
var light = new THREE.PointLight(0xffffff);
light.position.set(100, 250, 250);
scene.add(light);
// mesh
var sphereGeometry = new THREE.SphereGeometry(500, 32, 16);
var sphereMaterial = new THREE.MeshBasicMaterial({
color: 0xff0000
});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.y = 100;
scene.add(sphere);
var spherical = new THREE.Sphere(new THREE.Vector3(0, 100, 0), 500); // create once. Then
var bx = []
var tpos = new THREE.Vector3();
for(var i = 0; i<1; i++){
var box = new THREE.Mesh(new THREE.BoxGeometry(30, 30, 30))
scene.add(box)
box.velocity = new THREE.Vector3();
//box.position.x = 100*(i/2)
box.position.z = 500
spherical.clampPoint(box.position, tpos)
box.position.copy(tpos)
box.velocity.copy(box.position)
//spherical.set( 100, 1*(i/2), Math.random()*(i/3) );
//box.position.setFromSpherical( spherical )
//box.geometry.translate(0, 100, 0)
bx.push(box)
}
var alpha = 0
setInterval(()=>{
alpha += 2
/*for(var i = 0; i<bx.length; i++){
bx[i].position.z = alpha
spherical.clampPoint(bx[i].position,...