⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleshadow.java

📁 Java具有平台独立、面向对象、多线程等许多优点
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Label;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;

/**
 * ShadowApp creates a single plane 
 */
public class SimpleShadow extends Applet {
	
	public class TriPlane extends Shape3D {

		TriPlane(Point3f A,Point3f B,Point3f C){
			this.setGeometry (createGeometry3(A,B,C));
			this.setAppearance(createAppearance());
		}
		
		Geometry createGeometry3(Point3f A, Point3f B, Point3f C){
			TriangleArray plane = new TriangleArray(3, GeometryArray.COORDINATES
													   | GeometryArray.NORMALS);

			plane.setCoordinate(0, A);
			plane.setCoordinate(1, B);
			plane.setCoordinate(2, C);
			
			Vector3f a = new Vector3f(A.x - B.x, A.y - B.y, A.z - B.z);
			Vector3f b = new Vector3f(C.x - B.x, C.y - B.y, C.z - B.z);
			Vector3f n = new Vector3f();
			n.cross(b, a);

			n.normalize();

			plane.setNormal(0, n);
			plane.setNormal(1, n);
			plane.setNormal(2, n);
			
			return plane;
		}
		
		Appearance createAppearance() {
			Appearance appear = new Appearance();
			Material material = new Material();
			appear.setMaterial(material);

			return appear;
		}
	}
	
	public class QuadPlane extends Shape3D {
	
		QuadPlane(Point3f A, Point3f B, Point3f C, Point3f D) {
			this.setGeometry(createGeometry4(A, B, C, D));
			this.setAppearance(createAppearance());
		} 
		
		Geometry createGeometry4(Point3f A, Point3f B, Point3f C, Point3f D){
			QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES
											   | GeometryArray.NORMALS);

			plane.setCoordinate(0, A);
			plane.setCoordinate(1, B);
			plane.setCoordinate(2, C);
			plane.setCoordinate(3, D);

			Vector3f a = new Vector3f(A.x - B.x, A.y - B.y, A.z - B.z);
			Vector3f b = new Vector3f(C.x - B.x, C.y - B.y, C.z - B.z);
			Vector3f n = new Vector3f();
			n.cross(b, a);

			n.normalize();

			plane.setNormal(0, n);
			plane.setNormal(1, n);
			plane.setNormal(2, n);
			plane.setNormal(3, n);

			return plane;
		}

		Appearance createAppearance() {
			Appearance appear = new Appearance();
			Material material = new Material();
			appear.setMaterial(material);

			return appear;
		}

	}


	public class Shadow3D extends Shape3D {

		Shadow3D(GeometryArray geom, Vector3f direction,
				 Color3f col, float height) {

			int vCount = geom.getVertexCount();
			TriangleArray poly = new TriangleArray (vCount,GeometryArray.COORDINATES 
																   | GeometryArray.COLOR_3 );
			int v;
			Point3f vertex = new Point3f();
			Point3f shadow = new Point3f();
			for (v = 0; v < vCount; v++) 
			{
				geom.getCoordinate(v, vertex);
				shadow.set( vertex.x + (vertex.y-height) * direction.x,
							height + 0.0001f,
							vertex.z + (vertex.y-height) * direction.y);
				poly.setCoordinate(v, shadow);
				poly.setColor(v, col);
			}

			this.setGeometry(poly);
		}

	} 

	public SimpleShadow (){
		setLayout(new BorderLayout());
		Canvas3D c = new Canvas3D(null);
		add("Center", c);
		
		BranchGroup scene = new BranchGroup();

		Shape3D plane = new TriPlane(new Point3f(0.0f,0.6f,-0.2f),
										new Point3f(-0.3f,-0.3f,0.2f),
										new Point3f(0.6f,-0.3f,0.2f));
		
		scene.addChild(plane);

		Shape3D floor = new QuadPlane(new Point3f(-1.0f, -0.5f, -1.0f),
									new Point3f(-1.0f, -0.5f,  1.0f),
									new Point3f( 1.0f, -0.5f,  1.0f),
									new Point3f( 1.0f, -0.5f, -1.0f));
		scene.addChild(floor);


		AmbientLight lightA = new AmbientLight();
		lightA.setInfluencingBounds(new BoundingSphere());
		scene.addChild(lightA);

		DirectionalLight lightD1 = new DirectionalLight();
		lightD1.setInfluencingBounds(new BoundingSphere());
		Vector3f direction = new Vector3f(-1.0f, -1.0f, -1.0f);
		direction.normalize();
		lightD1.setDirection(direction);
		scene.addChild(lightD1);

		Shape3D shadow = new Shadow3D((GeometryArray) plane.getGeometry(),
									  direction,
									  new Color3f( 0.2f, 0.2f,  0.2f),
									  -0.5f);
		scene.addChild(shadow);


		SimpleUniverse u = new SimpleUniverse(c);

		// This will move the ViewPlatform back a bit so the
		// objects in the scene can be viewed.
		u.getViewingPlatform().setNominalViewingTransform();

		u.addBranchGraph(scene);
	}
	
	public static void main(String argv[])
	{
		new MainFrame(new SimpleShadow(), 256, 256);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -