123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.Collections.Generic;
- using Autodesk.Max;
- using BabylonExport.Entities;
- using MaxSharp;
- namespace Max2Babylon
- {
- partial class BabylonExporter
- {
- private void ExportLight(Node lightNode, BabylonScene babylonScene)
- {
- if (lightNode._Node.GetBoolProperty("babylonjs_noexport"))
- {
- return;
- }
- var maxLight = (lightNode.Object as Light);
- var babylonLight = new BabylonLight();
- RaiseMessage(maxLight.Name, true);
- babylonLight.name = lightNode.Name;
- babylonLight.id = lightNode.GetGuid().ToString();
- // Type
- var lightState = Loader.Global.LightState.Create();
- maxLight._Light.EvalLightState(0, Interval.Forever._IInterval, lightState);
- var directionScale = -1;
- switch (lightState.Type)
- {
- case LightType.OmniLgt:
- babylonLight.type = 0;
- break;
- case LightType.SpotLgt:
- babylonLight.type = 2;
- babylonLight.angle = (float)(maxLight.GetFallOffSize(0, Interval.Forever) * Math.PI / 180.0f);
- babylonLight.exponent = 1;
- break;
- case LightType.DirectLgt:
- babylonLight.type = 1;
- break;
- case LightType.AmbientLgt:
- babylonLight.type = 3;
- babylonLight.groundColor = new float[] { 0, 0, 0 };
- directionScale = 1;
- break;
- }
- // Shadows
- if (maxLight.ShadowMethod == 1)
- {
- if (lightState.Type == LightType.DirectLgt)
- {
- ExportShadowGenerator(lightNode, babylonScene);
- }
- else
- {
- RaiseWarning("Shadows maps are only supported for directional lights", true);
- }
- }
- // Position
- var wm = lightNode.GetWorldMatrix(0, false);
- var position = wm.Trans;
- babylonLight.position = position.ToArraySwitched();
- // Direction
- var target = lightNode._Node.Target;
- if (target != null)
- {
- var targetWm = target.GetObjTMBeforeWSM(0, Interval.Forever._IInterval);
- var targetPosition = targetWm.Trans;
- var direction = targetPosition.Subtract(position);
- babylonLight.direction = direction.ToArraySwitched();
- }
- else
- {
- var dir = wm.GetRow(2).MultiplyBy(directionScale);
- babylonLight.direction = dir.ToArraySwitched();
- }
- // Exclusion
- var maxScene = Kernel.Scene;
- var inclusion = maxLight._Light.ExclList.TestFlag(1); //NT_INCLUDE
- var checkExclusionList = maxLight._Light.ExclList.TestFlag(2); //NT_AFFECT_ILLUM
- if (checkExclusionList)
- {
- var list = new List<string>();
- foreach (var meshNode in maxScene.NodesListBySuperClass(SuperClassID.GeometricObject))
- {
- if (meshNode._Node.CastShadows == 1)
- {
- var inList = maxLight._Light.ExclList.FindNode(meshNode._Node) != -1;
- if ((!inList && inclusion) || (inList && !inclusion))
- {
- list.Add(meshNode.GetGuid().ToString());
- }
- }
- }
- babylonLight.excludedMeshesIds = list.ToArray();
- }
- // Other fields
- babylonLight.intensity = maxLight.GetIntensity(0, Interval.Forever);
- babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Interval.Forever).ToArray() : new float[] { 0, 0, 0 };
- babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Interval.Forever).ToArray() : new float[] { 0, 0, 0 };
- if (maxLight.UseAttenuation)
- {
- babylonLight.range = maxLight.GetAttenuation(0, 1, Interval.Forever);
- }
- // Animations
- var animations = new List<BabylonAnimation>();
- if (!ExportVector3Controller(lightNode._Node.TMController.PositionController, "position", animations))
- {
- ExportVector3Animation("position", animations, key =>
- {
- var worldMatrix = lightNode.GetWorldMatrix(key, lightNode.HasParent());
- return worldMatrix.Trans.ToArraySwitched();
- });
- }
- ExportVector3Animation("direction", animations, key =>
- {
- var targetNode = lightNode._Node.Target;
- if (targetNode != null)
- {
- var targetWm = target.GetObjTMBeforeWSM(0, Interval.Forever._IInterval);
- var targetPosition = targetWm.Trans;
- var direction = targetPosition.Subtract(position);
- return direction.ToArraySwitched();
- }
-
- var dir = wm.GetRow(2).MultiplyBy(directionScale);
- return dir.ToArraySwitched();
- });
- ExportFloatAnimation("intensity", animations, key => new[] { maxLight.GetIntensity(key, Interval.Forever) });
- babylonLight.animations = animations.ToArray();
- if (lightNode._Node.GetBoolProperty("babylonjs_autoanimate"))
- {
- babylonLight.autoAnimate = true;
- babylonLight.autoAnimateFrom = (int)lightNode._Node.GetFloatProperty("babylonjs_autoanimate_from");
- babylonLight.autoAnimateTo = (int)lightNode._Node.GetFloatProperty("babylonjs_autoanimate_to");
- babylonLight.autoAnimateLoop = lightNode._Node.GetBoolProperty("babylonjs_autoanimateloop");
- }
- babylonScene.LightsList.Add(babylonLight);
- }
- }
- }
|