- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Three.js 并有一个问题。在粒子函数中,我添加了四处飞舞的图像。代码:
函数 makeParticles() {
var particle, material;
// we're gonna move from z position -1000 (far away)
// to 1000 (where the camera is) and add a random particle at every pos.
for ( var zpos= -1000; zpos < 1000; zpos+=20 ) {
// we make a particle material and pass through the
// colour and custom particle render function we defined.
var particleTexture = THREE.ImageUtils.loadTexture('img/fly.png');
material = new THREE.ParticleBasicMaterial( { map: particleTexture, transparent: true, program: particleRender } );
// make the particle
particle = new THREE.Particle(material);
// give it a random x and y position between -500 and 500
particle.position.x = Math.random() * 1000 - 500;
particle.position.y = Math.random() * 1000 - 500;
// set its z position
particle.position.z = zpos;
// scale it up a bit
particle.scale.x = particle.scale.y = 0.3;
// add it to the scene
scene.add( particle );
// and to the array of particles.
particles.push(particle);
}
}
问题是,当我调整页面大小时,所有这些图像的宽度都被压扁了,并且不保持比例。如何在调整页面大小时保持图片大小?
完整代码:
<script>
// the main three.js components
var camera, scene, renderer,
// to keep track of the mouse position
mouseX = 0, mouseY = 0,
// an array to store our particles in
particles = [];
// let's get going!
init();
function init() {
// Camera params :
// field of view, aspect ratio for render output, near and far clipping plane.
camera = new THREE.PerspectiveCamera(-50, window.innerWidth / window.innerHeight, -20, -10000 );
// move the camera backwards so we can see stuff!
// default position is 0,0,0.
camera.position.z = 80;
// the scene contains all the 3D object data
scene = new THREE.Scene();
// camera needs to go in the scene
scene.add(camera);
// and the CanvasRenderer figures out what the
// stuff in the scene looks like and draws it!
renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
// the renderer's canvas domElement is added to the body
document.body.appendChild( renderer.domElement );
makeParticles();
// add the mouse move listener
document.addEventListener( 'mousemove', onMouseMove, false );
// render 30 times a second (should also look
// at requestAnimationFrame)
setInterval(update,1000/30);
}
// the main update function, called 30 times a second
function update() {
updateParticles();
// and render the scene from the perspective of the camera
renderer.render( scene, camera );
}
// creates a random field of Particle objects
function makeParticles() {
var particle, material;
// we're gonna move from z position -1000 (far away)
// to 1000 (where the camera is) and add a random particle at every pos.
for ( var zpos= -1000; zpos < 1000; zpos+=20 ) {
// we make a particle material and pass through the
// colour and custom particle render function we defined.
var particleTexture = THREE.ImageUtils.loadTexture('img/fly.png');
material = new THREE.ParticleBasicMaterial( { map: particleTexture, transparent: true, program: particleRender } );
// make the particle
particle = new THREE.Particle(material);
// give it a random x and y position between -500 and 500
particle.position.x = Math.random() * 1000 - 500;
particle.position.y = Math.random() * 1000 - 500;
// set its z position
particle.position.z = zpos;
// scale it up a bit
particle.scale.x = particle.scale.y = 0.3;
// add it to the scene
scene.add( particle );
// and to the array of particles.
particles.push(particle);
}
}
// there isn't a built in circle particle renderer
// so we have to define our own.
function particleRender( context ) {
// we get passed a reference to the canvas context
context.beginPath();
// and we just have to draw our shape at 0,0 - in this
// case an arc from 0 to 2Pi radians or 360ยบ - a full circle!
context.arc( 0, 0, 1, 0, Math.PI * 2, true );
context.fill();
};
// moves all the particles dependent on mouse position
function updateParticles() {
// iterate through every particle
for(var i=0; i<particles.length; i++) {
particle = particles[i];
// and move it forward dependent on the mouseY position.
particle.position.z += mouseY * 0.02;
// if the particle is too close move it to the back
if(particle.position.z>1500) particle.position.z-=2300;
}
}
// called when the mouse moves
function onMouseMove( event ) {
// store the mouseX and mouseY position
mouseX = event.clientX;
mouseY = event.clientY;
}
</script>
最佳答案
我想,您需要调整渲染器的大小并更新相机纵横比。下面的代码几乎在每个three.js的例子中都能找到,但是我在你的代码中没有看到。
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize() {
var canvasWidth = window.innerWidth;
var canvasHeight = window.innerHeight;
renderer.setSize( canvasWidth, canvasHeight );
camera.aspect = canvasWidth / canvasHeight;
camera.updateProjectionMatrix();
}
关于javascript - Three.js THREE.ImageUtils.loadTexture 图片调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752873/
我正试图在使用 webvr-boilerplate 的项目中使镜像效果正常工作当 VREffect 处于事件状态时。 我尝试(使用 r72dev 和 r72)使用 THREE.Mirror:
我正在尝试为 localClipping 配置 THREE.plane 的位置,但我发现它有点不直观。我有一个想法,是否可以创建一些planeGeometry,操纵它,并将其位置和方向转换为在其位置创
我有 8 个从 Object3D 到不同方向的光线转换器,用于碰撞检测。我想根据对象旋转旋转他们指向的方向。我遵循了 here 中的解决方案 光线转换器开始旋转,但方式很奇怪。它开始检测来自不同方向的
我在微信上用three.js开发3D游戏,但是有个问题,three.js怎么设置渐变的背景色。我在文档上看到了背景色,但是没有渐变的背景色。请说明我是中国人英语不好。 最佳答案 对于高级效果,请尝试使
我试图通过使用Three.js挤压形状来给SVG路径(lineto和moveto)3D感觉,但是该过程会导致一些我无法删除的瑕疵。 什么会导致渲染的3D形状出现奇异的伪像?有没有办法删除它们? 这些伪
我试图在THREE.js中学习有关性能几何的更多信息,并且已经了解索引BufferGeometry和InstancedBufferGeometry是两种性能最高的几何类型。 到目前为止,我的理解是,在
three.js带有许多有用的控件,这些控件会导致相机响应键盘和鼠标输入而移动。 它们都位于https://github.com/mrdoob/three.js/blob/master/example
几个小时以来,我尝试了数十种不同的方法,但都没有效果,如下所示: document.body.addEventListener("keydown", function() { THREEx.Ful
几个小时以来,我尝试了数十种不同的方法,但都没有效果,如下所示: document.body.addEventListener("keydown", function() { THREEx.Ful
当我创建一个 JSON 模型并使用 THREE.JSONLoader.load 加载它时,它的加载没有任何问题,但是当我尝试从中创建一个变量并将数据包含在主脚本中时,THREE.JSONLoader.
我正在尝试添加一个系统粒子,一个三点到场景,但我有这个错误: “THREE.Object3D.add:对象不是 THREE.Object3D 的实例。未定义” 代码: var backCount =
请参阅以下 fiddle : https://jsfiddle.net/1jmws2bp/ 如果将鼠标移动到线条或圆圈上,它应该将颜色更改为白色(对我来说,本地有效,在 jsfiddle 中有时会有一
我正在努力处理我正在处理的涉及重复图像流的可视化。我让它与一个带有 ParticleSystem 的 Sprite 一起工作,但我只能将一种 Material 应用于系统。因为我想在纹理之间进行选择,
我想移动和旋转摄像头,但要保持PointLight相对于摄像头的位置相同。我读过一堆线程说,您可以将灯光对象添加到摄影机而不是场景。像这样: pointLight = new THREE.PointL
我是 three.js 的新手。我创建了多个网格并使用 JSONLoader 进行了分组。现在我的问题是将动画应用于该组或多个网格。是否可以将动画应用于 JSONLoader 中的该组。 提前致谢。
我想了解如何three.js在内部工作。我不明白的一件事是如何three.js编译并运行着色器程序。如果我要重新实现 three.js我自己我会有一个顶点着色器归因于 Geometry以及归因于 Ma
我以前设法显示了框,但在这里,我已经剥离了所有内容,以便通过定位 collada 模型来试验扩展框,但框不会显示。 function loadObjects(){ cobj = new THREE
我是3D编程的新手,我确实开始使用Three.JS从WebGL探索3D世界。 我想在更改camera.position.z和对象的“Z”位置时预先确定对象的大小。 例如: 我有一个大小为 100x10
在 BufferGeometry 中,有没有一种方法可以访问面索引和法线而不转换为 Geometry? 手头的几何体是由 Threejs 编辑器创建的 SphereBufferGeometry。 我只
我遵循了这个 stackoverflow 示例: ThreeJS geometry flipping 我成功镜像了我的几何体。然而现在我的几何图形是黑色的。我可以在翻转几何体的同时翻转法线来纠正这个问
我是一名优秀的程序员,十分优秀!