【发布时间】:2017-09-20 09:08:24
【问题描述】:
大家好:) 我想用 js 创建自定义图形。我该怎么做?
我尝试使用 Threejs。我创建坐标轴,但我不知道如何为 ex 创建图形。这个函数:x^2+2*y^2.
class Graphic extends Component {
constructor() {
super();
this.scene = new Three.Scene;
}
componentDidMount() {
this.createScene();
}
createScene = () => {
this.setCamera();
this.getLight();
this.coordinateAxes();
this.reproduce();
}
coordinateAxes = () => {
const lineGeometryFirst = new Three.Geometry();
const lineGeometrySecond = new Three.Geometry();
const lineGeometryThird = new Three.Geometry();
const pointZero = new Three.Vector3(0, 0, 0);
const pointX = new Three.Vector3(100, 0, 0);
const pointY = new Three.Vector3(0, 100, 0);
const pointZ = new Three.Vector3(0, 0, 100);
const arrowX = {
left: new Three.Vector3(95, -5, 0),
right: new Three.Vector3(95, 5, 0),
};
const arrowY = {
left: new Three.Vector3(-5, 95, 0),
right: new Three.Vector3(5, 95, 0),
};
const arrowZ = {
left: new Three.Vector3(0, -5, 95),
right: new Three.Vector3(0, 5, 95),
};
lineGeometryFirst.vertices.push(pointZero, pointX, arrowX.left, pointX, arrowX.right);
lineGeometrySecond.vertices.push(pointZero, pointY, arrowY.left, pointY, arrowY.right);
lineGeometryThird.vertices.push(pointZero, pointZ, arrowZ.left, pointZ, arrowZ.right);
const axisX = new Three.Line( lineGeometryFirst, new Three.LineBasicMaterial({ color: 0xffffff, linewidth: 500 }));
const axisY = new Three.Line( lineGeometrySecond, new Three.LineBasicMaterial({ color: 0x00FF00, linewidth: 500 }));
const axisZ = new Three.Line( lineGeometryThird, new Three.LineBasicMaterial({ color: 0xFFFF00, linewidth: 500 }));
this.scene.add(axisX);
this.scene.add(axisY);
this.scene.add(axisZ);
}
reproduce = () => {
const width = window.innerWidth;
const height = window.innerHeight;
const renderer = new Three.WebGLRenderer({ canvas: this.canvas });
const camera = this.setCamera(width, height);
renderer.setSize(width, height);
renderer.render(this.scene, camera);
}
setCamera = (width, height) => {
const camera = new Three.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 5000);
camera.position.set(120, 50, 300);
return camera;
}
getLight = () => {
const light = new Three.AmbientLight(0xffffff);
this.scene.add(light);
}
render() {
return <canvas ref={canvas => this.canvas = canvas} />;
}
}
谁能让我走上正确的道路?
【问题讨论】:
-
问题需要改进。 “图形”过于宽泛或含糊不清。
-
Okey:) 我改标题
标签: javascript reactjs 3d three.js