diff --git a/src/Add-on/TestDrawDirectionalLight.ts b/src/Add-on/TestDrawDirectionalLight.ts new file mode 100644 index 000000000..5c7329f67 --- /dev/null +++ b/src/Add-on/TestDrawDirectionalLight.ts @@ -0,0 +1,22 @@ +import { Command } from "../Editor/CommandMachine"; +import { DirectionalLight } from "../DatabaseServices/DirectionalLight"; +import { app } from "../ApplicationServices/Application"; + +/** + * + * + * @export + * @class TestDrawDirectionalLight + * @implements {Command} + */ +export class TestDrawDirectionalLight implements Command +{ + async exec() + { + let pl = new DirectionalLight(); + + app.m_Database.ModelSpace.Append(pl); + + app.m_Editor.UpdateScreen(); + } +} diff --git a/src/Add-on/TestDrawSpotLight.ts b/src/Add-on/TestDrawSpotLight.ts new file mode 100644 index 000000000..78d4b2878 --- /dev/null +++ b/src/Add-on/TestDrawSpotLight.ts @@ -0,0 +1,26 @@ +import { Command } from "../Editor/CommandMachine"; +import { SpotLight } from "../DatabaseServices/SpotLight"; +import { app } from "../ApplicationServices/Application"; +import { Matrix4 } from "three"; +/** + * + * + * @export + * @class TestDrawSpotLight + * @implements {Command} + */ +export class TestDrawSpotLight implements Command +{ + async exec() + { + + let pl = new SpotLight(); + + let ro = new Matrix4(); + pl.ApplyMatrix(ro.makeRotationZ(0.4)); + + app.m_Database.ModelSpace.Append(pl); + + app.m_Editor.UpdateScreen(); + } +} diff --git a/src/Add-on/ViewChange.ts b/src/Add-on/ViewChange.ts index fec8bbf6b..c9f8b12ca 100644 --- a/src/Add-on/ViewChange.ts +++ b/src/Add-on/ViewChange.ts @@ -29,3 +29,17 @@ export class ViewToFront implements Command } } + +export class ViewToRight implements Command +{ + async exec() + { + app.m_Viewer.m_CameraCtrl.LookAt(new Vector3(1, 0, 0)); + let mat = app.m_Editor.UCSMatrix; + + mat.lookAt(new Vector3(), new Vector3(1, 0, 0), new Vector3(0, 0, 1)); + app.m_Editor.UCSMatrix = mat; + + app.m_Editor.UpdateScreen(); + } +} diff --git a/src/DatabaseServices/DirectionalLight.ts b/src/DatabaseServices/DirectionalLight.ts new file mode 100644 index 000000000..3dec2b4ac --- /dev/null +++ b/src/DatabaseServices/DirectionalLight.ts @@ -0,0 +1,80 @@ +import { Light } from "./Light"; +import { Factory } from "./CADFactory"; +import { RenderType } from "../GraphicsSystem/Enum"; +import { Object3D, Mesh } from "three"; +import * as THREE from "three"; +import { CADFile } from "./CADFile"; +import ThreeBSP from "../Geometry/ThreeCSG"; + +/** + * 平行光源 + * + * @export + * @class DirectionalLight + * @extends {Light} + */ +@Factory +export class DirectionalLight extends Light +{ + constructor() + { + super(); + this.m_Intensity = 1.5; + } + + protected m_Light: THREE.DirectionalLight; + Draw(renderType: RenderType = RenderType.Wireframe): Object3D + { + if (this.m_Light) + return this.m_Light; + + this.m_Light = new THREE.DirectionalLight(this.m_LightColor, this.m_Intensity); + this.m_Light.castShadow = true; + + //绘制灯光助手 + let lightGeo = new THREE.BoxGeometry(0.05, 0.05, 0.05); + let geoMat = new THREE.MeshStandardMaterial({ + emissive: 0xffffee, + emissiveIntensity: 1, + color: 0x000000 + }); + + this.m_Light.add(new Mesh(lightGeo, geoMat)); + + this.m_Light.userData = this; + + this.UpdatePostion(); + + this.m_DrawEntity.set(renderType, this.m_Light); + + return this.m_Light; + } + + Update() + { + super.Update(); + if (!this.m_Light) return; + + this.UpdatePostion(); + } + + //#region -------------------------File------------------------- + //对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化 + + //对象从文件中读取数据,初始化自身 + ReadFile(file: CADFile) + { + super.ReadFile(file); + let ver = file.Read(); + + + this.Update(); + } + //对象将自身数据写入到文件. + WriteFile(file: CADFile) + { + super.WriteFile(file); + file.Write(1); + + } +} diff --git a/src/DatabaseServices/SpotLight.ts b/src/DatabaseServices/SpotLight.ts new file mode 100644 index 000000000..3c693d29e --- /dev/null +++ b/src/DatabaseServices/SpotLight.ts @@ -0,0 +1,101 @@ +import { Light } from "./Light"; +import { Factory } from "./CADFactory"; +import { RenderType } from "../GraphicsSystem/Enum"; +import { Object3D, Mesh } from "three"; +import * as THREE from "three"; +import { CADFile } from "./CADFile"; +/** + * + * + * @export + * @class SpotLight + * @extends {Light} + */ +@Factory +export class SpotLight extends Light +{ + /** + * If non-zero, light will attenuate linearly from maximum intensity at light position down to zero at distance. + * Default — 0.0. + */ + private m_Distance: number = 0.0; + + // 光线沿着光线的距离变暗的量 + // 在物理上正确的模式下,衰减 = 2会导致物理上真实的光线衰减。 + // 缺省值是1。 + private m_Decay: number = 2; + // 光线散射角度,最大为Math.PI/2 + private m_angle: number = Math.PI / 3;//默认 + + // 聚光锥的半影衰减百分比。在0和1之间的值。 默认值 — 0.0。 + private m_penumbra: number = 0; + constructor() + { + super(); + this.m_Intensity = 1.5; + this.m_Distance = 100; + + } + + protected m_Light: THREE.SpotLight; + Draw(renderType: RenderType = RenderType.Wireframe): Object3D + { + if (this.m_Light) + return this.m_Light; + + this.m_Light = new THREE.SpotLight(this.m_LightColor, this.m_Intensity, this.m_Distance, this.m_angle, this.m_penumbra, this.m_Decay); + this.m_Light.castShadow = true; + + //绘制灯光助手 + let lightGeo = new THREE.ConeGeometry(0.05, 0.2, 0.3); + let geoMat = new THREE.MeshStandardMaterial({ + emissive: 0xffffee, + emissiveIntensity: 1, + color: 0x000000 + }); + + this.m_Light.add(new Mesh(lightGeo, geoMat)); + + this.m_Light.userData = this; + + this.UpdatePostion(); + + this.m_DrawEntity.set(renderType, this.m_Light); + + return this.m_Light; + } + + Update() + { + super.Update(); + if (!this.m_Light) return; + this.m_Light.distance = this.m_Distance; + this.m_Light.decay = this.m_Decay; + this.m_Light.angle = this.m_angle; + this.m_Light.penumbra = this.m_penumbra; + this.UpdatePostion(); + } + + //#region -------------------------File------------------------- + //对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化 + + //对象从文件中读取数据,初始化自身 + ReadFile(file: CADFile) + { + super.ReadFile(file); + let ver = file.Read(); + this.m_Distance = file.Read(); + this.m_Decay = file.Read(); + + this.Update(); + } + //对象将自身数据写入到文件. + WriteFile(file: CADFile) + { + super.WriteFile(file); + file.Write(1); + file.Write(this.m_Distance); + file.Write(this.m_Decay); + } + +} diff --git a/src/Editor/CommandRegister.ts b/src/Editor/CommandRegister.ts index 9e3dfc8c7..7f51bf998 100644 --- a/src/Editor/CommandRegister.ts +++ b/src/Editor/CommandRegister.ts @@ -33,10 +33,12 @@ import { Test } from '../Add-on/test'; import { Command_DrawBoard2 } from '../Add-on/TestDrawBoard'; import { Command_Trim } from '../Add-on/Trim'; import { Redo, Undo } from '../Add-on/Undo'; -import { ViewToFront, ViewToTop } from '../Add-on/ViewChange'; +import { ViewToFront, ViewToTop, ViewToRight } from '../Add-on/ViewChange'; import { commandMachine } from './CommandMachine'; import { Command_Offset } from '../Add-on/Offset'; import { TestDrawPointLight } from '../Add-on/TestDrawPointLight'; +import { TestDrawDirectionalLight } from '../Add-on/TestDrawDirectionalLight'; +import { TestDrawSpotLight } from '../Add-on/TestDrawSpotLight'; // import { DrawFloor } from '../Add-on/DrawFloor'; // import { RevTarget, SaveTarget } from '../Add-on/RenderTarget'; @@ -75,6 +77,8 @@ export function registerCommand() commandMachine.RegisterCommand("qs", new ViewToFront()) + commandMachine.RegisterCommand("ys", new ViewToRight()) + commandMachine.RegisterCommand("tt", new Test()) commandMachine.RegisterCommand("grip", new DrawGripStretch()) @@ -121,6 +125,10 @@ export function registerCommand() commandMachine.RegisterCommand("ptl", new TestDrawPointLight()); + commandMachine.RegisterCommand("dl", new TestDrawDirectionalLight()); + + commandMachine.RegisterCommand("sl", new TestDrawSpotLight()); + // commandMachine.RegisterCommand("st", new SaveTarget()) // commandMachine.RegisterCommand("rt", new RevTarget()) }