gpt4 book ai didi

javascript - 在 three.js 中向对象(白色圆圈)添加文本

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:03 26 4
gpt4 key购买 nike

这是一个JS Fiddle这显示了我现在所拥有的。我想添加一个文本 3D/2D 无所谓。文本应随着白色圆圈的旋转而旋转。只要能获得预期的结果,任何方法都是优选的。这是代码。

three.js代码

var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight,

mouseX = 0,
mouseY = 0,

windowHalfX = window.innerWidth / 2,
windowHalfY = window.innerHeight / 2,

SEPARATION = 200,
AMOUNTX = 100,
AMOUNTY = 10,

camera, scene, renderer;

init();
animate();

function init() {

var container, separation = 100,
amountX = 50,
amountY = 50,
particles, particle;

container = document.createElement('div');
document.body.appendChild(container);

camera = new THREE.PerspectiveCamera(75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000);
camera.position.y = 5800;
camera.position.z = 1000;

scene = new THREE.Scene();

renderer = new THREE.CanvasRenderer({
alpha: true
});
renderer.setPixelRatio(window.devicePixelRatio);

renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container.appendChild(renderer.domElement);

// particles

var PI2 = Math.PI * 2;
var material = new THREE.SpriteCanvasMaterial({

color: 0xffffff,
program: function (context) {

context.beginPath();
context.arc(0, 0, 9.5, 0, PI2, true);
context.fill();

}

});
for (var i = 0; i < 1000; i++) {

particle = new THREE.Sprite(material);
particle.position.x = Math.random() * 2 - 1;
particle.position.y = Math.random() * 2 - 1;
particle.position.z = Math.random() * 2 - 1;

particle.position.normalize();
particle.position.multiplyScalar(Math.random() * 10 + 450);
particle.scale.multiplyScalar(10);
scene.add(particle);

}

// lines

for (var i = 0; i < 300; i++) {

var geometry = new THREE.Geometry();

var vertex = new THREE.Vector3(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1);
vertex.normalize();
vertex.multiplyScalar(1850);

geometry.vertices.push(vertex);

var vertex2 = vertex.clone();
vertex2.multiplyScalar(Math.random() * 0.3 + 1);

geometry.vertices.push(vertex2);

var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({
color: 0xffffff,
opacity: Math.random()
}));
scene.add(line);
}

document.addEventListener('mousemove', onDocumentMouseMove, false);
document.addEventListener('touchstart', onDocumentTouchStart, false);
document.addEventListener('touchmove', onDocumentTouchMove, false);

//

window.addEventListener('resize', onWindowResize, false);

}

function onWindowResize() {

windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize(window.innerWidth, window.innerHeight);

}

//

function onDocumentMouseMove(event) {

mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
}

function onDocumentTouchStart(event) {

if (event.touches.length > 1) {

event.preventDefault();

mouseX = event.touches[0].pageX - windowHalfX;
mouseY = event.touches[0].pageY - windowHalfY;

}

}

function onDocumentTouchMove(event) {

if (event.touches.length == 1) {

event.preventDefault();

mouseX = event.touches[0].pageX - windowHalfX;
mouseY = event.touches[0].pageY - windowHalfY;

}

}

//

function animate() {

requestAnimationFrame(animate);

render();

}

function render() {

camera.position.x += (mouseX - camera.position.x) * .05;
camera.position.y += (-mouseY + 200 - camera.position.y) * .05;

camera.lookAt(scene.position);

renderer.render(scene, camera);

}

提前致谢。

最佳答案

简单的 3D 文本,我在一个旧文件中找到它:你也需要来源:

 <script src="http://mrdoob.github.com/three.js/examples/fonts/helvetiker_regular.typeface.js"></script>

var MyWords = "Thomas Sebastian";
var shape = new THREE.TextGeometry(MyWords, {font: 'helvetiker',size:'24',curveSegments: 20, weight: 'normal',height : 4,hover:30});
var wrapper = new THREE.MeshPhongMaterial({color: 0x65676,ambient: 0x030303, specular: 0x009900, shininess: 30});
var words = new THREE.Mesh(shape, wrapper);
words.position.x=-80;
words.position.z=0;
words.position.y=20;
scene.add(words);

结果:)

enter image description here

关于javascript - 在 three.js 中向对象(白色圆圈)添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29894937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com