Sorry I don’t seem to have succeeded in modifying the previous content due to internet reasons … And I forgot to check for other things.
package com.mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.light.SpotLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Image;
import com.jme3.texture.Texture2D;
import com.jme3.texture.image.ColorSpace;
import com.jme3.texture.image.ImageRaster;
import com.jme3.util.BufferUtils;
import java.nio.ByteBuffer;
/**
* 你的第一个jME3程序
* @author yanmaoyuan
*/
public class JmeBox extends SimpleApplication {
// 定向光
private DirectionalLight sunLight;
// 环境光
private AmbientLight ambientLight;
/**
* 初始化3D场景,显示一个方块。
*/
@Override
public void simpleInitApp() {
Mesh box = new Box(1, 1, 1);
// 添加光源
addLight();
Material mat = new Material(assetManager, "Shaders/FogOfWar/FOWLighting.j3md");
mat.setTexture("FowMap", image());
Geometry geom = new Geometry("Box");
geom.setMesh(box);
geom.setMaterial(mat);
// 环境光
AmbientLight ambient = new AmbientLight();
rootNode.attachChild(geom);
rootNode.addLight(ambient);
}
public static void main(String[] args) {
// 启动jME3程序
JmeBox app = new JmeBox();
app.start();
}
public Texture2D image() {
// Create the raw FOW overlay image
int size = 1024;
ByteBuffer data = BufferUtils.createByteBuffer(size * size * 4); // square image, four bytes per color
Image image = new Image(Image.Format.ABGR8, size, size, data, ColorSpace.Linear);
// Create a raster that we can draw to
ImageRaster raster = ImageRaster.create(image);
for( int i = 0; i < 1024; i++ ) {
for( int j = 0; j < 1024; j++ ) {
float f = (1f - i/1024) * (1f - j/1024);
ColorRGBA color = new ColorRGBA(f, f, f, 1.0f);
raster.setPixel(i, j, color);
}
}
// Create the texture to set to the FowMap on materials
Texture2D fowMap = new Texture2D(image);
return fowMap;
}
/**
* Adding a light source
*/
private void addLight() {
// 定向光
sunLight = new DirectionalLight();
sunLight.setDirection(new Vector3f(-1, -2, -3));
sunLight.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
// 环境光
ambientLight = new AmbientLight();
ambientLight.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
// 将模型和光源添加到场景图中
rootNode.addLight(sunLight);
rootNode.addLight(ambientLight);
}
}
#ifdef HAS_FOW
// Get the x,z location of the vertex in world space
vec2 worldXz = (TransformWorld(modelSpacePos)).xz;
// Change the world space coordinate to texture space 0..1
vec2 fowUv = worldXz - vec2(0, 0); // use your actual values here in format like 0.0
fowUv /= vec2(1024.0, 1024.0);
// Sample the texture
vec4 fow = texture2D(m_FowMap, worldXz);
// adjust lighting by the fog of war value
DiffuseSum *= vec4(fow.rgb, 1.0);
AmbientSum *= fow.rgb;
SpecularSum *= fow.rgb;
#endif
#ifdef VERTEX_COLOR
AmbientSum *= inColor.rgb;
DiffuseSum *= inColor;
#endif
DiffuseSum = vec4(1.0, 0.0, 0.0, 1.0);
Technique {
LightMode MultiPass
VertexShader GLSL310 GLSL300 GLSL100 GLSL150: Shaders/FogOfWar/FOWLighting.vert
FragmentShader GLSL310 GLSL300 GLSL100 GLSL150: Shaders/FogOfWar/FOWLighting.frag
WorldParameters {
WorldViewProjectionMatrix
NormalMatrix
WorldViewMatrix
ViewMatrix
CameraPosition
WorldMatrix
ViewProjectionMatrix
}
Defines {
VERTEX_COLOR : UseVertexColor
VERTEX_LIGHTING : VertexLighting
MATERIAL_COLORS : UseMaterialColors
DIFFUSEMAP : DiffuseMap
NORMALMAP : NormalMap
SPECULARMAP : SpecularMap
PARALLAXMAP : ParallaxMap
NORMALMAP_PARALLAX : PackedNormalParallax
STEEP_PARALLAX : SteepParallax
ALPHAMAP : AlphaMap
COLORRAMP : ColorRamp
LIGHTMAP : LightMap
SEPARATE_TEXCOORD : SeparateTexCoord
DISCARD_ALPHA : AlphaDiscardThreshold
USE_REFLECTION : EnvMap
SPHERE_MAP : EnvMapAsSphereMap
NUM_BONES : NumberOfBones
INSTANCING : UseInstancing
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
// fog - jayfella
USE_FOG : UseFog
FOG_LINEAR : LinearFog
FOG_EXP : ExpFog
FOG_EXPSQ : ExpSqFog
//add
HAS_FOW : FowMap
}
}