From b5d9a7507b1290d864f7f3686093e4f54f5e8509 Mon Sep 17 00:00:00 2001 From: ZoeLeeFZ Date: Mon, 26 Aug 2019 22:13:53 +0800 Subject: [PATCH] =?UTF-8?q?!404=20=E5=B9=B3=E8=A1=8C=E5=85=89=E6=8A=95?= =?UTF-8?q?=E5=BD=B1=E8=8C=83=E5=9B=B4=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/LightUtils.ts | 32 +++++++++++++++++++ .../Lights/DirectionalLight.ts | 14 +++----- src/UI/Components/RightPanel/ScenePanel.tsx | 12 ++++--- src/UI/Store/RightPanelStore/LightStore.ts | 21 ++++++++++-- 4 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 src/Common/LightUtils.ts diff --git a/src/Common/LightUtils.ts b/src/Common/LightUtils.ts new file mode 100644 index 000000000..6d0290b56 --- /dev/null +++ b/src/Common/LightUtils.ts @@ -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(); + } + }) + +} diff --git a/src/DatabaseServices/Lights/DirectionalLight.ts b/src/DatabaseServices/Lights/DirectionalLight.ts index d875a9c39..9265c9870 100644 --- a/src/DatabaseServices/Lights/DirectionalLight.ts +++ b/src/DatabaseServices/Lights/DirectionalLight.ts @@ -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); diff --git a/src/UI/Components/RightPanel/ScenePanel.tsx b/src/UI/Components/RightPanel/ScenePanel.tsx index 5592bbde3..eb1883b1d 100644 --- a/src/UI/Components/RightPanel/ScenePanel.tsx +++ b/src/UI/Components/RightPanel/ScenePanel.tsx @@ -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); } } diff --git a/src/UI/Store/RightPanelStore/LightStore.ts b/src/UI/Store/RightPanelStore/LightStore.ts index 7c3cb1682..c724bbabc 100644 --- a/src/UI/Store/RightPanelStore/LightStore.ts +++ b/src/UI/Store/RightPanelStore/LightStore.ts @@ -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); + } }