private void SetColorAndIntensityOfAllLights(Sansar.Color c, float intensity) { foreach (var light in lights) { light.SetColorAndIntensity(c, intensity); } }
private void PlayLightProgram(int LightProgram) { fltEffectTime = float.Parse(EffectTime[LightProgram]); fltLightIntensity = float.Parse(LightIntensity[LightProgram]); fltLightAngle = float.Parse(LightAngle[LightProgram]); fltLightFallOff = float.Parse(LightFallOff[LightProgram]); spotActive = false; blinkActive = false; fadeActive = false; pulseActive = false; light.SetAngle(fltLightAngle); light.SetAngularFalloff(fltLightFallOff); if (RandomColor[LightProgram]) { targetColor.R = (float)random.NextDouble(); targetColor.G = (float)random.NextDouble(); targetColor.B = (float)random.NextDouble(); targetColor.A = LightColor[LightProgram].A; } else { targetColor = LightColor[LightProgram]; } if (Effect[LightProgram] == "fade") { //Log.Write("Fade LightProgram:" + LightProgram); //Log.Write("Color: " + LightColor[LightProgram]); fltBaseLightIntensity = float.Parse(BaseLightIntensity[LightProgram]); //Log.Write("deltaTime: " + EffectParm[LightProgram]); deltaTime = float.Parse(EffectParm[LightProgram]); //Log.Write("deltaTime: " + deltaTime); FadeIn(fltEffectTime, deltaTime); } if (Effect[LightProgram] == "pulse") { Log.Write("Pulse1"); fltBaseLightIntensity = float.Parse(BaseLightIntensity[LightProgram]); Log.Write("Pulse2"); Pulse(fltEffectTime, deltaTime, RandomColor[LightProgram]); } if (Effect[LightProgram] == "blink") { Blink(fltEffectTime, RandomColor[LightProgram]); } if (Effect[LightProgram] == "spot") { Spot(fltEffectTime, RandomColor[LightProgram]); } }
private void StartInterpolation() { if ((interpolationCoroutine == null) && HasFadeTime()) { targetColor = previousColor = lights[0].GetNormalizedColor(); targetIntensity = previousIntensity = lights[0].GetRelativeIntensity(); interpolationDuration = 0.0f; interpolationTime = 0.0f; interpolationActive = false; interpolationCoroutine = StartCoroutine(InterpolateLightColor); } }
private void Subscribe(ScriptEventData sed) { if (subscriptions == null) { subscriptions = SubscribeToAll(TurnOnEvent, (data) => { if (TurnOnFadeTime > 0.0f) { previousColor = lights[0].GetNormalizedColor(); previousIntensity = lights[0].GetRelativeIntensity(); targetColor = initialColor; targetIntensity = initialIntensity; interpolationDuration = TurnOnFadeTime; interpolationTime = TurnOnFadeTime; interpolationActive = true; } else { interpolationActive = false; SetColorAndIntensityOfAllLights(initialColor, initialIntensity); } }); subscriptions += SubscribeToAll(TurnOffEvent, (data) => { if (TurnOffFadeTime > 0.0f) { previousColor = lights[0].GetNormalizedColor(); previousIntensity = lights[0].GetRelativeIntensity(); targetColor = Sansar.Color.Black; targetIntensity = 0.0f; interpolationDuration = TurnOffFadeTime; interpolationTime = TurnOffFadeTime; interpolationActive = true; } else { interpolationActive = false; SetColorAndIntensityOfAllLights(Sansar.Color.Black, 0.0f); } }); } if (HasFadeTime()) { StartInterpolation(); } }
private void SetRandomColorAndIntensityOfAllLights() { foreach (var light in lights) { // Pick a random color but don't let it be too dark or else the relative intensity doesn't work well Sansar.Vector randomVector = new Sansar.Vector((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble()); if (randomVector.LengthSquared() < 0.5f) { randomVector = randomVector.Normalized(); } Sansar.Color randomColor = new Sansar.Color(randomVector.X, randomVector.Y, randomVector.Z); // Pick a random intensity from min to max float randomIntensity = randomMinIntensity + (randomMaxIntensity - randomMinIntensity) * (float)rnd.NextDouble(); light.SetColorAndIntensity(randomColor, randomIntensity); } }
protected override void SimpleInit() { rnd = new Random(); lights = new List <LightComponent>(); uint lightCount = ObjectPrivate.GetComponentCount(ComponentType.LightComponent); for (uint i = 0; i < lightCount; i++) { LightComponent lc = (LightComponent)ObjectPrivate.GetComponent(ComponentType.LightComponent, i); if (lc.IsScriptable) { lights.Add(lc); break; } } if (lights.Count == 0) { Log.Write(LogLevel.Error, "SimpleLightOnOff::Init", "Object must have at least one scriptable light added at edit time for SimpleLightOnOff script to work."); return; } initialColor = lights[0].GetNormalizedColor(); initialIntensity = lights[0].GetRelativeIntensity(); randomMinIntensity = (MinRandomIntensity < MaxRandomIntensity ? MinRandomIntensity : MaxRandomIntensity); randomMaxIntensity = (MinRandomIntensity < MaxRandomIntensity ? MaxRandomIntensity : MinRandomIntensity); // Turn off the light if specified if (TurnOffAtStart) { SetColorAndIntensityOfAllLights(Sansar.Color.Black, 0.0f); } if (StartEnabled) { Subscribe(null); } SubscribeToAll(EnableEvent, Subscribe); SubscribeToAll(DisableEvent, Unsubscribe); }
private void InterpolateLightColor() { const float deltaTime = 0.1f; TimeSpan ts = TimeSpan.FromSeconds(deltaTime); while (true) { Wait(ts); if (interpolationActive) { interpolationTime = Math.Max(interpolationTime - deltaTime, 0.0f); float t = interpolationTime / interpolationDuration; Sansar.Color color = previousColor * t + targetColor * (1.0f - t); float intensity = previousIntensity * t + targetIntensity * (1.0f - t); SetColorAndIntensityOfAllLights(color, intensity); interpolationActive = (interpolationTime > 0.0f); } } }
private void Subscribe(ScriptEventData sed) { if (subscriptions == null) { subscriptions = SubscribeToAll(TurnOnEvent, (data) => { if (TurnOnFadeTime > 0.0f) { previousColor = lights[0].GetNormalizedColor(); previousIntensity = lights[0].GetRelativeIntensity(); targetColor = initialColor; targetIntensity = initialIntensity; interpolationDuration = TurnOnFadeTime; interpolationTime = TurnOnFadeTime; interpolationActive = true; } else { interpolationActive = false; SetColorAndIntensityOfAllLights(initialColor, initialIntensity); } }); subscriptions += SubscribeToAll(TurnOffEvent, (data) => { if (TurnOffFadeTime > 0.0f) { previousColor = lights[0].GetNormalizedColor(); previousIntensity = lights[0].GetRelativeIntensity(); targetColor = Sansar.Color.Black; targetIntensity = 0.0f; interpolationDuration = TurnOffFadeTime; interpolationTime = TurnOffFadeTime; interpolationActive = true; } else { interpolationActive = false; SetColorAndIntensityOfAllLights(Sansar.Color.Black, 0.0f); } }); subscriptions += SubscribeToAll(TurnRandomEvent, (data) => { if (TurnRandomFadeTime > 0.0f) { // Pick a random color but don't let it be too dark or else the relative intensity doesn't work well Sansar.Vector randomVector = new Sansar.Vector((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble()); while (randomVector.LengthSquared() < 0.1f) { randomVector = new Sansar.Vector((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble()); } if (randomVector.LengthSquared() < 0.5f) { randomVector = randomVector.Normalized(); } Sansar.Color randomColor = new Sansar.Color(randomVector.X, randomVector.Y, randomVector.Z); // Pick a random intensity from min to max float randomIntensity = randomMinIntensity + (randomMaxIntensity - randomMinIntensity) * (float)rnd.NextDouble(); previousColor = lights[0].GetNormalizedColor(); previousIntensity = lights[0].GetRelativeIntensity(); targetColor = randomColor; targetIntensity = randomIntensity; interpolationDuration = TurnRandomFadeTime; interpolationTime = TurnRandomFadeTime; interpolationActive = true; } else { interpolationActive = false; SetRandomColorAndIntensityOfAllLights(); } }); } if (HasFadeTime()) { StartInterpolation(); } }
protected override void SimpleInit() { rnd = new Random(); if (LightComponents.Count > 0) { int removed = LightComponents.RemoveAll(light => light == null || !light.IsScriptable); if (removed > 0) { Log.Write(LogLevel.Error, "LightOnOff::Init", $"{removed} lights were not set scriptable and will not be controllable by this script."); } if (LightComponents.Count == 0) { Log.Write(LogLevel.Error, "LightOnOff::Init", "None of the selected Lights were scriptable."); return; } } else { uint lightCount = ObjectPrivate.GetComponentCount(ComponentType.LightComponent); bool foundUnscriptable = false; for (uint i = 0; i < lightCount; i++) { LightComponent lc = (LightComponent)ObjectPrivate.GetComponent(ComponentType.LightComponent, i); if (lc.IsScriptable) { LightComponents.Add(lc); } else if (!foundUnscriptable) { Log.Write(LogLevel.Error, "LightOnOff::Init", "Some lights on this object are not scriptable (first found: " + lc.Name + ")"); foundUnscriptable = true; } } if (LightComponents.Count == 0) { Log.Write(LogLevel.Error, "LightOnOff::Init", "No scriptable lights found on this object: " + ObjectPrivate.Name); return; } } initialColor = LightComponents[0].GetNormalizedColor(); initialIntensity = LightComponents[0].GetRelativeIntensity(); randomMinIntensity = (MinRandomIntensity < MaxRandomIntensity ? MinRandomIntensity : MaxRandomIntensity); randomMaxIntensity = (MinRandomIntensity < MaxRandomIntensity ? MaxRandomIntensity : MinRandomIntensity); // Turn off the light if specified if (TurnOffAtStart) { SetColorAndIntensityOfAllLights(Sansar.Color.Black, 0.0f); } if (StartEnabled) { Subscribe(null); } SubscribeToAll(EnableEvent, Subscribe); SubscribeToAll(DisableEvent, Unsubscribe); }