use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.
the class EnvMapUtils method getCubeMapCrossDebugView.
/**
* Creates a debug Node of the given cube map to attach to the gui node
*
* the cube map is layered this way :
* <pre>
* _____
* | |
* | +Y |
* _____|_____|_____ _____
* | | | | |
* | -X | +Z | +X | -Z |
* |_____|_____|_____|_____|
* | |
* | -Y |
* |_____|
*
*</pre>
*
* @param cubeMap the cube map
* @param assetManager the asset Manager
* @return
*/
public static Node getCubeMapCrossDebugView(TextureCubeMap cubeMap, AssetManager assetManager) {
Node n = new Node("CubeMapDebug" + cubeMap.getName());
int size = cubeMap.getImage().getWidth();
Picture[] pics = new Picture[6];
float ratio = 128f / (float) size;
for (int i = 0; i < 6; i++) {
pics[i] = new Picture("bla");
Texture2D tex = new Texture2D(new Image(cubeMap.getImage().getFormat(), size, size, cubeMap.getImage().getData(i), cubeMap.getImage().getColorSpace()));
pics[i].setTexture(assetManager, tex, true);
pics[i].setWidth(size);
pics[i].setHeight(size);
n.attachChild(pics[i]);
}
pics[0].setLocalTranslation(size, size * 2, 1);
pics[0].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
pics[1].setLocalTranslation(size * 3, size * 2, 1);
pics[1].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
pics[2].setLocalTranslation(size * 2, size * 3, 1);
pics[2].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
pics[3].setLocalTranslation(size * 2, size, 1);
pics[3].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
pics[4].setLocalTranslation(size * 2, size * 2, 1);
pics[4].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
pics[5].setLocalTranslation(size * 4, size * 2, 1);
pics[5].setLocalRotation(new Quaternion().fromAngleAxis(PI, Vector3f.UNIT_Z));
Quad q = new Quad(size * 4, size * 3);
Geometry g = new Geometry("bg", q);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Black);
g.setMaterial(mat);
g.setLocalTranslation(0, 0, 0);
n.attachChild(g);
n.setLocalScale(ratio);
return n;
}
use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.
the class MatParam method getValueAsString.
/**
* Returns the material parameter value as it would appear in a J3M
* file. E.g.<br/>
* <code>
* MaterialParameters {<br/>
* ABC : 1 2 3 4<br/>
* }<br/>
* </code>
* Assuming "ABC" is a Vector4 parameter, then the value
* "1 2 3 4" would be returned by this method.
* <br/><br/>
* @return material parameter value as it would appear in a J3M file.
*/
public String getValueAsString() {
switch(type) {
case Boolean:
case Float:
case Int:
return value.toString();
case Vector2:
Vector2f v2 = (Vector2f) value;
return v2.getX() + " " + v2.getY();
/*
This may get used at a later point of time
When arrays can be inserted in J3M files
case Vector2Array:
Vector2f[] v2Arr = (Vector2f[]) value;
String v2str = "";
for (int i = 0; i < v2Arr.length ; i++) {
v2str += v2Arr[i].getX() + " " + v2Arr[i].getY() + "\n";
}
return v2str;
*/
case Vector3:
Vector3f v3 = (Vector3f) value;
return v3.getX() + " " + v3.getY() + " " + v3.getZ();
/*
case Vector3Array:
Vector3f[] v3Arr = (Vector3f[]) value;
String v3str = "";
for (int i = 0; i < v3Arr.length ; i++) {
v3str += v3Arr[i].getX() + " "
+ v3Arr[i].getY() + " "
+ v3Arr[i].getZ() + "\n";
}
return v3str;
case Vector4Array:
// can be either ColorRGBA, Vector4f or Quaternion
if (value instanceof Vector4f) {
Vector4f[] v4arr = (Vector4f[]) value;
String v4str = "";
for (int i = 0; i < v4arr.length ; i++) {
v4str += v4arr[i].getX() + " "
+ v4arr[i].getY() + " "
+ v4arr[i].getZ() + " "
+ v4arr[i].getW() + "\n";
}
return v4str;
} else if (value instanceof ColorRGBA) {
ColorRGBA[] colorArr = (ColorRGBA[]) value;
String colStr = "";
for (int i = 0; i < colorArr.length ; i++) {
colStr += colorArr[i].getRed() + " "
+ colorArr[i].getGreen() + " "
+ colorArr[i].getBlue() + " "
+ colorArr[i].getAlpha() + "\n";
}
return colStr;
} else if (value instanceof Quaternion) {
Quaternion[] quatArr = (Quaternion[]) value;
String quatStr = "";
for (int i = 0; i < quatArr.length ; i++) {
quatStr += quatArr[i].getX() + " "
+ quatArr[i].getY() + " "
+ quatArr[i].getZ() + " "
+ quatArr[i].getW() + "\n";
}
return quatStr;
} else {
throw new UnsupportedOperationException("Unexpected Vector4Array type: " + value);
}
*/
case Vector4:
// can be either ColorRGBA, Vector4f or Quaternion
if (value instanceof Vector4f) {
Vector4f v4 = (Vector4f) value;
return v4.getX() + " " + v4.getY() + " " + v4.getZ() + " " + v4.getW();
} else if (value instanceof ColorRGBA) {
ColorRGBA color = (ColorRGBA) value;
return color.getRed() + " " + color.getGreen() + " " + color.getBlue() + " " + color.getAlpha();
} else if (value instanceof Quaternion) {
Quaternion quat = (Quaternion) value;
return quat.getX() + " " + quat.getY() + " " + quat.getZ() + " " + quat.getW();
} else {
throw new UnsupportedOperationException("Unexpected Vector4 type: " + value);
}
case Texture2D:
case Texture3D:
case TextureArray:
case TextureBuffer:
case TextureCubeMap:
Texture texVal = (Texture) value;
TextureKey texKey = (TextureKey) texVal.getKey();
if (texKey == null) {
// often does as well, even implicitly.
return texVal + ":returned null key";
}
String ret = "";
if (texKey.isFlipY()) {
ret += "Flip ";
}
//Wrap mode
ret += getWrapMode(texVal, Texture.WrapAxis.S);
ret += getWrapMode(texVal, Texture.WrapAxis.T);
ret += getWrapMode(texVal, Texture.WrapAxis.R);
//Min and Mag filter
Texture.MinFilter def = Texture.MinFilter.BilinearNoMipMaps;
if (texVal.getImage().hasMipmaps() || texKey.isGenerateMips()) {
def = Texture.MinFilter.Trilinear;
}
if (texVal.getMinFilter() != def) {
ret += "Min" + texVal.getMinFilter().name() + " ";
}
if (texVal.getMagFilter() != Texture.MagFilter.Bilinear) {
ret += "Mag" + texVal.getMagFilter().name() + " ";
}
return ret + "\"" + texKey.getName() + "\"";
default:
// parameter type not supported in J3M
return null;
}
}
use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.
the class WaterFilter method initFilter.
@Override
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
if (reflectionScene == null) {
reflectionScene = vp.getScenes().get(0);
DirectionalLight l = findLight((Node) reflectionScene);
if (l != null) {
lightDirection = l.getDirection();
}
}
this.renderManager = renderManager;
this.viewPort = vp;
reflectionPass = new Pass();
reflectionPass.init(renderManager.getRenderer(), reflectionMapSize, reflectionMapSize, Format.RGBA8, Format.Depth);
reflectionCam = new Camera(reflectionMapSize, reflectionMapSize);
reflectionView = new ViewPort("reflectionView", reflectionCam);
reflectionView.setClearFlags(true, true, true);
reflectionView.attachScene(reflectionScene);
reflectionView.setOutputFrameBuffer(reflectionPass.getRenderFrameBuffer());
plane = new Plane(Vector3f.UNIT_Y, new Vector3f(0, waterHeight, 0).dot(Vector3f.UNIT_Y));
reflectionProcessor = new ReflectionProcessor(reflectionCam, reflectionPass.getRenderFrameBuffer(), plane);
reflectionProcessor.setReflectionClipPlane(plane);
reflectionView.addProcessor(reflectionProcessor);
normalTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/water_normalmap.dds");
if (foamTexture == null) {
foamTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/foam.jpg");
}
if (causticsTexture == null) {
causticsTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/caustics.jpg");
}
heightTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/heightmap.jpg");
normalTexture.setWrap(WrapMode.Repeat);
foamTexture.setWrap(WrapMode.Repeat);
causticsTexture.setWrap(WrapMode.Repeat);
heightTexture.setWrap(WrapMode.Repeat);
material = new Material(manager, "Common/MatDefs/Water/Water.j3md");
material.setTexture("HeightMap", heightTexture);
material.setTexture("CausticsMap", causticsTexture);
material.setTexture("FoamMap", foamTexture);
material.setTexture("NormalMap", normalTexture);
material.setTexture("ReflectionMap", reflectionPass.getRenderedTexture());
material.setFloat("WaterTransparency", waterTransparency);
material.setFloat("NormalScale", normalScale);
material.setFloat("R0", refractionConstant);
material.setFloat("MaxAmplitude", maxAmplitude);
material.setVector3("LightDir", lightDirection);
material.setColor("LightColor", lightColor);
material.setFloat("ShoreHardness", shoreHardness);
material.setFloat("RefractionStrength", refractionStrength);
material.setFloat("WaveScale", waveScale);
material.setVector3("FoamExistence", foamExistence);
material.setFloat("SunScale", sunScale);
material.setVector3("ColorExtinction", colorExtinction);
material.setFloat("Shininess", shininess);
material.setColor("WaterColor", waterColor);
material.setColor("DeepWaterColor", deepWaterColor);
material.setVector2("WindDirection", windDirection);
material.setFloat("FoamHardness", foamHardness);
material.setBoolean("UseRipples", useRipples);
material.setBoolean("UseHQShoreline", useHQShoreline);
material.setBoolean("UseSpecular", useSpecular);
material.setBoolean("UseFoam", useFoam);
material.setBoolean("UseCaustics", useCaustics);
material.setBoolean("UseRefraction", useRefraction);
material.setFloat("ReflectionDisplace", reflectionDisplace);
material.setFloat("FoamIntensity", foamIntensity);
material.setFloat("UnderWaterFogDistance", underWaterFogDistance);
material.setFloat("CausticsIntensity", causticsIntensity);
if (center != null) {
material.setVector3("Center", center);
material.setFloat("Radius", radius * radius);
material.setBoolean("SquareArea", shapeType == AreaShape.Square);
}
material.setFloat("WaterHeight", waterHeight);
}
use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.
the class TranslucentBucketFilter method initFilter.
@Override
protected void initFilter(AssetManager manager, RenderManager rm, ViewPort vp, int w, int h) {
this.renderManager = rm;
this.viewPort = vp;
material = new Material(manager, "Common/MatDefs/Post/Overlay.j3md");
material.setColor("Color", ColorRGBA.White);
Texture2D tex = processor.getFilterTexture();
material.setTexture("Texture", tex);
if (tex.getImage().getMultiSamples() > 1) {
material.setInt("NumSamples", tex.getImage().getMultiSamples());
} else {
material.clearParam("NumSamples");
}
renderManager.setHandleTranslucentBucket(false);
if (enabledSoftParticles && depthTexture != null) {
initSoftParticles(vp, true);
}
}
use of com.jme3.texture.Texture2D in project jmonkeyengine by jMonkeyEngine.
the class CombinedTexture method flatten.
/**
* This method flattens the texture and creates a single result of Texture2D
* type.
*
* @param geometry
* the geometry the texture is created for
* @param geometriesOMA
* the old memory address of the geometries list that the given
* geometry belongs to (needed for bounding box creation)
* @param userDefinedUVCoordinates
* the UV's defined by user (null or zero length table if none
* were defined)
* @param blenderContext
* the blender context
* @return the name of the user UV coordinates used (null if the UV's were
* generated)
*/
public String flatten(Geometry geometry, Long geometriesOMA, Map<String, List<Vector2f>> userDefinedUVCoordinates, BlenderContext blenderContext) {
Mesh mesh = geometry.getMesh();
Texture previousTexture = null;
UVCoordinatesType masterUVCoordinatesType = null;
String masterUserUVSetName = null;
for (TextureData textureData : textureDatas) {
// decompress compressed textures (all will be merged into one texture anyway)
if (textureDatas.size() > 1 && textureData.texture.getImage().getFormat().isCompressed()) {
textureData.texture.setImage(ImageUtils.decompress(textureData.texture.getImage()));
textureData.textureBlender = TextureBlenderFactory.alterTextureType(textureData.texture.getImage().getFormat(), textureData.textureBlender);
}
if (previousTexture == null) {
// the first texture will lead the others to its shape
if (textureData.texture instanceof GeneratedTexture) {
resultTexture = ((GeneratedTexture) textureData.texture).triangulate(mesh, geometriesOMA, textureData.uvCoordinatesType, blenderContext);
} else if (textureData.texture instanceof Texture2D) {
resultTexture = textureData.texture;
if (textureData.uvCoordinatesType == UVCoordinatesType.TEXCO_UV && userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) {
if (textureData.uvCoordinatesName == null) {
// get the first UV available
resultUVS = userDefinedUVCoordinates.values().iterator().next();
} else {
resultUVS = userDefinedUVCoordinates.get(textureData.uvCoordinatesName);
}
if (resultUVS == null && LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("The texture " + textureData.texture.getName() + " has assigned non existing UV coordinates group: " + textureData.uvCoordinatesName + ".");
}
masterUserUVSetName = textureData.uvCoordinatesName;
} else {
TemporalMesh temporalMesh = (TemporalMesh) blenderContext.getLoadedFeature(geometriesOMA, LoadedDataType.TEMPORAL_MESH);
resultUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, temporalMesh);
}
}
this.blend(resultTexture, textureData.textureBlender, blenderContext);
previousTexture = resultTexture;
masterUVCoordinatesType = textureData.uvCoordinatesType;
} else {
if (textureData.texture instanceof GeneratedTexture) {
if (!(resultTexture instanceof TriangulatedTexture)) {
resultTexture = new TriangulatedTexture((Texture2D) resultTexture, resultUVS, blenderContext);
resultUVS = null;
previousTexture = resultTexture;
}
TriangulatedTexture triangulatedTexture = ((GeneratedTexture) textureData.texture).triangulate(mesh, geometriesOMA, textureData.uvCoordinatesType, blenderContext);
triangulatedTexture.castToUVS((TriangulatedTexture) resultTexture, blenderContext);
triangulatedTexture.blend(textureData.textureBlender, (TriangulatedTexture) resultTexture, blenderContext);
resultTexture = previousTexture = triangulatedTexture;
} else if (textureData.texture instanceof Texture2D) {
if (this.isUVTypesMatch(masterUVCoordinatesType, masterUserUVSetName, textureData.uvCoordinatesType, textureData.uvCoordinatesName) && resultTexture instanceof Texture2D) {
this.scale((Texture2D) textureData.texture, resultTexture.getImage().getWidth(), resultTexture.getImage().getHeight());
ImageUtils.merge(resultTexture.getImage(), textureData.texture.getImage());
previousTexture = resultTexture;
} else {
if (!(resultTexture instanceof TriangulatedTexture)) {
resultTexture = new TriangulatedTexture((Texture2D) resultTexture, resultUVS, blenderContext);
resultUVS = null;
}
// first triangulate the current texture
List<Vector2f> textureUVS = null;
if (textureData.uvCoordinatesType == UVCoordinatesType.TEXCO_UV && userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) {
if (textureData.uvCoordinatesName == null) {
// get the first UV available
textureUVS = userDefinedUVCoordinates.values().iterator().next();
} else {
textureUVS = userDefinedUVCoordinates.get(textureData.uvCoordinatesName);
}
} else {
TemporalMesh geometries = (TemporalMesh) blenderContext.getLoadedFeature(geometriesOMA, LoadedDataType.TEMPORAL_MESH);
textureUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries);
}
TriangulatedTexture triangulatedTexture = new TriangulatedTexture((Texture2D) textureData.texture, textureUVS, blenderContext);
// then move the texture to different UV's
triangulatedTexture.castToUVS((TriangulatedTexture) resultTexture, blenderContext);
// merge triangulated textures
for (int i = 0; i < ((TriangulatedTexture) resultTexture).getFaceTextureCount(); ++i) {
ImageUtils.merge(((TriangulatedTexture) resultTexture).getFaceTextureElement(i).image, triangulatedTexture.getFaceTextureElement(i).image);
}
}
}
}
}
if (resultTexture instanceof TriangulatedTexture) {
if (mappingType == MaterialContext.MTEX_NOR) {
for (int i = 0; i < ((TriangulatedTexture) resultTexture).getFaceTextureCount(); ++i) {
TriangleTextureElement triangleTextureElement = ((TriangulatedTexture) resultTexture).getFaceTextureElement(i);
// TODO: get proper strength factor
triangleTextureElement.image = ImageUtils.convertToNormalMapTexture(triangleTextureElement.image, 1);
}
}
resultUVS = ((TriangulatedTexture) resultTexture).getResultUVS();
resultTexture = ((TriangulatedTexture) resultTexture).getResultTexture();
masterUserUVSetName = null;
}
// setting additional data
resultTexture.setWrap(WrapMode.Repeat);
// the filters are required if generated textures are used because
// otherwise ugly lines appear between the mesh faces
resultTexture.setMagFilter(MagFilter.Nearest);
resultTexture.setMinFilter(MinFilter.NearestNoMipMaps);
return masterUserUVSetName;
}
Aggregations