var renderer, scene, camera, controls, stats;
var WIDTH = window.innerWidth,
\t HEIGHT = window.innerHeight,
\t FOV = 35,
\t NEAR = 1,
\t FAR = 1000,
ray1, ray2, mesh;
function populateScene(){
\t var cubeGeo = new THREE.BoxBufferGeometry(10, 10, 10),
\t \t cubeMat = new THREE.MeshPhongMaterial({ color: "red", transparent: true, opacity: 0.5 });
\t mesh = new THREE.Mesh(cubeGeo, cubeMat);
mesh.applyMatrix(new THREE.Matrix4().makeScale(1, 1, -1));
mesh.updateMatrixWorld(true);
\t scene.add(mesh);
var dir = new THREE.Vector3(0.5, 0.5, 1);
dir.normalize();
ray1 = new THREE.Ray(new THREE.Vector3(), dir);
var arrow1 = new THREE.ArrowHelper(ray1.direction, ray1.origin, 20, 0x00ff00);
scene.add(arrow1);
var inverseMatrix = new THREE.Matrix4();
inverseMatrix.getInverse(mesh.matrixWorld);
ray2 = ray1.clone();
ray2.applyMatrix4(inverseMatrix);
var arrow2 = new THREE.ArrowHelper(ray2.direction, ray2.origin, 20, 0xff0000);
scene.add(arrow2);
}
function init() {
\t document.body.style.backgroundColor = "slateGray";
\t renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
\t document.body.appendChild(renderer.domElement);
\t document.body.style.overflow = "hidden";
\t document.body.style.margin = "0";
\t document.body.style.padding = "0";
\t scene = new THREE.Scene();
\t camera = new THREE.PerspectiveCamera(FOV, WIDTH/HEIGHT, NEAR, FAR);
\t camera.position.z = 50;
\t scene.add(camera);
\t controls = new THREE.TrackballControls(camera, renderer.domElement);
\t controls.dynamicDampingFactor = 0.5;
\t controls.rotateSpeed = 3;
\t var light = new THREE.PointLight(0xffffff, 1, Infinity);
\t camera.add(light);
\t stats = new Stats();
\t stats.domElement.style.position = 'absolute';
\t stats.domElement.style.top = '0';
\t document.body.appendChild(stats.domElement);
\t resize();
\t window.onresize = resize;
\t populateScene();
\t animate();
var rayCaster = new THREE.Raycaster();
document.getElementById("greenCast").addEventListener("click", function(){
rayCaster.ray.copy(ray1);
alert(rayCaster.intersectObject(mesh).length + " intersections!");
});
document.getElementById("redCast").addEventListener("click", function(){
rayCaster.ray.copy(ray2);
alert(rayCaster.intersectObject(mesh).length + " intersections!");
});
document.getElementById("setSide").addEventListener("click", function(){
mesh.material.side = THREE.DoubleSide;
mesh.material.needsUpdate = true;
});
}
function resize() {
\t WIDTH = window.innerWidth;
\t HEIGHT = window.innerHeight;
\t if (renderer && camera && controls) {
\t \t renderer.setSize(WIDTH, HEIGHT);
\t \t camera.aspect = WIDTH/HEIGHT;
\t \t camera.updateProjectionMatrix();
\t \t controls.handleResize();
\t }
}
function render() {
\t renderer.render(scene, camera);
}
function animate() {
\t requestAnimationFrame(animate);
\t render();
\t controls.update();
\t stats.update();
}
function threeReady() {
\t init();
}
(function() {
\t function addScript(url, callback) {
\t \t callback = callback || function() { };
\t \t var script = document.createElement("script");
\t \t script.addEventListener("load", callback);
\t \t script.setAttribute("src", url);
\t \t document.head.appendChild(script);
\t }
\t addScript("https://threejs.org/build/three.js", function() {
\t \t addScript("https://threejs.org/examples/js/controls/TrackballControls.js", function() {
\t \t \t addScript("https://threejs.org/examples/js/libs/stats.min.js", function() {
\t \t \t \t threeReady();
\t \t \t })
\t \t })
\t })
})();
body{
text-align: center;
}
<input id="greenCast" type="button" value="Cast Green">
<input id="redCast" type="button" value="Cast Red">
<input id="setSide" type="button" value="Set THREE.DoubleSide">
このソリューションでは、働いていました! – Vad