Rendering Mesh from TextureRegion renders whole TextureAtlas - opengl-es

I have a simple render method that I want to use to draw a simple mesh. The code below, using the TextureRegion class instead of Texture renders a dirty and broken mesh. Its seems that the whole Texture Atlas from which the region is extracted is rendered instead of the texture region:
public void (SpriteBatch batch, OrthographicCamera camera){
mesh.setVertices(vert);
mesh.setIndices(indi);
Gdx.gl20.glEnable(GL20.GL_BLEND);
Gdx.graphics.getGL20().glEnable(GL20.GL_TEXTURE_2D);
shader.begin();
shader.setUniformMatrix("u_worldView", camera.combined);
shader.setUniformi("u_texture", 0);
textureRegion_laser.getTexture().bind(0);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
Gdx.gl20.glDisable(GL20.GL_BLEND);
Gdx.graphics.getGL20().glDisable(GL20.GL_TEXTURE_2D);
}
The code below which uses a Texture instead of of a TextureRegion draws the mesh correctly. What's the pb?
public void (SpriteBatch batch, OrthographicCamera camera){
mesh.setVertices(vert);
mesh.setIndices(indi);
Gdx.gl20.glEnable(GL20.GL_BLEND);
Gdx.graphics.getGL20().glEnable(GL20.GL_TEXTURE_2D);
shader.begin();
shader.setUniformMatrix("u_worldView", camera.combined);
shader.setUniformi("u_texture", 0);
texture.bind(0);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
Gdx.gl20.glDisable(GL20.GL_BLEND);
Gdx.graphics.getGL20().glDisable(GL20.GL_TEXTURE_2D);
}

Related

Issue in rotate camera during initial loading of 3d Object in threejs

I have to rotate the camera during initial loading of object in three js.
So can anyone help me in which way i have to do that.Can i use Perspective Camera for this ?
for reference
https://theyearofgreta.com/
It is possible to tween the camera movement on the initial load of the object.
new TWEEN.Tween(currentPosition)
.to(targetPosition, 2000)
.onUpdate(function () {
camera.position.set(...currentPosition);
camera.lookAt(new THREE.Vector3(0, 0, 0));
});
I made a simple example to showcase the movement:
https://codepen.io/cdeep/pen/ZEygVNy
Basics of using tween for three.js animations:
http://learningthreejs.com/blog/2011/08/17/tweenjs-for-smooth-animation/

Emit light from an object

I'm making a Three.js scene in which I have stars object and I would like to be able to make them "glow".
By glow I mean make them really emit light not just put a "halo" effect around them.
I tried to put a PointLight object at the same position as the star, this make light emit from the object but as you can see, it doesn't make the object "glow" which make a weird effect.
My current code looks like this:
class Marker extends THREE.Object3D {
constructor() {
super();
// load obj model
const loader = new OBJLoader();
loader.load(
"https://supersecretdomain.com/star.obj",
object => {
object.traverse(child => {
if (child instanceof THREE.Mesh) {
// child.material.map = texture;
child.material = new THREE.MeshLambertMaterial({
color: 0xffff00
});
child.scale.set(0.01, 0.01, 0.01);
}
});
this.add(object);
}
);
const light = new THREE.PointLight(0xffff00, 0.5, 5);
this.add(light);
}
}
Any idea of how to do this ? ;)
Adding a point light to the star is the correct way to make other objects be affected by its light. To make the star itself shine, you can set the emissive color of the material to something other than black (for best results, you probably want it to be the same color as the light).
In addition to setting up your light, and setting the material emissive property as mentioned by Jave above..
You might want to look into the THREE PostProcessing examples.. Specifically Unreal Bloom pass... If you can get that working, it really sells the effect.
https://threejs.org/examples/webgl_postprocessing_unreal_bloom.html
Notice how the highlight glow actually bleeds into the scene around the object...

How do I change the texture of a GLTF model dynamically?

In my scene I have loaded a .gltf model and it is rendered just fine.. It has a .png texture that is rendered on the surface of the 3d model. Is it possible to swap the texture programmatically? I'm using aframe (a-asset-item and a-entity to load the gltf asset)
Once you’ve loaded a model in A-Frame or three.js, it doesn’t matter much what format it was before1. For A-Frame, you can use JS to modify the model after it loads.
var tex = new THREE.TextureLoader().load('diffuse.png');
tex.flipY = false; // for glTF models.
el.addEventListener('model-loaded', function (e {
e.detail.model.traverse(function(node) {
if (node.isMesh) node.material.map = tex;
});
});
See docs on THREE.MeshStandardMaterial to learn what properties there are to edit, although this could vary depending on the model you’re loading.
1 One exception is the tex.flipY=false setting above — you'll (probably) only need that for glTF, where the UVs have a different orientation than the three.js default.

How to apply texture for .fbx model in three.js?

How do i change texture of ".fbx" model in three js library at runtime?
The main problem with applying a texture to an .FBX model, is that the .FBX is loaded as a group of sub-models, each with their own materials. The way to replace these textures is to traverse the model structure:
// FBX loader returns a group containing a multiple sub-objects. Traverse and apply texture to all.
group.traverse(function (child) {
if (child instanceof THREE.Mesh) {
// apply texture
child.material.map = texture
child.material.needsUpdate = true;
}
});
For a working sample, I've modified the default three.js FBX example to demonstrate this:
http://www.eponalabs.com/experiments/FBXReplaceTexture/fbx_replace_texture.html
When you press the button, the code fragment above is used to replace the textures with a placeholder image from Unsplash.it.

three.js and openlayers coordinates don't line up

I'm using three.js on a separate canvas on top of a openlayers map. the way I synchronize the view of the map and the three.js camera (which looks straight down onto the map) looks like this:
// calculate how far away the camera needs to be, to
// see this part of the map
function distanceFromExtentAndFOV(h, vFOV) {
return h / (2 * Math.tan(vFOV / 2));
}
// keep three.js camera in sync with visible part of openlayers map
function updateThreeCam(
group, // three.js group, containing camera
view, // ol view
map // ol map
) {
extent = getViewExtent(view, map);
const h = ol.extent.getHeight(extent);
mapCenter = ol.extent.getCenter(extent);
camDist = distanceFromExtentAndFOV(h, constants.CAM_V_FOV_RAD);
const pos = [...mapCenter, camDist];
group.position.set(...pos);
group.updateMatrixWorld();
}
// whenever view changes, update three.js camera
view.on('change:center', (event) => {
updateThreeCam(group, event.target, map);
});
view.on('change:resolution', (event) => {
updateThreeCam(group, event.target, map);
});
updateThreeCam(group, view, map);
I'm using three.js for custom animations of objects, that in the end land on the ground. however, when I turn the objects into openlayers features, the original three.js objects and the features don't line up perfectly (although their position coordinates are exactly the same). — also, when you pan, you can see that s.th. is slightly off.
// turn three.js planes into openlayers features:
const features = rects.map((rect) => {
const point = new ol.geom.Point([
rect.position.x,
tect.position.y
]);
return new ol.Feature(point);
});
vectorSource.addFeatures(features);
as I am pretty sure that my the code for synchronizing three.js and ol is correct, I am not really sure what what is going wrong. am I missing s.th. here?
It could be that you are using PerspectiveCamera and that will change the size/position based on depth. Try OrtographicCamera instead.

Resources