public static SceneLightsource CreateInfiniteLight(string name, string imagePath) { if (string.IsNullOrWhiteSpace(imagePath)) { Tracer.Print("Create Lightsource","Empty image path for Environment map " + name); } var al = new SceneLightsource { Name = name, LightType = LightSourceTypes.EnvironmentMap }; al["EnvMap.Image"] = imagePath; return al; }
public static SceneLightsource CreatePointLight(string name, Vector3 position) { var pt = new SceneLightsource { Name = name, LightType = LightSourceTypes.Point }; pt["PointLight.Position"] = position; return pt; }
public static SceneLightsource CreateAreaLight(string name, ISceneGeometry sceneGeo) { var al = new SceneLightsource { Name = name, LightType = LightSourceTypes.Area }; al["AreaLight.Mesh"] = sceneGeo; return al; }
public static SceneLightsource CreateDirectionalLight(string name, Vector3 direction) { var pt = new SceneLightsource { Name = name, LightType = LightSourceTypes.Directional }; pt["DirLight.Direction"] = direction; return pt; }
private void BuildLights(SceneLightsource[] sceneLights) { var lightInfos = new List<LightInfo>(); foreach (var sceneLightsource in sceneLights) { var lightInfo = sceneLightsource.CreateInfo(); if (lightInfo.type == 5) { EnvMapPath = sceneLightsource.Get("EnvMap.Image", string.Empty); } if (lightInfo.type != 2) { lightInfos.Add(lightInfo); } else { var li = ((Dictionary<string, List<int>>)BuildCache["LightTrianglesIndexes"])[((SceneMeshEntity)sceneLightsource["AreaLight.Mesh"]).Name]; lightInfos.Add(new LightInfo() { type = 2, emission = sceneLightsource.Color, startTriangle = li.Min(), endTriangle = li.Max(), }); } } var triangleInfos = ((List<TriangleLight>)BuildCache["LightTriangles"]).Select(tr => new LightTriangleInfo() { v0 = tr.v1, v1 = tr.v2, v2 = tr.v3, n = tr.normal, area = CalcTriangleArea(tr) }).ToList(); //TODO Index from 1!!! First triangle is fake triangleInfos.Insert(0, new LightTriangleInfo() { area = 1, n = new Vector3(1, 1, 1), v1 = new Vector3(100, 100, 100), v2 = new Vector3(101, 101, 101), v0 = new Vector3(102, 102, 102), }); var triBufferDesc = new BufferDesc { Width = (uint)triangleInfos.Count, Format = Format.User, Type = BufferType.Input, ElemSize = (uint)Marshal.SizeOf(typeof(LightTriangleInfo)) }; var triangleBuffer = new Buffer(Session.OptixContext, triBufferDesc); var stream = triangleBuffer.Map(); stream.WriteRange(triangleInfos.ToArray()); triangleBuffer.Unmap(); if (!string.IsNullOrWhiteSpace(EnvMapPath)) { Tracer.Print("BuildLights", "Loading environment map " + EnvMapPath); var sceneEnvMapPath = EnvMapPath; var envmap = TextureCache.Instance.Load(Session.OptixContext, sceneEnvMapPath); //KC_outside_hi.hdr var id = envmap.GetId(); Session.OptixContext["envmapId"].Set((uint)id); Session.OptixContext["use_envmap"].Set(1); var elt = lightInfos.Find(lt => lt.type == 5); var index = lightInfos.IndexOf(elt); lightInfos[index] = new LightInfo { type = 5, samplerId = id, emission = elt.emission }; } else { Session.OptixContext["use_envmap"].Set(0); } var ltBufferDesc = new BufferDesc { Width = (uint)lightInfos.Count, Format = Format.User, Type = BufferType.Input, ElemSize = (uint)Marshal.SizeOf(typeof(LightInfo)) }; var lightBuffer = new OptixDotNet.Buffer(Session.OptixContext, ltBufferDesc); var ltStream = lightBuffer.Map(); //LightsCount.LValue += lts.Count; foreach (var lightinfo in lightInfos) { ltStream.Write(lightinfo); } lightBuffer.Unmap(); Session.Frame.LightTrianglesBuffer = triangleBuffer; Session.Frame.LightBuffer = lightBuffer; }