com.badlogic.gdx.graphics.g3d.ModelInstance Java Examples

The following examples show how to use com.badlogic.gdx.graphics.g3d.ModelInstance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RotateTool.java    From Mundus with Apache License 2.0 6 votes vote down vote up
public RotateHandle(int id, Color color) {
    super(id);
    model = UsefulMeshs.torus(new Material(ColorAttribute.createDiffuse(color)), 20, 1f, 50, 50);
    modelInstance = new ModelInstance(model);
    modelInstance.materials.first().set(getIdAttribute());
    switch (id) {
    case X_HANDLE_ID:
        this.getRotationEuler().y = 90;
        this.getScale().x = 0.9f;
        this.getScale().y = 0.9f;
        this.getScale().z = 0.9f;
        break;
    case Y_HANDLE_ID:
        this.getRotationEuler().x = 90;
        break;
    case Z_HANDLE_ID:
        this.getRotationEuler().z = 90;
        this.getScale().x = 1.1f;
        this.getScale().y = 1.1f;
        this.getScale().z = 1.1f;
        break;
    }
    // mi.transform.translate(0, 100, 0);
}
 
Example #2
Source File: Sky.java    From gdx-proto with Apache License 2.0 6 votes vote down vote up
public static void createSkyBox (Texture xpos, Texture xneg, Texture ypos, Texture yneg, Texture zpos, Texture zneg) {
	modelInstance = new ModelInstance(model, "Skycube");
	
	// Set material textures
	modelInstance.materials.get(0).set(TextureAttribute.createDiffuse(xpos));
	modelInstance.materials.get(1).set(TextureAttribute.createDiffuse(xneg));
	modelInstance.materials.get(2).set(TextureAttribute.createDiffuse(ypos));
	modelInstance.materials.get(3).set(TextureAttribute.createDiffuse(yneg));
	modelInstance.materials.get(5).set(TextureAttribute.createDiffuse(zpos));
	modelInstance.materials.get(4).set(TextureAttribute.createDiffuse(zneg));
	
	//Disable depth test
	modelInstance.materials.get(0).set(new DepthTestAttribute(0));
	modelInstance.materials.get(1).set(new DepthTestAttribute(0));
	modelInstance.materials.get(2).set(new DepthTestAttribute(0));
	modelInstance.materials.get(3).set(new DepthTestAttribute(0));
	modelInstance.materials.get(4).set(new DepthTestAttribute(0));
	modelInstance.materials.get(5).set(new DepthTestAttribute(0));
	
	enabled = true;
}
 
Example #3
Source File: LevelBuilder.java    From gdx-proto with Apache License 2.0 6 votes vote down vote up
/** client builds statics, probably based on info from server */
public static void buildStatics(LevelStatic[] statics) {
	if (staticGeometry == null) {
		staticGeometry = new Array<>();
	}
	Log.debug("client building statics received from server: " + statics.length);
	ModelBuilder mb = new ModelBuilder();
	mb.begin();
	for (LevelStatic stat : statics) {
		Model model = Assets.manager.get(stat.modelName, Model.class);
		setupStaticModel(model.meshParts, stat.mtx, true);
		Node node = mb.node("piece", model);
		stat.mtx.getTranslation(tmp);
		node.translation.set(tmp);
		node.rotation.set(stat.mtx.getRotation(q));
	}
	Model finalModel = mb.end();
	ModelInstance instance = new ModelInstance(finalModel);
	staticGeometry.add(instance);
}
 
Example #4
Source File: GameModel.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
/**
 * Holds a an instance of the model.
 *
 * @param name       Name of model
 * @param model    Model to instantiate
 * @param location World position at which to place the model instance
 * @param rotation The rotation of the model instance in degrees
 * @param scale    Scale of the model instance
 */
public GameModel(Model model,
				 String name,
				 Vector3 location,
				 Vector3 rotation,
				 Vector3 scale) {
	super(name);
	modelInstance = new ModelInstance(model);

	applyTransform(location, rotation, scale, modelInstance);

	try {
		modelInstance.calculateBoundingBox(boundingBox);
	} catch (Exception e) {
		Gdx.app.debug(TAG, "Error when calculating bounding box.", e);
	}
	boundingBox.getCenter(center);
	boundingBox.getDimensions(dimensions);
	boundingBoxRadius = dimensions.len() / 2f;
	modelTransform = modelInstance.transform;
	halfExtents.set(dimensions).scl(0.5f);
}
 
Example #5
Source File: SimpleRoom.java    From gdx-vr with Apache License 2.0 6 votes vote down vote up
@Override
public void create() {
	assets = new AssetManager();
	String model = "Bambo_House.g3db";
	assets.load(model, Model.class);
	assets.finishLoading();
	modelInstance = new ModelInstance(assets.get(model, Model.class), new Matrix4().setToScaling(0.6f, 0.6f, 0.6f));

	DefaultShader.Config config = new Config();
	config.defaultCullFace = GL20.GL_NONE;
	ShaderProvider shaderProvider = new DefaultShaderProvider(config);
	modelBatch = new ModelBatch(shaderProvider);

	ModelBuilder builder = new ModelBuilder();
	float groundSize = 1000f;
	ground = new ModelInstance(builder.createRect(-groundSize, 0, groundSize, groundSize, 0, groundSize, groundSize, 0, -groundSize, -groundSize, 0, -groundSize, 0,
			1, 0, new Material(), Usage.Position | Usage.Normal), new Matrix4().setToTranslation(0, -0.01f, 0));
	environment = new Environment();
	environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
	environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

	VirtualReality.renderer.listeners.add(this);
	// VirtualReality.head.setCyclops(true);
}
 
Example #6
Source File: Block.java    From gdx-proto with Apache License 2.0 6 votes vote down vote up
public Block(ModelInstance instance, int width, int height, int depth, boolean dynamic) {
	this.dynamic = dynamic;
	this.instance = instance;
	dimen.set(width, height, depth);
	tmp.set(dimen).scl(0.5f);
	body = Physics.inst.createBoxObject(tmp);
	Physics.applyStaticGeometryCollisionFlags(body);
	mtx.set(instance.transform);
	mtx.getTranslation(translation);
	translation.add(tmp);
	mtx.setToTranslation(translation);
	body.setWorldTransform(mtx);
	blocks.add(this);
	if (dynamic) {
		Physics.inst.addDynamicEntityToWorld(body);
	} else {
		Physics.inst.addStaticGeometryToWorld(body);
	}
}
 
Example #7
Source File: Sprite3DRenderer.java    From bladecoder-adventure-engine with Apache License 2.0 6 votes vote down vote up
private void retrieveSource(String source) {
	ModelCacheEntry entry = (ModelCacheEntry) sourceCache.get(source);

	if (entry == null || entry.refCounter < 1) {
		loadSource(source);
		EngineAssetManager.getInstance().finishLoading();
		entry = (ModelCacheEntry) sourceCache.get(source);
	}

	if (entry.modelInstance == null) {
		Model model3d = EngineAssetManager.getInstance().getModel3D(source);
		entry.modelInstance = new ModelInstance(model3d);
		entry.controller = new AnimationController(entry.modelInstance);
		entry.camera3d = getCamera(entry.modelInstance);
	}
}
 
Example #8
Source File: Utils3D.java    From bladecoder-adventure-engine with Apache License 2.0 6 votes vote down vote up
public static void createFloor() {

		ModelBuilder modelBuilder = new ModelBuilder();
		modelBuilder.begin();
		MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES,
				Usage.Position | Usage.Normal | Usage.ColorUnpacked, new Material(
						ColorAttribute.createDiffuse(Color.WHITE)));
		mpb.setColor(1f, 1f, 1f, 1f);
//		mpb.box(0, -0.1f, 0, 10, .2f, 10);
		mpb.rect(-10, 0, -10, 
				-10, 0, 10,
				10, 0, 10,
				10, 0, -10, 0, 1, 0);
		floorModel = modelBuilder.end();
		floorInstance = new ModelInstance(floorModel);
		
		// TODO Set only when FBO is active
		floorInstance.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));
	}
 
Example #9
Source File: Terrain.java    From Mundus with Apache License 2.0 6 votes vote down vote up
public void init() {
    final int numVertices = this.vertexResolution * vertexResolution;
    final int numIndices = (this.vertexResolution - 1) * (vertexResolution - 1) * 6;

    mesh = new Mesh(true, numVertices, numIndices, attribs);
    this.vertices = new float[numVertices * stride];
    mesh.setIndices(buildIndices());
    buildVertices();
    mesh.setVertices(vertices);

    MeshPart meshPart = new MeshPart(null, mesh, 0, numIndices, GL20.GL_TRIANGLES);
    meshPart.update();
    ModelBuilder mb = new ModelBuilder();
    mb.begin();
    mb.part(meshPart, material);
    model = mb.end();
    modelInstance = new ModelInstance(model);
    modelInstance.transform = transform;
}
 
Example #10
Source File: Utils3D.java    From bladecoder-adventure-engine with Apache License 2.0 6 votes vote down vote up
private static void createAxes() {
	ModelBuilder modelBuilder = new ModelBuilder();
	modelBuilder.begin();
	MeshPartBuilder builder = modelBuilder.part("grid", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
	builder.setColor(Color.LIGHT_GRAY);
	for (float t = GRID_MIN; t <= GRID_MAX; t+=GRID_STEP) {
		builder.line(t, 0, GRID_MIN, t, 0, GRID_MAX);
		builder.line(GRID_MIN, 0, t, GRID_MAX, 0, t);
	}
	builder = modelBuilder.part("axes", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
	builder.setColor(Color.RED);
	builder.line(0, 0, 0, 10, 0, 0);
	builder.setColor(Color.GREEN);
	builder.line(0, 0, 0, 0, 10, 0);
	builder.setColor(Color.BLUE);
	builder.line(0, 0, 0, 0, 0, 10);
	axesModel = modelBuilder.end();
	axesInstance = new ModelInstance(axesModel);
}
 
Example #11
Source File: Utils3D.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public static ModelInstance getAxes() {
	if(axesModel == null) {
		createAxes();
	}
	
	return axesInstance;		
}
 
Example #12
Source File: GameModel.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
public static void applyTransform(Vector3 location, Vector3 rotation, Vector3 scale,
								  ModelInstance modelInstance) {
	for (Node node : modelInstance.nodes) {
		node.scale.set(Math.abs(scale.x), Math.abs(scale.y), Math.abs(scale.z));
	}
	modelInstance.transform.rotate(Vector3.X, rotation.x);
	modelInstance.transform.rotate(Vector3.Z, rotation.z);
	modelInstance.transform.rotate(Vector3.Y, rotation.y);
	modelInstance.transform.setTranslation(location);

	modelInstance.calculateTransforms();
}
 
Example #13
Source File: GameObjectBlueprint.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
public GameObjectBlueprint(BlenderModel blenderModel, Model model) {
	this.model = model;
	setFromObject(blenderModel);
	this.mass = 0;
	ModelInstance modelInstance = new ModelInstance(model);
	GameModel.applyTransform(position, rotation, blenderModel.scale, modelInstance);

	this.shape = Bullet.obtainStaticNodeShape(modelInstance.nodes);
	this.shapeType = "static_node_shape_" + blenderModel.name;
	setCollisionFlags(this.mass);
}
 
Example #14
Source File: Utils3D.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public static ModelInstance getFloor() {
	if(floorModel == null) {
		createFloor();
	}
	
	return floorInstance;
}
 
Example #15
Source File: BulletEntity.java    From gdx-ai with Apache License 2.0 5 votes vote down vote up
public BulletEntity (final ModelInstance modelInstance, final btCollisionObject body) {
	this.modelInstance = modelInstance;
	this.transform = this.modelInstance.transform;
	this.body = body;

	if (body != null) {
		body.userData = this;
		if (body instanceof btRigidBody) {
			this.motionState = new MotionState(this.modelInstance.transform);
			((btRigidBody)this.body).setMotionState(motionState);
		} else
			body.setWorldTransform(transform);
	}
}
 
Example #16
Source File: FluidSimulatorGeneric.java    From fluid-simulator-v2 with Apache License 2.0 5 votes vote down vote up
public FluidSimulatorGeneric(FluidSimulatorStarter fluidSimulatorStarter) {
		this.game = fluidSimulatorStarter;
		// LibGDX single batches cannot have a size more than 5460
		batch = new SpriteBatch(IS_DESKTOP ? 5460 : ANDROID_SIZE);
		font = new BitmapFont();
		camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
		camera.position.set(0, (WORLD_HEIGHT / 2) - 1, 0);
		immediateRenderer = new ImmediateModeRenderer20(SIZE*6, false, true, 0);
		irt = new Renderer20(SIZE*6, false, true, 1);
		irt2 = new ImmediateModeRenderer20(SIZE*11, false, true, 1);
		shapeRenderer = new ShapeRenderer(SIZE);
		renderer = new Box2DDebugRenderer(true, true, false, true, false, false);
		
		//3D
		camera3D = new PerspectiveCamera(67, WORLD_WIDTH, WORLD_HEIGHT);
		camera3D.position.set(0, 130f, 250f);
		camera3D.lookAt(0,150f,0);
		camera3D.near = 0.1f;
		camera3D.far = 500f;
		camera3D.update();
		ModelBuilder modelBuilder = new ModelBuilder();
//        model = modelBuilder.createSphere(5f, 5f, 5f, 4, 4, GL10.GL_TRIANGLES,
//                new Material(ColorAttribute.createDiffuse(Color.GREEN)),
//                Usage.Position | Usage.Normal);
        model = modelBuilder.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
        instance = new ModelInstance(model);
        modelBatch = new ModelBatch();
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, 0, -0.8f, -0.2f));
        camController = new Camera3DController(camera3D);
        camController.setFluidSimulator(this);
		
		world = new World(new Vector2(0, -9.8f), false);
		world.setContactListener(this);
	}
 
Example #17
Source File: Terrain.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param model the model which is used for rendering
 * @param collisionMesh a mesh used for collision
 * @return
 */
public static TerrainChunk CreateMeshChunk (Model model, Model collisionMesh) {
	// Creates collision out the collision mesh
	btCollisionObject obj = new btCollisionObject();
	btCollisionShape shape = new btBvhTriangleMeshShape(collisionMesh.meshParts);
	obj.setCollisionShape(shape);
	Physics.applyStaticGeometryCollisionFlags(obj);
	Physics.inst.addStaticGeometryToWorld(obj);

	return new TerrainChunk(new ModelInstance(model), obj);
}
 
Example #18
Source File: TerrainChunk.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
public void render(ModelBatch batch, Environment env) {
	batch.render(modelInstance, env);
	
	for (ModelInstance m : sceneObjects) {
		batch.render(m, env);
	}
}
 
Example #19
Source File: View.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
private boolean groundPieceVisibilityCheck(ModelInstance modelInst) {
	float halfWidth = LevelBuilder.groundPieceSize / 2f;
	modelInst.transform.getTranslation(tmp);
	// we want the center of the piece
	tmp.add(halfWidth, 0, halfWidth);
	float radius = LevelBuilder.groundPieceSize;
	return camera.frustum.sphereInFrustum(tmp, LevelBuilder.groundPieceSize);
	// this naive method is useful for debugging to see pop-in/pop-out
	//return camera.frustum.pointInFrustum(tmp);
}
 
Example #20
Source File: Box.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
/** create some boxes to fill the level with some test geometry */
public static void createBoxes(int count) {
	ModelBuilder main = new ModelBuilder();
	ModelBuilder mb = new ModelBuilder();
	Material material = new Material();
	if (Main.isClient()) {
		material.set(TextureAttribute.createDiffuse(Assets.manager.get("textures/marble.jpg", Texture.class)));
	}
	main.begin();
	//float x = GameWorld.WORLD_WIDTH;
	//float y = GameWorld.WORLD_DEPTH;
	for (int i = 0; i < count; i++) {
		//float w = MathUtils.random(minW, maxW);
		float w = 8f;
		float d = 8f;
		float h = (i+1)*5f;
		tmp.set(10f + (w+2) * i, 0f, 10f + (d+2) * i);
		if (Main.isClient()) {
			mb.begin();
			MeshPartBuilder mpb = mb.part("part-" + i, GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, material);
			mpb.box(w, h, d);
			Model boxModel = mb.end();
			Node node = main.node("box-" + i, boxModel);
			node.translation.set(tmp);
			q.idt();
			node.rotation.set(q);
		}
		//node.translation.set(MathUtils.random(x), 0f, MathUtils.random(y));
		//q.set(Vector3.X, -90);
		mtx.set(q);
		mtx.setTranslation(tmp);
		btCollisionObject obj = Physics.inst.createBoxObject(tmp.set(w/2, h/2, d/2));
		obj.setWorldTransform(mtx);
		Physics.applyStaticGeometryCollisionFlags(obj);
		Physics.inst.addStaticGeometryToWorld(obj);
	}
	Model finalModel = main.end();
	instance = new ModelInstance(finalModel);
}
 
Example #21
Source File: Sky.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
public static void createSkyBox (Texture skybox) {
	modelInstance = new ModelInstance(model, "Skybox");
	
	// Set material texutres and Disable depth test
	modelInstance.materials.get(0).set(TextureAttribute.createDiffuse(skybox));
	modelInstance.materials.get(0).set(new DepthTestAttribute(0));
	
	enabled = true;
}
 
Example #22
Source File: ScaleTool.java    From Mundus with Apache License 2.0 5 votes vote down vote up
public ScaleHandle(int id, Model model) {
    super(id);
    this.model = model;
    this.modelInstance = new ModelInstance(model);
    modelInstance.materials.first().set(getIdAttribute());

}
 
Example #23
Source File: Entity.java    From Radix with MIT License 5 votes vote down vote up
public void render(ModelBatch batch) {
    if (model != null) {
        // TODO: support subtle action. need to create instance every time?
        ModelInstance instance = new ModelInstance(model);
        instance.materials.get(0).set(TextureAttribute.createDiffuse(texture));
        instance.transform.translate(position.getX(), position.getY(), position.getZ());
        batch.render(instance);
    }
}
 
Example #24
Source File: EntityModel.java    From Radix with MIT License 5 votes vote down vote up
private static ModelInstance getInstance(String objPath, String texturePath) {
    Model model = loader.loadModel(Gdx.files.internal(objPath), new ObjLoader.ObjLoaderParameters(true));
    ModelInstance instance = new ModelInstance(model);
    Texture texture = new Texture(Gdx.files.internal(texturePath));
    instance.materials.get(0).set(TextureAttribute.createDiffuse(texture));
    return instance;
}
 
Example #25
Source File: Scene.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
/**
 * Create a scene
 * @param modelInstance
 * @param animated
 */
public Scene(ModelInstance modelInstance, boolean animated) {
	super();
	this.modelInstance = modelInstance;
	if(animated){
		this.animationController = new AnimationControllerHack(modelInstance);
	}
	animations = new AnimationsPlayer(this);
}
 
Example #26
Source File: GameRenderer.java    From Radix with MIT License 5 votes vote down vote up
private void drawBlockSelection() {
    int curProgressInt = Math.round(RadixClient.getInstance().getPlayer().getBreakPercent() * 10) - 1;
    if ((blockBreakModel == null || blockBreakStage != curProgressInt) && curProgressInt >= 0) {
        if (blockBreakModel != null)
            blockBreakModel.dispose();

        blockBreakStage = curProgressInt;

        ModelBuilder builder = new ModelBuilder();
        blockBreakModel = builder.createBox(1f, 1f, 1f,
                new Material(TextureAttribute.createDiffuse(blockBreakStages[blockBreakStage]),
                        new BlendingAttribute(),
                        FloatAttribute.createAlphaTest(0.25f)),
                VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates);
        blockBreakModelInstance = new ModelInstance(blockBreakModel);
    }

    Vec3i curBlk = RadixClient.getInstance().getSelectedBlock();
    if (curBlk != null && curProgressInt >= 0) {
        Gdx.gl.glPolygonOffset(100000, 2000000);
        blockOverlayBatch.begin(RadixClient.getInstance().getCamera());
        blockBreakModelInstance.transform.translate(curBlk.x + 0.5f, curBlk.y + 0.5f, curBlk.z + 0.5f);
        blockOverlayBatch.render(blockBreakModelInstance);
        blockBreakModelInstance.transform.translate(-(curBlk.x + 0.5f), -(curBlk.y + 0.5f), -(curBlk.z + 0.5f));
        blockOverlayBatch.end();
        Gdx.gl.glPolygonOffset(100000, -2000000);
    }
}
 
Example #27
Source File: ModelComponent.java    From Mundus with Apache License 2.0 5 votes vote down vote up
public void setModel(ModelAsset model, boolean inheritMaterials) {
    this.modelAsset = model;
    modelInstance = new ModelInstance(model.getModel());
    modelInstance.transform = gameObject.getTransform();

    // apply default materials of model
    if (inheritMaterials) {
        for (String g3dbMatID : model.getDefaultMaterials().keySet()) {
            materials.put(g3dbMatID, model.getDefaultMaterials().get(g3dbMatID));
        }
    }
    applyMaterials();
}
 
Example #28
Source File: ModelComponent.java    From Mundus with Apache License 2.0 5 votes vote down vote up
@Override
public Component clone(GameObject go) {
    ModelComponent mc = new ModelComponent(go, shader);
    mc.modelAsset = this.modelAsset;
    mc.modelInstance = new ModelInstance(modelAsset.getModel());
    mc.shader = this.shader;
    return mc;
}
 
Example #29
Source File: Skybox.java    From Mundus with Apache License 2.0 5 votes vote down vote up
public Skybox(FileHandle positiveX, FileHandle negativeX, FileHandle positiveY, FileHandle negativeY,
        FileHandle positiveZ, FileHandle negativeZ) {
    set(positiveX, negativeX, positiveY, negativeY, positiveZ, negativeZ);

    boxModel = createModel();
    boxInstance = new ModelInstance(boxModel);
}
 
Example #30
Source File: PickableModelComponent.java    From Mundus with Apache License 2.0 5 votes vote down vote up
@Override
public Component clone(GameObject go) {
    PickableModelComponent mc = new PickableModelComponent(go, shader);
    mc.modelAsset = this.modelAsset;
    mc.modelInstance = new ModelInstance(modelAsset.getModel());
    mc.shader = this.shader;
    mc.encodeRaypickColorId();
    return mc;
}