Search in sources :

Example 6 with ColorRGBA

use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.

the class GeneratedTexture method generateSkyTexture.

/**
     * Creates a texture for the sky. The result texture has 6 layers.
     * @param size
     *            the size of the texture (width and height are equal)
     * @param horizontalColor
     *            the horizon color
     * @param zenithColor
     *            the zenith color
     * @param blenderContext
     *            the blender context
     * @return the sky texture
     */
public TextureCubeMap generateSkyTexture(int size, ColorRGBA horizontalColor, ColorRGBA zenithColor, BlenderContext blenderContext) {
    Image image = ImageUtils.createEmptyImage(Format.RGB8, size, size, 6);
    PixelInputOutput pixelIO = PixelIOFactory.getPixelIO(image.getFormat());
    TexturePixel pixel = new TexturePixel();
    float delta = 1 / (float) (size - 1);
    float sideV, sideS = 1, forwardU = 1, forwardV, upS;
    TempVars tempVars = TempVars.get();
    CastFunction castFunction = CAST_FUNCTIONS[blenderContext.getBlenderKey().getSkyGeneratedTextureShape().ordinal()];
    float castRadius = blenderContext.getBlenderKey().getSkyGeneratedTextureRadius();
    for (int x = 0; x < size; ++x) {
        sideV = 1;
        forwardV = 1;
        upS = 0;
        for (int y = 0; y < size; ++y) {
            castFunction.cast(tempVars.vect1.set(1, sideV, sideS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // right
            pixelIO.write(image, NEGATIVE_X, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(0, sideV, 1 - sideS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // left
            pixelIO.write(image, POSITIVE_X, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, forwardV, 0), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // front
            pixelIO.write(image, POSITIVE_Z, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(1 - forwardU, forwardV, 1), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // back
            pixelIO.write(image, NEGATIVE_Z, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, 0, upS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // top
            pixelIO.write(image, NEGATIVE_Y, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, 1, 1 - upS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // bottom
            pixelIO.write(image, POSITIVE_Y, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            sideV = FastMath.clamp(sideV - delta, 0, 1);
            forwardV = FastMath.clamp(forwardV - delta, 0, 1);
            upS = FastMath.clamp(upS + delta, 0, 1);
        }
        sideS = FastMath.clamp(sideS - delta, 0, 1);
        forwardU = FastMath.clamp(forwardU - delta, 0, 1);
    }
    tempVars.release();
    return new TextureCubeMap(image);
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) TextureCubeMap(com.jme3.texture.TextureCubeMap) TempVars(com.jme3.util.TempVars) Image(com.jme3.texture.Image)

Example 7 with ColorRGBA

use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.

the class MaterialContext method applyMaterial.

/**
     * Applies material to a given geometry.
     * 
     * @param geometry
     *            the geometry
     * @param geometriesOMA
     *            the geometries OMA
     * @param userDefinedUVCoordinates
     *            UV coords defined by user
     * @param blenderContext
     *            the blender context
     */
public void applyMaterial(Geometry geometry, Long geometriesOMA, Map<String, List<Vector2f>> userDefinedUVCoordinates, BlenderContext blenderContext) {
    Material material = null;
    if (shadeless) {
        material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        if (!transparent) {
            diffuseColor.a = 1;
        }
        material.setColor("Color", diffuseColor);
    } else {
        material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        material.setBoolean("UseMaterialColors", Boolean.TRUE);
        // setting the colors
        if (!transparent) {
            diffuseColor.a = 1;
        }
        material.setColor("Diffuse", diffuseColor);
        material.setColor("Specular", specularColor);
        material.setFloat("Shininess", shininess);
        material.setColor("Ambient", new ColorRGBA(ambientFactor, ambientFactor, ambientFactor, 1f));
    }
    // applying textures
    int textureIndex = 0;
    if (loadedTextures != null && loadedTextures.size() > 0) {
        if (loadedTextures.size() > TextureHelper.TEXCOORD_TYPES.length) {
            LOGGER.log(Level.WARNING, "The blender file has defined more than {0} different textures. JME supports only {0} UV mappings.", TextureHelper.TEXCOORD_TYPES.length);
        }
        for (CombinedTexture combinedTexture : loadedTextures) {
            if (textureIndex < TextureHelper.TEXCOORD_TYPES.length) {
                String usedUserUVSet = combinedTexture.flatten(geometry, geometriesOMA, userDefinedUVCoordinates, blenderContext);
                this.setTexture(material, combinedTexture.getMappingType(), combinedTexture.getResultTexture());
                List<Vector2f> uvs = combinedTexture.getResultUVS();
                if (uvs != null && uvs.size() > 0) {
                    VertexBuffer uvCoordsBuffer = new VertexBuffer(TextureHelper.TEXCOORD_TYPES[textureIndex++]);
                    uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()])));
                    geometry.getMesh().setBuffer(uvCoordsBuffer);
                }
                if (usedUserUVSet != null) {
                    userDefinedUVCoordinates = new HashMap<>(userDefinedUVCoordinates);
                    userDefinedUVCoordinates.remove(usedUserUVSet);
                }
            } else {
                LOGGER.log(Level.WARNING, "The texture could not be applied because JME only supports up to {0} different UV's.", TextureHelper.TEXCOORD_TYPES.length);
            }
        }
    }
    if (userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) {
        LOGGER.fine("Storing unused, user defined UV coordinates sets.");
        if (userDefinedUVCoordinates.size() > TextureHelper.TEXCOORD_TYPES.length) {
            LOGGER.log(Level.WARNING, "The blender file has defined more than {0} different UV coordinates for the mesh. JME supports only {0} UV coordinates buffers.", TextureHelper.TEXCOORD_TYPES.length);
        }
        for (Entry<String, List<Vector2f>> entry : userDefinedUVCoordinates.entrySet()) {
            if (textureIndex < TextureHelper.TEXCOORD_TYPES.length) {
                List<Vector2f> uvs = entry.getValue();
                VertexBuffer uvCoordsBuffer = new VertexBuffer(TextureHelper.TEXCOORD_TYPES[textureIndex++]);
                uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()])));
                geometry.getMesh().setBuffer(uvCoordsBuffer);
            } else {
                LOGGER.log(Level.WARNING, "The user's UV set named: '{0}' could not be stored because JME only supports up to {1} different UV's.", new Object[] { entry.getKey(), TextureHelper.TEXCOORD_TYPES.length });
            }
        }
    }
    // applying additional data
    material.setName(name);
    if (vertexColor) {
        material.setBoolean(shadeless ? "VertexColor" : "UseVertexColor", true);
    }
    material.getAdditionalRenderState().setFaceCullMode(faceCullMode != null ? faceCullMode : blenderContext.getBlenderKey().getFaceCullMode());
    if (transparent) {
        material.setTransparent(true);
        material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        geometry.setQueueBucket(Bucket.Transparent);
    }
    geometry.setMaterial(material);
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) VertexBuffer(com.jme3.scene.VertexBuffer) Vector2f(com.jme3.math.Vector2f) Material(com.jme3.material.Material) List(java.util.List) CombinedTexture(com.jme3.scene.plugins.blender.textures.CombinedTexture)

Example 8 with ColorRGBA

use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.

the class MaterialHelper method getParticlesMaterial.

/**
     * This method converts the given material into particles-usable material.
     * The texture and glow color are being copied.
     * The method assumes it receives the Lighting type of material.
     * @param material
     *            the source material
     * @param blenderContext
     *            the blender context
     * @return material converted into particles-usable material
     */
public Material getParticlesMaterial(Material material, Integer alphaMaskIndex, BlenderContext blenderContext) {
    Material result = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Particle.j3md");
    // copying texture
    MatParam diffuseMap = material.getParam("DiffuseMap");
    if (diffuseMap != null) {
        Texture texture = ((Texture) diffuseMap.getValue()).clone();
        // applying alpha mask to the texture
        Image image = texture.getImage();
        ByteBuffer sourceBB = image.getData(0);
        sourceBB.rewind();
        int w = image.getWidth();
        int h = image.getHeight();
        ByteBuffer bb = BufferUtils.createByteBuffer(w * h * 4);
        IAlphaMask iAlphaMask = alphaMasks.get(alphaMaskIndex);
        iAlphaMask.setImageSize(w, h);
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                bb.put(sourceBB.get());
                bb.put(sourceBB.get());
                bb.put(sourceBB.get());
                bb.put(iAlphaMask.getAlpha(x, y));
            }
        }
        image = new Image(Format.RGBA8, w, h, bb, ColorSpace.Linear);
        texture.setImage(image);
        result.setTextureParam("Texture", VarType.Texture2D, texture);
    }
    // copying glow color
    MatParam glowColor = material.getParam("GlowColor");
    if (glowColor != null) {
        ColorRGBA color = (ColorRGBA) glowColor.getValue();
        result.setParam("GlowColor", VarType.Vector3, color);
    }
    return result;
}
Also used : MatParam(com.jme3.material.MatParam) ColorRGBA(com.jme3.math.ColorRGBA) Material(com.jme3.material.Material) Image(com.jme3.texture.Image) Texture(com.jme3.texture.Texture) ByteBuffer(java.nio.ByteBuffer)

Example 9 with ColorRGBA

use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.

the class TestFog method simpleInitApp.

public void simpleInitApp() {
    this.flyCam.setMoveSpeed(50);
    Node mainScene = new Node();
    cam.setLocation(new Vector3f(-34.74095f, 95.21318f, -287.4945f));
    cam.setRotation(new Quaternion(0.023536969f, 0.9361278f, -0.016098259f, -0.35050195f));
    // load sky
    mainScene.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
    createTerrain(mainScene);
    DirectionalLight sun = new DirectionalLight();
    Vector3f lightDir = new Vector3f(-0.37352666f, -0.50444174f, -0.7784704f);
    sun.setDirection(lightDir);
    sun.setColor(ColorRGBA.White.clone().multLocal(2));
    mainScene.addLight(sun);
    rootNode.attachChild(mainScene);
    fpp = new FilterPostProcessor(assetManager);
    //fpp.setNumSamples(4);
    int numSamples = getContext().getSettings().getSamples();
    if (numSamples > 0) {
        fpp.setNumSamples(numSamples);
    }
    fog = new FogFilter();
    fog.setFogColor(new ColorRGBA(0.9f, 0.9f, 0.9f, 1.0f));
    fog.setFogDistance(155);
    fog.setFogDensity(1.0f);
    fpp.addFilter(fog);
    viewPort.addProcessor(fpp);
    initInputs();
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) Quaternion(com.jme3.math.Quaternion) Node(com.jme3.scene.Node) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) FilterPostProcessor(com.jme3.post.FilterPostProcessor) FogFilter(com.jme3.post.filters.FogFilter)

Example 10 with ColorRGBA

use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.

the class TestMultiRenderTarget method simpleInitApp.

@Override
public void simpleInitApp() {
    viewPort.addProcessor(this);
    //        flyCam.setEnabled(false);
    cam.setLocation(new Vector3f(4.8037705f, 4.851632f, 10.789033f));
    cam.setRotation(new Quaternion(-0.05143692f, 0.9483723f, -0.21131563f, -0.230846f));
    Node tank = (Node) assetManager.loadModel("Models/HoverTank/Tank2.mesh.xml");
    //tankMesh.getMaterial().setColor("Specular", ColorRGBA.Black);
    rootNode.attachChild(tank);
    display1 = new Picture("Picture");
    // make it appear behind stats view
    display1.move(0, 0, -1);
    display2 = (Picture) display1.clone();
    display3 = (Picture) display1.clone();
    display4 = (Picture) display1.clone();
    display = (Picture) display1.clone();
    ColorRGBA[] colors = new ColorRGBA[] { ColorRGBA.White, ColorRGBA.Blue, ColorRGBA.Cyan, ColorRGBA.DarkGray, ColorRGBA.Green, ColorRGBA.Magenta, ColorRGBA.Orange, ColorRGBA.Pink, ColorRGBA.Red, ColorRGBA.Yellow };
    pls = new PointLight[3];
    for (int i = 0; i < pls.length; i++) {
        PointLight pl = new PointLight();
        pl.setColor(colors[i % colors.length]);
        pl.setRadius(5);
        display.addLight(pl);
        pls[i] = pl;
    }
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) Quaternion(com.jme3.math.Quaternion) Picture(com.jme3.ui.Picture) Vector3f(com.jme3.math.Vector3f) Node(com.jme3.scene.Node) PointLight(com.jme3.light.PointLight)

Aggregations

ColorRGBA (com.jme3.math.ColorRGBA)98 Vector3f (com.jme3.math.Vector3f)65 Material (com.jme3.material.Material)44 DirectionalLight (com.jme3.light.DirectionalLight)42 Geometry (com.jme3.scene.Geometry)23 ParticleEmitter (com.jme3.effect.ParticleEmitter)14 PointLight (com.jme3.light.PointLight)14 Quaternion (com.jme3.math.Quaternion)14 Spatial (com.jme3.scene.Spatial)14 AmbientLight (com.jme3.light.AmbientLight)13 Sphere (com.jme3.scene.shape.Sphere)12 Node (com.jme3.scene.Node)11 KeyTrigger (com.jme3.input.controls.KeyTrigger)10 FilterPostProcessor (com.jme3.post.FilterPostProcessor)10 Texture (com.jme3.texture.Texture)9 BulletAppState (com.jme3.bullet.BulletAppState)8 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)7 Quad (com.jme3.scene.shape.Quad)7 TextureCubeMap (com.jme3.texture.TextureCubeMap)7 EmitterSphereShape (com.jme3.effect.shapes.EmitterSphereShape)6