Applied javadoc patch to SkyFactory from sgold

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10901 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
experimental
rem..om 11 years ago
parent 01419a5fa4
commit d7414262e2
  1. 543
      engine/src/core/com/jme3/util/SkyFactory.java

@ -1,245 +1,298 @@
/* /*
* Copyright (c) 2009-2012 jMonkeyEngine * Copyright (c) 2009-2012 jMonkeyEngine
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* *
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.util; package com.jme3.util;
import com.jme3.asset.AssetManager; import com.jme3.asset.AssetManager;
import com.jme3.asset.TextureKey; import com.jme3.asset.TextureKey;
import com.jme3.bounding.BoundingSphere; import com.jme3.bounding.BoundingSphere;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.Bucket; import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry; import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Sphere; import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Image; import com.jme3.texture.Image;
import com.jme3.texture.Image.Format; import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture; import com.jme3.texture.Texture;
import com.jme3.texture.TextureCubeMap; import com.jme3.texture.TextureCubeMap;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* <code>SkyFactory</code> is used to create jME {@link Spatial}s that can * <code>SkyFactory</code> is used to create jME {@link Spatial}s that can
* be attached to the scene to display a sky image in the background. * be attached to the scene to display a sky image in the background.
* *
* @author Kirill Vainer * @author Kirill Vainer
*/ */
public class SkyFactory { public class SkyFactory {
/** /**
* Creates a sky using the given texture (cubemap or spheremap). * Create a sky with radius=10 using the given cubemap or spheremap texture.
* *
* @param assetManager The asset manager to use to load materials * For the sky to be visible, its radius must fall between the near and far
* @param texture Texture to use for the sky * planes of the camera's frustrum.
* @param normalScale The normal scale is multiplied by the 3D normal *
* to get a texture coordinate. Use Vector3f.UNIT_XYZ to not apply * @param assetManager from which to load materials
* and transformation to the normal. * @param texture to use
* @param sphereMap The way the texture is used * @param normalScale The normal scale is multiplied by the 3D normal to get
* depends on this value:<br> * a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and
* <ul> * transformation to the normal.
* <li>true: Its a Texture2D with the pixels arranged for * @param sphereMap determines how the texture is used:<br>
* <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere mapping</a>.</li> * <ul>
* <li>false: Its either a TextureCubeMap or Texture2D. If its a Texture2D * <li>true: The texture is a Texture2D with the pixels arranged for
* then the image is taken from it and is inserted into a TextureCubeMap</li> * <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere
* </ul> * mapping</a>.</li>
* @return A spatial representing the sky * <li>false: The texture is either a TextureCubeMap or Texture2D. If it is
*/ * a Texture2D then the image is taken from it and is inserted into a
public static Spatial createSky(AssetManager assetManager, Texture texture, Vector3f normalScale, boolean sphereMap) { * TextureCubeMap</li>
return createSky(assetManager, texture, normalScale, sphereMap, 10); * </ul>
} * @return a new spatial representing the sky, ready to be attached to the
* scene graph
/** */
* Creates a sky using the given texture (cubemap or spheremap). public static Spatial createSky(AssetManager assetManager, Texture texture,
* Vector3f normalScale, boolean sphereMap) {
* @param assetManager The asset manager to use to load materials return createSky(assetManager, texture, normalScale, sphereMap, 10);
* @param texture Texture to use for the sky }
* @param normalScale The normal scale is multiplied by the 3D normal
* to get a texture coordinate. Use Vector3f.UNIT_XYZ to not apply /**
* and transformation to the normal. * Create a sky using the given cubemap or spheremap texture.
* @param sphereMap The way the texture is used *
* depends on this value:<br> * @param assetManager from which to load materials
* <ul> * @param texture to use
* <li>true: Its a Texture2D with the pixels arranged for * @param normalScale The normal scale is multiplied by the 3D normal to get
* <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere mapping</a>.</li> * a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and
* <li>false: Its either a TextureCubeMap or Texture2D. If its a Texture2D * transformation to the normal.
* then the image is taken from it and is inserted into a TextureCubeMap</li> * @param sphereMap determines how the texture is used:<br>
* </ul> * <ul>
* @param sphereRadius If specified, this will be the sky sphere's radius. * <li>true: The texture is a Texture2D with the pixels arranged for
* This should be the camera's near plane for optimal quality. * <a href="http://en.wikipedia.org/wiki/Sphere_mapping">sphere
* @return A spatial representing the sky * mapping</a>.</li>
*/ * <li>false: The texture is either a TextureCubeMap or Texture2D. If it is
public static Spatial createSky(AssetManager assetManager, Texture texture, Vector3f normalScale, boolean sphereMap, int sphereRadius) { * a Texture2D then the image is taken from it and is inserted into a
if (texture == null) { * TextureCubeMap</li>
throw new IllegalArgumentException("texture cannot be null"); * </ul>
} * @param sphereRadius the sky sphere's radius: for the sky to be visible,
final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true); * its radius must fall between the near and far planes of the camera's
* frustrum
Geometry sky = new Geometry("Sky", sphereMesh); * @return a new spatial representing the sky, ready to be attached to the
sky.setQueueBucket(Bucket.Sky); * scene graph
sky.setCullHint(Spatial.CullHint.Never); */
sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO)); public static Spatial createSky(AssetManager assetManager, Texture texture,
Vector3f normalScale, boolean sphereMap, int sphereRadius) {
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md"); if (texture == null) {
throw new IllegalArgumentException("texture cannot be null");
skyMat.setVector3("NormalScale", normalScale); }
if (sphereMap) { final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true);
skyMat.setBoolean("SphereMap", sphereMap);
} else if (!(texture instanceof TextureCubeMap)) { Geometry sky = new Geometry("Sky", sphereMesh);
// make sure its a cubemap sky.setQueueBucket(Bucket.Sky);
Image img = texture.getImage(); sky.setCullHint(Spatial.CullHint.Never);
texture = new TextureCubeMap(); sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO));
texture.setImage(img);
} Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md");
skyMat.setTexture("Texture", texture);
sky.setMaterial(skyMat); skyMat.setVector3("NormalScale", normalScale);
if (sphereMap) {
return sky; skyMat.setBoolean("SphereMap", sphereMap);
} } else if (!(texture instanceof TextureCubeMap)) {
// make sure its a cubemap
private static void checkImage(Image image) { Image img = texture.getImage();
// if (image.getDepth() != 1) texture = new TextureCubeMap();
// throw new IllegalArgumentException("3D/Array images not allowed"); texture.setImage(img);
}
if (image.getWidth() != image.getHeight()) { skyMat.setTexture("Texture", texture);
throw new IllegalArgumentException("Image width and height must be the same"); sky.setMaterial(skyMat);
}
return sky;
if (image.getMultiSamples() != 1) { }
throw new IllegalArgumentException("Multisample textures not allowed");
} private static void checkImage(Image image) {
} // if (image.getDepth() != 1)
// throw new IllegalArgumentException("3D/Array images not allowed");
private static void checkImagesForCubeMap(Image... images) {
if (images.length == 1) { if (image.getWidth() != image.getHeight()) {
return; throw new IllegalArgumentException("Image width and height must be the same");
} }
Format fmt = images[0].getFormat(); if (image.getMultiSamples() != 1) {
int width = images[0].getWidth(); throw new IllegalArgumentException("Multisample textures not allowed");
int height = images[0].getHeight(); }
}
ByteBuffer data = images[0].getData(0);
int size = data != null ? data.capacity() : 0; private static void checkImagesForCubeMap(Image... images) {
if (images.length == 1) {
checkImage(images[0]); return;
}
for (int i = 1; i < images.length; i++) {
Image image = images[i]; Format fmt = images[0].getFormat();
checkImage(images[i]); int width = images[0].getWidth();
if (image.getFormat() != fmt) { int height = images[0].getHeight();
throw new IllegalArgumentException("Images must have same format");
} ByteBuffer data = images[0].getData(0);
if (image.getWidth() != width || image.getHeight() != height) { int size = data != null ? data.capacity() : 0;
throw new IllegalArgumentException("Images must have same resolution");
} checkImage(images[0]);
ByteBuffer data2 = image.getData(0);
if (data2 != null){ for (int i = 1; i < images.length; i++) {
if (data2.capacity() != size) { Image image = images[i];
throw new IllegalArgumentException("Images must have same size"); checkImage(images[i]);
} if (image.getFormat() != fmt) {
} throw new IllegalArgumentException("Images must have same format");
} }
} if (image.getWidth() != width || image.getHeight() != height) {
throw new IllegalArgumentException("Images must have same resolution");
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down, Vector3f normalScale) { }
return createSky(assetManager, west, east, north, south, up, down, normalScale, 10); ByteBuffer data2 = image.getData(0);
} if (data2 != null){
if (data2.capacity() != size) {
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down, Vector3f normalScale, int sphereRadius) { throw new IllegalArgumentException("Images must have same size");
final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true); }
Geometry sky = new Geometry("Sky", sphereMesh); }
sky.setQueueBucket(Bucket.Sky); }
sky.setCullHint(Spatial.CullHint.Never); }
sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO));
/**
Image westImg = west.getImage(); * Create a cube-mapped sky with radius=10 using six textures.
Image eastImg = east.getImage(); *
Image northImg = north.getImage(); * For the sky to be visible, its radius must fall between the near and far
Image southImg = south.getImage(); * planes of the camera's frustrum.
Image upImg = up.getImage(); *
Image downImg = down.getImage(); * @param assetManager from which to load materials
* @param west texture for the western face of the cube
checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg); * @param east texture for the eastern face of the cube
* @param north texture for the northern face of the cube
Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null); * @param south texture for the southern face of the cube
* @param up texture for the top face of the cube
cubeImage.addData(westImg.getData(0)); * @param down texture for the bottom face of the cube
cubeImage.addData(eastImg.getData(0)); * @param normalScale The normal scale is multiplied by the 3D normal to get
* a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and
cubeImage.addData(downImg.getData(0)); * transformation to the normal.
cubeImage.addData(upImg.getData(0)); * @return a new spatial representing the sky, ready to be attached to the
* scene graph
cubeImage.addData(southImg.getData(0)); */
cubeImage.addData(northImg.getData(0)); public static Spatial createSky(AssetManager assetManager, Texture west,
Texture east, Texture north, Texture south, Texture up,
if (westImg.getEfficentData() != null){ Texture down, Vector3f normalScale) {
// also consilidate efficient data return createSky(assetManager, west, east, north, south, up, down,
ArrayList<Object> efficientData = new ArrayList<Object>(6); normalScale, 10);
efficientData.add(westImg.getEfficentData()); }
efficientData.add(eastImg.getEfficentData());
efficientData.add(downImg.getEfficentData()); /**
efficientData.add(upImg.getEfficentData()); * Create a cube-mapped sky using six textures.
efficientData.add(southImg.getEfficentData()); *
efficientData.add(northImg.getEfficentData()); * @param assetManager from which to load materials
cubeImage.setEfficentData(efficientData); * @param west texture for the western face of the cube
} * @param east texture for the eastern face of the cube
* @param north texture for the northern face of the cube
TextureCubeMap cubeMap = new TextureCubeMap(cubeImage); * @param south texture for the southern face of the cube
cubeMap.setAnisotropicFilter(0); * @param up texture for the top face of the cube
cubeMap.setMagFilter(Texture.MagFilter.Bilinear); * @param down texture for the bottom face of the cube
cubeMap.setMinFilter(Texture.MinFilter.NearestNoMipMaps); * @param normalScale The normal scale is multiplied by the 3D normal to get
cubeMap.setWrap(Texture.WrapMode.EdgeClamp); * a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and
* transformation to the normal.
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md"); * @param sphereRadius the sky sphere's radius: for the sky to be visible,
skyMat.setTexture("Texture", cubeMap); * its radius must fall between the near and far planes of the camera's
skyMat.setVector3("NormalScale", normalScale); * frustrum
sky.setMaterial(skyMat); * @return a new spatial representing the sky, ready to be attached to the
* scene graph
return sky; */
} public static Spatial createSky(AssetManager assetManager, Texture west,
Texture east, Texture north, Texture south, Texture up,
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down) { Texture down, Vector3f normalScale, float sphereRadius) {
return createSky(assetManager, west, east, north, south, up, down, Vector3f.UNIT_XYZ); final Sphere sphereMesh = new Sphere(10, 10, sphereRadius, false, true);
} Geometry sky = new Geometry("Sky", sphereMesh);
sky.setQueueBucket(Bucket.Sky);
public static Spatial createSky(AssetManager assetManager, Texture texture, boolean sphereMap) { sky.setCullHint(Spatial.CullHint.Never);
return createSky(assetManager, texture, Vector3f.UNIT_XYZ, sphereMap); sky.setModelBound(new BoundingSphere(Float.POSITIVE_INFINITY, Vector3f.ZERO));
}
Image westImg = west.getImage();
public static Spatial createSky(AssetManager assetManager, String textureName, boolean sphereMap) { Image eastImg = east.getImage();
TextureKey key = new TextureKey(textureName, true); Image northImg = north.getImage();
key.setGenerateMips(true); Image southImg = south.getImage();
key.setAsCube(!sphereMap); Image upImg = up.getImage();
Texture tex = assetManager.loadTexture(key); Image downImg = down.getImage();
return createSky(assetManager, tex, sphereMap);
} checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg);
}
Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null);
cubeImage.addData(westImg.getData(0));
cubeImage.addData(eastImg.getData(0));
cubeImage.addData(downImg.getData(0));
cubeImage.addData(upImg.getData(0));
cubeImage.addData(southImg.getData(0));
cubeImage.addData(northImg.getData(0));
if (westImg.getEfficentData() != null){
// also consilidate efficient data
ArrayList<Object> efficientData = new ArrayList<Object>(6);
efficientData.add(westImg.getEfficentData());
efficientData.add(eastImg.getEfficentData());
efficientData.add(downImg.getEfficentData());
efficientData.add(upImg.getEfficentData());
efficientData.add(southImg.getEfficentData());
efficientData.add(northImg.getEfficentData());
cubeImage.setEfficentData(efficientData);
}
TextureCubeMap cubeMap = new TextureCubeMap(cubeImage);
cubeMap.setAnisotropicFilter(0);
cubeMap.setMagFilter(Texture.MagFilter.Bilinear);
cubeMap.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
cubeMap.setWrap(Texture.WrapMode.EdgeClamp);
Material skyMat = new Material(assetManager, "Common/MatDefs/Misc/Sky.j3md");
skyMat.setTexture("Texture", cubeMap);
skyMat.setVector3("NormalScale", normalScale);
sky.setMaterial(skyMat);
return sky;
}
public static Spatial createSky(AssetManager assetManager, Texture west, Texture east, Texture north, Texture south, Texture up, Texture down) {
return createSky(assetManager, west, east, north, south, up, down, Vector3f.UNIT_XYZ);
}
public static Spatial createSky(AssetManager assetManager, Texture texture, boolean sphereMap) {
return createSky(assetManager, texture, Vector3f.UNIT_XYZ, sphereMap);
}
public static Spatial createSky(AssetManager assetManager, String textureName, boolean sphereMap) {
TextureKey key = new TextureKey(textureName, true);
key.setGenerateMips(true);
key.setAsCube(!sphereMap);
Texture tex = assetManager.loadTexture(key);
return createSky(assetManager, tex, sphereMap);
}
}

Loading…
Cancel
Save