!404 平行光投影范围计算

pull/404/MERGE
ZoeLeeFZ 5 years ago committed by ChenX
parent 0c9827e093
commit b5d9a7507b

@ -0,0 +1,32 @@
import { Box3, DirectionalLight as TDirLight, Mesh, OrthographicCamera } from "three";
import { app } from "../ApplicationServices/Application";
import { DirectionalLight } from "../DatabaseServices/Lights/DirectionalLight";
import { GetBox } from "../Geometry/GeUtils";
import { CameraUpdate } from "../GraphicsSystem/CameraUpdate";
let cameraUpdate = new CameraUpdate();
cameraUpdate.SetSize(2028, 2048);
export function DirLightShadowArea(light: DirectionalLight)
{
let box = new Box3();
//场景内的包围盒
for (let obj of app.Viewer.VisibleObjects)
if (obj instanceof Mesh)
box.union(GetBox(obj));
let direction = light.Position.clone().sub(light.Target).normalize();
cameraUpdate.LookAt(direction.negate());
cameraUpdate.ZoomExtensBox3(box);
light.DrawObject.traverse(o =>
{
if (o instanceof TDirLight)
{
o.shadow.camera.copy(cameraUpdate.Camera as OrthographicCamera);
o.shadow.camera.updateProjectionMatrix();
}
})
}

@ -1,4 +1,4 @@
import { DirectionalLightHelper, DirectionalLight as TDirectionalLight, Matrix4, Object3D, Vector3, Group } from "three";
import { DirectionalLightHelper, DirectionalLight as TDirectionalLight, Matrix4, Object3D, Vector3, Group, Vector2 } from "three";
import { UpdateDraw } from "../../Common/Status";
import { equalv3 } from "../../Geometry/GeUtils";
import { RenderType } from "../../GraphicsSystem/RenderType";
@ -13,6 +13,7 @@ import { HostApplicationServices } from "../../ApplicationServices/HostApplicati
@Factory
export class DirectionalLight extends Light
{
OnlyRenderType = true;
private _Target = new Vector3();
protected _ShowHelper = false;
@ -53,18 +54,15 @@ export class DirectionalLight extends Light
}
protected InitDrawObject(renderType: RenderType = RenderType.Wireframe): Object3D
{
// if (renderType !== RenderType.Physical) return;
let lightGroup = new Group();
let light = new TDirectionalLight(this._LightColor, this._Intensity);
light.castShadow = HostApplicationServices.UseShadow;
light.shadow.camera.matrixAutoUpdate = true;
light.shadow.camera.near = 1;
light.shadow.camera.far = 100000;
light.shadow.camera.left = -1500;
light.shadow.camera.right = 1500;
light.shadow.camera.top = 1500;
light.shadow.camera.bottom = -1500;
light.shadow.mapSize = new Vector2(2048, 2048);
let helper = new DirectionalLightHelper(light, 500);
helper.lightPlane.matrixAutoUpdate = true;
lightGroup.add(light, helper);
lightGroup.matrixAutoUpdate = false;
@ -75,8 +73,6 @@ export class DirectionalLight extends Light
}
UpdateDrawObject(type: RenderType, en: Object3D)
{
// if (type !== RenderType.Physical) return;
let light = en.children[0] as TDirectionalLight;
super.UpdateDrawObject(type, light);
light.target.position.copy(this._Target);

@ -19,7 +19,8 @@ enum ETime
Evening = "傍晚",
}
const DefaultDist = 2000;
/**太阳光照射距离 */
export const DefaultDist = { dist: 10000 };
@inject("store")
@observer
@ -32,9 +33,10 @@ export class ScenePanel extends React.Component<{ store?: RightPanelStore }, {}>
}
private SetSunPostion(angle: number, rotation: number)
{
let pos = new Vector3(DefaultDist * Math.cos(angle), 0, DefaultDist * Math.sin(angle));
let light = this.props.store.lightStore.m_DirectionalLight;
let pos = new Vector3(DefaultDist.dist * Math.cos(angle), 0, DefaultDist.dist * Math.sin(angle));
pos.applyMatrix4(new Matrix4().makeRotationZ(rotation));
this.props.store.lightStore.m_DirectionalLight.Position = pos;
light.Position = pos.add(light.Target);
app.Editor.UpdateScreen();
}
private handleSuntime = () =>
@ -95,9 +97,9 @@ export class ScenePanel extends React.Component<{ store?: RightPanelStore }, {}>
this.handleSuntime();
else
{
let vec = sun.Position.setZ(0);
let vec = sun.Position.setZ(0).sub(sun.Target);
this.sunState.angle = equalv3(vec, new Vector3()) ? "60" : FixedNotZero(TMath.radToDeg(vec.angleTo(sun.Position)), 0);
this.sunState.angle = equalv3(vec, new Vector3()) ? "60" : FixedNotZero(TMath.radToDeg(vec.angleTo(sun.Position.sub(sun.Target))), 0);
this.sunState.rotation = FixedNotZero(TMath.radToDeg(angle(vec)), 0);
}
}

@ -1,8 +1,9 @@
import { autorun, observable } from "mobx";
import { Light as TLight, PointLight, SpotLight, DirectionalLight as TDirectionalLight } from 'three';
import { DirectionalLight as TDirectionalLight, Light as TLight, PointLight, SpotLight } from 'three';
import { end } from "xaop";
import { app } from "../../../ApplicationServices/Application";
import { HostApplicationServices } from "../../../ApplicationServices/HostApplicationServices";
import { DirLightShadowArea } from "../../../Common/LightUtils";
import { Entity } from "../../../DatabaseServices/Entity/Entity";
import { AmbientLight } from "../../../DatabaseServices/Lights/AmbientLight";
import { DirectionalLight } from "../../../DatabaseServices/Lights/DirectionalLight";
@ -75,12 +76,13 @@ export class LightStore
{
lg.Erase();
}
this.UpdateDirLightShadowArea();
app.Editor.UpdateScreen();
}
InitScene()
{
commandMachine.CommandStart("初始化灯光");
this.isShowShadow = false;
this.m_ShowAmbientLight = true;
this.m_AmbientLight = new AmbientLight();
this.m_ShowDirLight = false;
@ -124,6 +126,9 @@ export class LightStore
if (obj instanceof PointLight || obj instanceof SpotLight || obj instanceof TDirectionalLight)
obj.castShadow = isShow;
});
this.UpdateDirLightShadowArea();
app.Editor.UpdateScreen();
}
ClearSceneLights()
@ -158,6 +163,12 @@ export class LightStore
{
lgStore.WatchLight(ent, false);
});
app.CommandReactor.OnCommandEnd((name, changeo, creawteo) =>
{
this.UpdateDirLightShadowArea();
});
}
private WatchLight(lg: Entity, IsErase: boolean)
{
@ -180,4 +191,10 @@ export class LightStore
this.m_ShowAmbientLight = !lg.IsErase;
}
}
private UpdateDirLightShadowArea()
{
if (HostApplicationServices.UseShadow && this.m_ShowDirLight)
DirLightShadowArea(this.m_DirectionalLight);
}
}

Loading…
Cancel
Save