Warp3DImageModule.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Reflection;
  33. using CSJ2K;
  34. using Nini.Config;
  35. using log4net;
  36. using Rednettle.Warp3D;
  37. using Mono.Addins;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Region.Physics.Manager;
  42. using OpenSim.Services.Interfaces;
  43. using OpenMetaverse;
  44. using OpenMetaverse.Assets;
  45. using OpenMetaverse.Imaging;
  46. using OpenMetaverse.Rendering;
  47. using OpenMetaverse.StructuredData;
  48. using WarpRenderer = global::Warp3D.Warp3D;
  49. namespace OpenSim.Region.CoreModules.World.Warp3DMap
  50. {
  51. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "Warp3DImageModule")]
  52. public class Warp3DImageModule : IMapImageGenerator, INonSharedRegionModule
  53. {
  54. private static readonly UUID TEXTURE_METADATA_MAGIC = new UUID("802dc0e0-f080-4931-8b57-d1be8611c4f3");
  55. private static readonly Color4 WATER_COLOR = new Color4(29, 72, 96, 216);
  56. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. #pragma warning disable 414
  58. private static string LogHeader = "[WARP 3D IMAGE MODULE]";
  59. #pragma warning restore 414
  60. private Scene m_scene;
  61. private IRendering m_primMesher;
  62. private Dictionary<UUID, Color4> m_colors = new Dictionary<UUID, Color4>();
  63. private IConfigSource m_config;
  64. private bool m_drawPrimVolume = true; // true if should render the prims on the tile
  65. private bool m_textureTerrain = true; // true if to create terrain splatting texture
  66. private bool m_texturePrims = true; // true if should texture the rendered prims
  67. private float m_texturePrimSize = 48f; // size of prim before we consider texturing it
  68. private bool m_renderMeshes = false; // true if to render meshes rather than just bounding boxes
  69. private bool m_useAntiAliasing = false; // true if to anti-alias the rendered image
  70. private bool m_Enabled = false;
  71. #region Region Module interface
  72. public void Initialise(IConfigSource source)
  73. {
  74. m_config = source;
  75. string[] configSections = new string[] { "Map", "Startup" };
  76. if (Util.GetConfigVarFromSections<string>(
  77. m_config, "MapImageModule", configSections, "MapImageModule") != "Warp3DImageModule")
  78. return;
  79. m_Enabled = true;
  80. m_drawPrimVolume
  81. = Util.GetConfigVarFromSections<bool>(m_config, "DrawPrimOnMapTile", configSections, m_drawPrimVolume);
  82. m_textureTerrain
  83. = Util.GetConfigVarFromSections<bool>(m_config, "TextureOnMapTile", configSections, m_textureTerrain);
  84. m_texturePrims
  85. = Util.GetConfigVarFromSections<bool>(m_config, "TexturePrims", configSections, m_texturePrims);
  86. m_texturePrimSize
  87. = Util.GetConfigVarFromSections<float>(m_config, "TexturePrimSize", configSections, m_texturePrimSize);
  88. m_renderMeshes
  89. = Util.GetConfigVarFromSections<bool>(m_config, "RenderMeshes", configSections, m_renderMeshes);
  90. m_useAntiAliasing
  91. = Util.GetConfigVarFromSections<bool>(m_config, "UseAntiAliasing", configSections, m_useAntiAliasing);
  92. }
  93. public void AddRegion(Scene scene)
  94. {
  95. if (!m_Enabled)
  96. return;
  97. m_scene = scene;
  98. List<string> renderers = RenderingLoader.ListRenderers(Util.ExecutingDirectory());
  99. if (renderers.Count > 0)
  100. {
  101. m_primMesher = RenderingLoader.LoadRenderer(renderers[0]);
  102. m_log.DebugFormat("[WARP 3D IMAGE MODULE]: Loaded prim mesher {0}", m_primMesher);
  103. }
  104. else
  105. {
  106. m_log.Debug("[WARP 3D IMAGE MODULE]: No prim mesher loaded, prim rendering will be disabled");
  107. }
  108. m_scene.RegisterModuleInterface<IMapImageGenerator>(this);
  109. }
  110. public void RegionLoaded(Scene scene)
  111. {
  112. }
  113. public void RemoveRegion(Scene scene)
  114. {
  115. }
  116. public void Close()
  117. {
  118. }
  119. public string Name
  120. {
  121. get { return "Warp3DImageModule"; }
  122. }
  123. public Type ReplaceableInterface
  124. {
  125. get { return null; }
  126. }
  127. #endregion
  128. #region IMapImageGenerator Members
  129. public Bitmap CreateMapTile()
  130. {
  131. // Vector3 camPos = new Vector3(127.5f, 127.5f, 221.7025033688163f);
  132. // Camera above the middle of the region
  133. Vector3 camPos = new Vector3(
  134. m_scene.RegionInfo.RegionSizeX/2 - 0.5f,
  135. m_scene.RegionInfo.RegionSizeY/2 - 0.5f,
  136. 221.7025033688163f);
  137. // Viewport viewing down onto the region
  138. Viewport viewport = new Viewport(camPos, -Vector3.UnitZ, 1024f, 0.1f,
  139. (int)m_scene.RegionInfo.RegionSizeX, (int)m_scene.RegionInfo.RegionSizeY,
  140. (float)m_scene.RegionInfo.RegionSizeX, (float)m_scene.RegionInfo.RegionSizeY );
  141. // Fill the viewport and return the image
  142. return CreateMapTile(viewport, false);
  143. }
  144. public Bitmap CreateViewImage(Vector3 camPos, Vector3 camDir, float fov, int width, int height, bool useTextures)
  145. {
  146. Viewport viewport = new Viewport(camPos, camDir, fov, Constants.RegionSize, 0.1f, width, height);
  147. return CreateMapTile(viewport, useTextures);
  148. }
  149. public Bitmap CreateMapTile(Viewport viewport, bool useTextures)
  150. {
  151. m_colors.Clear();
  152. int width = viewport.Width;
  153. int height = viewport.Height;
  154. if (m_useAntiAliasing)
  155. {
  156. width *= 2;
  157. height *= 2;
  158. }
  159. WarpRenderer renderer = new WarpRenderer();
  160. renderer.CreateScene(width, height);
  161. renderer.Scene.autoCalcNormals = false;
  162. #region Camera
  163. warp_Vector pos = ConvertVector(viewport.Position);
  164. pos.z -= 0.001f; // Works around an issue with the Warp3D camera
  165. warp_Vector lookat = warp_Vector.add(ConvertVector(viewport.Position), ConvertVector(viewport.LookDirection));
  166. renderer.Scene.defaultCamera.setPos(pos);
  167. renderer.Scene.defaultCamera.lookAt(lookat);
  168. if (viewport.Orthographic)
  169. {
  170. renderer.Scene.defaultCamera.isOrthographic = true;
  171. renderer.Scene.defaultCamera.orthoViewWidth = viewport.OrthoWindowWidth;
  172. renderer.Scene.defaultCamera.orthoViewHeight = viewport.OrthoWindowHeight;
  173. }
  174. else
  175. {
  176. float fov = viewport.FieldOfView;
  177. fov *= 1.75f; // FIXME: ???
  178. renderer.Scene.defaultCamera.setFov(fov);
  179. }
  180. #endregion Camera
  181. renderer.Scene.addLight("Light1", new warp_Light(new warp_Vector(1.0f, 0.5f, 1f), 0xffffff, 0, 320, 40));
  182. renderer.Scene.addLight("Light2", new warp_Light(new warp_Vector(-1f, -1f, 1f), 0xffffff, 0, 100, 40));
  183. CreateWater(renderer);
  184. CreateTerrain(renderer, m_textureTerrain);
  185. if (m_drawPrimVolume)
  186. CreateAllPrims(renderer, useTextures);
  187. renderer.Render();
  188. Bitmap bitmap = renderer.Scene.getImage();
  189. if (m_useAntiAliasing)
  190. {
  191. using (Bitmap origBitmap = bitmap)
  192. bitmap = ImageUtils.ResizeImage(origBitmap, viewport.Width, viewport.Height);
  193. }
  194. // XXX: It shouldn't really be necesary to force a GC here as one should occur anyway pretty shortly
  195. // afterwards. It's generally regarded as a bad idea to manually GC. If Warp3D is using lots of memory
  196. // then this may be some issue with the Warp3D code itself, though it's also quite possible that generating
  197. // this map tile simply takes a lot of memory.
  198. foreach (var o in renderer.Scene.objectData.Values)
  199. {
  200. warp_Object obj = (warp_Object)o;
  201. obj.vertexData = null;
  202. obj.triangleData = null;
  203. }
  204. renderer.Scene.removeAllObjects();
  205. renderer = null;
  206. viewport = null;
  207. m_colors.Clear();
  208. GC.Collect();
  209. m_log.Debug("[WARP 3D IMAGE MODULE]: GC.Collect()");
  210. return bitmap;
  211. }
  212. public byte[] WriteJpeg2000Image()
  213. {
  214. try
  215. {
  216. using (Bitmap mapbmp = CreateMapTile())
  217. return OpenJPEG.EncodeFromImage(mapbmp, true);
  218. }
  219. catch (Exception e)
  220. {
  221. // JPEG2000 encoder failed
  222. m_log.Error("[WARP 3D IMAGE MODULE]: Failed generating terrain map: ", e);
  223. }
  224. return null;
  225. }
  226. #endregion
  227. #region Rendering Methods
  228. // Add a water plane to the renderer.
  229. private void CreateWater(WarpRenderer renderer)
  230. {
  231. float waterHeight = (float)m_scene.RegionInfo.RegionSettings.WaterHeight;
  232. renderer.AddPlane("Water", m_scene.RegionInfo.RegionSizeX * 0.5f);
  233. renderer.Scene.sceneobject("Water").setPos(m_scene.RegionInfo.RegionSizeX/2 - 0.5f,
  234. waterHeight,
  235. m_scene.RegionInfo.RegionSizeY/2 - 0.5f );
  236. warp_Material waterColorMaterial = new warp_Material(ConvertColor(WATER_COLOR));
  237. waterColorMaterial.setReflectivity(0); // match water color with standard map module thanks lkalif
  238. waterColorMaterial.setTransparency((byte)((1f - WATER_COLOR.A) * 255f));
  239. renderer.Scene.addMaterial("WaterColor", waterColorMaterial);
  240. renderer.SetObjectMaterial("Water", "WaterColor");
  241. }
  242. // Add a terrain to the renderer.
  243. // Note that we create a 'low resolution' 256x256 vertex terrain rather than trying for
  244. // full resolution. This saves a lot of memory especially for very large regions.
  245. private void CreateTerrain(WarpRenderer renderer, bool textureTerrain)
  246. {
  247. ITerrainChannel terrain = m_scene.Heightmap;
  248. // 'diff' is the difference in scale between the real region size and the size of terrain we're buiding
  249. float diff = (float)m_scene.RegionInfo.RegionSizeX / 256f;
  250. warp_Object obj = new warp_Object(256 * 256, 255 * 255 * 2);
  251. // Create all the vertices for the terrain
  252. for (float y = 0; y < m_scene.RegionInfo.RegionSizeY; y += diff)
  253. {
  254. for (float x = 0; x < m_scene.RegionInfo.RegionSizeX; x += diff)
  255. {
  256. warp_Vector pos = ConvertVector(x, y, (float)terrain[(int)x, (int)y]);
  257. obj.addVertex(new warp_Vertex(pos,
  258. x / (float)m_scene.RegionInfo.RegionSizeX,
  259. (((float)m_scene.RegionInfo.RegionSizeY) - y) / m_scene.RegionInfo.RegionSizeY) );
  260. }
  261. }
  262. // Now that we have all the vertices, make another pass and create
  263. // the normals for each of the surface triangles and
  264. // create the list of triangle indices.
  265. for (float y = 0; y < m_scene.RegionInfo.RegionSizeY; y += diff)
  266. {
  267. for (float x = 0; x < m_scene.RegionInfo.RegionSizeX; x += diff)
  268. {
  269. float newX = x / diff;
  270. float newY = y / diff;
  271. if (newX < 255 && newY < 255)
  272. {
  273. int v = (int)newY * 256 + (int)newX;
  274. // Normal for a triangle made up of three adjacent vertices
  275. Vector3 v1 = new Vector3(newX, newY, (float)terrain[(int)x, (int)y]);
  276. Vector3 v2 = new Vector3(newX + 1, newY, (float)terrain[(int)(x + 1), (int)y]);
  277. Vector3 v3 = new Vector3(newX, newY + 1, (float)terrain[(int)x, ((int)(y + 1))]);
  278. warp_Vector norm = ConvertVector(SurfaceNormal(v1, v2, v3));
  279. norm = norm.reverse();
  280. obj.vertex(v).n = norm;
  281. // Make two triangles for each of the squares in the grid of vertices
  282. obj.addTriangle(
  283. v,
  284. v + 1,
  285. v + 256);
  286. obj.addTriangle(
  287. v + 256 + 1,
  288. v + 256,
  289. v + 1);
  290. }
  291. }
  292. }
  293. renderer.Scene.addObject("Terrain", obj);
  294. UUID[] textureIDs = new UUID[4];
  295. float[] startHeights = new float[4];
  296. float[] heightRanges = new float[4];
  297. OpenSim.Framework.RegionSettings regionInfo = m_scene.RegionInfo.RegionSettings;
  298. textureIDs[0] = regionInfo.TerrainTexture1;
  299. textureIDs[1] = regionInfo.TerrainTexture2;
  300. textureIDs[2] = regionInfo.TerrainTexture3;
  301. textureIDs[3] = regionInfo.TerrainTexture4;
  302. startHeights[0] = (float)regionInfo.Elevation1SW;
  303. startHeights[1] = (float)regionInfo.Elevation1NW;
  304. startHeights[2] = (float)regionInfo.Elevation1SE;
  305. startHeights[3] = (float)regionInfo.Elevation1NE;
  306. heightRanges[0] = (float)regionInfo.Elevation2SW;
  307. heightRanges[1] = (float)regionInfo.Elevation2NW;
  308. heightRanges[2] = (float)regionInfo.Elevation2SE;
  309. heightRanges[3] = (float)regionInfo.Elevation2NE;
  310. uint globalX, globalY;
  311. Util.RegionHandleToWorldLoc(m_scene.RegionInfo.RegionHandle, out globalX, out globalY);
  312. warp_Texture texture;
  313. using (
  314. Bitmap image
  315. = TerrainSplat.Splat(terrain, textureIDs, startHeights, heightRanges,
  316. new Vector3d(globalX, globalY, 0.0), m_scene.AssetService, textureTerrain))
  317. {
  318. texture = new warp_Texture(image);
  319. }
  320. warp_Material material = new warp_Material(texture);
  321. material.setReflectivity(50);
  322. renderer.Scene.addMaterial("TerrainColor", material);
  323. renderer.Scene.material("TerrainColor").setReflectivity(0); // reduces tile seams a bit thanks lkalif
  324. renderer.SetObjectMaterial("Terrain", "TerrainColor");
  325. }
  326. private void CreateAllPrims(WarpRenderer renderer, bool useTextures)
  327. {
  328. if (m_primMesher == null)
  329. return;
  330. m_scene.ForEachSOG(
  331. delegate(SceneObjectGroup group)
  332. {
  333. CreatePrim(renderer, group.RootPart, useTextures);
  334. foreach (SceneObjectPart child in group.Parts)
  335. CreatePrim(renderer, child, useTextures);
  336. }
  337. );
  338. }
  339. private void CreatePrim(WarpRenderer renderer, SceneObjectPart prim,
  340. bool useTextures)
  341. {
  342. const float MIN_SIZE = 2f;
  343. if ((PCode)prim.Shape.PCode != PCode.Prim)
  344. return;
  345. if (prim.Scale.LengthSquared() < MIN_SIZE * MIN_SIZE)
  346. return;
  347. FacetedMesh renderMesh = null;
  348. Primitive omvPrim = prim.Shape.ToOmvPrimitive(prim.OffsetPosition, prim.RotationOffset);
  349. if (m_renderMeshes)
  350. {
  351. if (omvPrim.Sculpt != null && omvPrim.Sculpt.SculptTexture != UUID.Zero)
  352. {
  353. // Try fetchinng the asset
  354. byte[] sculptAsset = m_scene.AssetService.GetData(omvPrim.Sculpt.SculptTexture.ToString());
  355. if (sculptAsset != null)
  356. {
  357. // Is it a mesh?
  358. if (omvPrim.Sculpt.Type == SculptType.Mesh)
  359. {
  360. AssetMesh meshAsset = new AssetMesh(omvPrim.Sculpt.SculptTexture, sculptAsset);
  361. FacetedMesh.TryDecodeFromAsset(omvPrim, meshAsset, DetailLevel.Highest, out renderMesh);
  362. meshAsset = null;
  363. }
  364. else // It's sculptie
  365. {
  366. IJ2KDecoder imgDecoder = m_scene.RequestModuleInterface<IJ2KDecoder>();
  367. if (imgDecoder != null)
  368. {
  369. Image sculpt = imgDecoder.DecodeToImage(sculptAsset);
  370. if (sculpt != null)
  371. {
  372. renderMesh = m_primMesher.GenerateFacetedSculptMesh(omvPrim, (Bitmap)sculpt,
  373. DetailLevel.Medium);
  374. sculpt.Dispose();
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. // If not a mesh or sculptie, try the regular mesher
  382. if (renderMesh == null)
  383. {
  384. renderMesh = m_primMesher.GenerateFacetedMesh(omvPrim, DetailLevel.Medium);
  385. }
  386. if (renderMesh == null)
  387. return;
  388. warp_Vector primPos = ConvertVector(prim.GetWorldPosition());
  389. warp_Quaternion primRot = ConvertQuaternion(prim.RotationOffset);
  390. warp_Matrix m = warp_Matrix.quaternionMatrix(primRot);
  391. if (prim.ParentID != 0)
  392. {
  393. SceneObjectGroup group = m_scene.SceneGraph.GetGroupByPrim(prim.LocalId);
  394. if (group != null)
  395. m.transform(warp_Matrix.quaternionMatrix(ConvertQuaternion(group.RootPart.RotationOffset)));
  396. }
  397. warp_Vector primScale = ConvertVector(prim.Scale);
  398. string primID = prim.UUID.ToString();
  399. // Create the prim faces
  400. // TODO: Implement the useTextures flag behavior
  401. for (int i = 0; i < renderMesh.Faces.Count; i++)
  402. {
  403. Face face = renderMesh.Faces[i];
  404. string meshName = primID + "-Face-" + i.ToString();
  405. // Avoid adding duplicate meshes to the scene
  406. if (renderer.Scene.objectData.ContainsKey(meshName))
  407. {
  408. continue;
  409. }
  410. warp_Object faceObj = new warp_Object(face.Vertices.Count, face.Indices.Count / 3);
  411. for (int j = 0; j < face.Vertices.Count; j++)
  412. {
  413. Vertex v = face.Vertices[j];
  414. warp_Vector pos = ConvertVector(v.Position);
  415. warp_Vector norm = ConvertVector(v.Normal);
  416. if (prim.Shape.SculptTexture == UUID.Zero)
  417. norm = norm.reverse();
  418. warp_Vertex vert = new warp_Vertex(pos, norm, v.TexCoord.X, v.TexCoord.Y);
  419. faceObj.addVertex(vert);
  420. }
  421. for (int j = 0; j < face.Indices.Count; j += 3)
  422. {
  423. faceObj.addTriangle(
  424. face.Indices[j + 0],
  425. face.Indices[j + 1],
  426. face.Indices[j + 2]);
  427. }
  428. Primitive.TextureEntryFace teFace = prim.Shape.Textures.GetFace((uint)i);
  429. Color4 faceColor = GetFaceColor(teFace);
  430. string materialName = String.Empty;
  431. if (m_texturePrims && prim.Scale.LengthSquared() > m_texturePrimSize*m_texturePrimSize)
  432. materialName = GetOrCreateMaterial(renderer, faceColor, teFace.TextureID);
  433. else
  434. materialName = GetOrCreateMaterial(renderer, faceColor);
  435. faceObj.transform(m);
  436. faceObj.setPos(primPos);
  437. faceObj.scaleSelf(primScale.x, primScale.y, primScale.z);
  438. renderer.Scene.addObject(meshName, faceObj);
  439. renderer.SetObjectMaterial(meshName, materialName);
  440. }
  441. }
  442. private Color4 GetFaceColor(Primitive.TextureEntryFace face)
  443. {
  444. Color4 color;
  445. if (face.TextureID == UUID.Zero)
  446. return face.RGBA;
  447. if (!m_colors.TryGetValue(face.TextureID, out color))
  448. {
  449. bool fetched = false;
  450. // Attempt to fetch the texture metadata
  451. UUID metadataID = UUID.Combine(face.TextureID, TEXTURE_METADATA_MAGIC);
  452. AssetBase metadata = m_scene.AssetService.GetCached(metadataID.ToString());
  453. if (metadata != null)
  454. {
  455. OSDMap map = null;
  456. try { map = OSDParser.Deserialize(metadata.Data) as OSDMap; } catch { }
  457. if (map != null)
  458. {
  459. color = map["X-JPEG2000-RGBA"].AsColor4();
  460. fetched = true;
  461. }
  462. }
  463. if (!fetched)
  464. {
  465. // Fetch the texture, decode and get the average color,
  466. // then save it to a temporary metadata asset
  467. AssetBase textureAsset = m_scene.AssetService.Get(face.TextureID.ToString());
  468. if (textureAsset != null)
  469. {
  470. int width, height;
  471. color = GetAverageColor(textureAsset.FullID, textureAsset.Data, out width, out height);
  472. OSDMap data = new OSDMap { { "X-JPEG2000-RGBA", OSD.FromColor4(color) } };
  473. metadata = new AssetBase
  474. {
  475. Data = System.Text.Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(data)),
  476. Description = "Metadata for JPEG2000 texture " + face.TextureID.ToString(),
  477. Flags = AssetFlags.Collectable,
  478. FullID = metadataID,
  479. ID = metadataID.ToString(),
  480. Local = true,
  481. Temporary = true,
  482. Name = String.Empty,
  483. Type = (sbyte)AssetType.Unknown
  484. };
  485. m_scene.AssetService.Store(metadata);
  486. }
  487. else
  488. {
  489. color = new Color4(0.5f, 0.5f, 0.5f, 1.0f);
  490. }
  491. }
  492. m_colors[face.TextureID] = color;
  493. }
  494. return color * face.RGBA;
  495. }
  496. private string GetOrCreateMaterial(WarpRenderer renderer, Color4 color)
  497. {
  498. string name = color.ToString();
  499. warp_Material material = renderer.Scene.material(name);
  500. if (material != null)
  501. return name;
  502. renderer.AddMaterial(name, ConvertColor(color));
  503. if (color.A < 1f)
  504. renderer.Scene.material(name).setTransparency((byte)((1f - color.A) * 255f));
  505. return name;
  506. }
  507. public string GetOrCreateMaterial(WarpRenderer renderer, Color4 faceColor, UUID textureID)
  508. {
  509. string materialName = "Color-" + faceColor.ToString() + "-Texture-" + textureID.ToString();
  510. if (renderer.Scene.material(materialName) == null)
  511. {
  512. renderer.AddMaterial(materialName, ConvertColor(faceColor));
  513. if (faceColor.A < 1f)
  514. {
  515. renderer.Scene.material(materialName).setTransparency((byte) ((1f - faceColor.A)*255f));
  516. }
  517. warp_Texture texture = GetTexture(textureID);
  518. if (texture != null)
  519. renderer.Scene.material(materialName).setTexture(texture);
  520. }
  521. return materialName;
  522. }
  523. private warp_Texture GetTexture(UUID id)
  524. {
  525. warp_Texture ret = null;
  526. byte[] asset = m_scene.AssetService.GetData(id.ToString());
  527. if (asset != null)
  528. {
  529. IJ2KDecoder imgDecoder = m_scene.RequestModuleInterface<IJ2KDecoder>();
  530. try
  531. {
  532. using (Bitmap img = (Bitmap)imgDecoder.DecodeToImage(asset))
  533. ret = new warp_Texture(img);
  534. }
  535. catch (Exception e)
  536. {
  537. m_log.Warn(string.Format("[WARP 3D IMAGE MODULE]: Failed to decode asset {0}, exception ", id), e);
  538. }
  539. }
  540. return ret;
  541. }
  542. #endregion Rendering Methods
  543. #region Static Helpers
  544. // Note: axis change.
  545. private static warp_Vector ConvertVector(float x, float y, float z)
  546. {
  547. return new warp_Vector(x, z, y);
  548. }
  549. private static warp_Vector ConvertVector(Vector3 vector)
  550. {
  551. return new warp_Vector(vector.X, vector.Z, vector.Y);
  552. }
  553. private static warp_Quaternion ConvertQuaternion(Quaternion quat)
  554. {
  555. return new warp_Quaternion(quat.X, quat.Z, quat.Y, -quat.W);
  556. }
  557. private static int ConvertColor(Color4 color)
  558. {
  559. int c = warp_Color.getColor((byte)(color.R * 255f), (byte)(color.G * 255f), (byte)(color.B * 255f));
  560. if (color.A < 1f)
  561. c |= (byte)(color.A * 255f) << 24;
  562. return c;
  563. }
  564. private static Vector3 SurfaceNormal(Vector3 c1, Vector3 c2, Vector3 c3)
  565. {
  566. Vector3 edge1 = new Vector3(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  567. Vector3 edge2 = new Vector3(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  568. Vector3 normal = Vector3.Cross(edge1, edge2);
  569. normal.Normalize();
  570. return normal;
  571. }
  572. public static Color4 GetAverageColor(UUID textureID, byte[] j2kData, out int width, out int height)
  573. {
  574. ulong r = 0;
  575. ulong g = 0;
  576. ulong b = 0;
  577. ulong a = 0;
  578. using (MemoryStream stream = new MemoryStream(j2kData))
  579. {
  580. try
  581. {
  582. int pixelBytes;
  583. using (Bitmap bitmap = (Bitmap)J2kImage.FromStream(stream))
  584. {
  585. width = bitmap.Width;
  586. height = bitmap.Height;
  587. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  588. pixelBytes = (bitmap.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
  589. // Sum up the individual channels
  590. unsafe
  591. {
  592. if (pixelBytes == 4)
  593. {
  594. for (int y = 0; y < height; y++)
  595. {
  596. byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  597. for (int x = 0; x < width; x++)
  598. {
  599. b += row[x * pixelBytes + 0];
  600. g += row[x * pixelBytes + 1];
  601. r += row[x * pixelBytes + 2];
  602. a += row[x * pixelBytes + 3];
  603. }
  604. }
  605. }
  606. else
  607. {
  608. for (int y = 0; y < height; y++)
  609. {
  610. byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  611. for (int x = 0; x < width; x++)
  612. {
  613. b += row[x * pixelBytes + 0];
  614. g += row[x * pixelBytes + 1];
  615. r += row[x * pixelBytes + 2];
  616. }
  617. }
  618. }
  619. }
  620. }
  621. // Get the averages for each channel
  622. const decimal OO_255 = 1m / 255m;
  623. decimal totalPixels = (decimal)(width * height);
  624. decimal rm = ((decimal)r / totalPixels) * OO_255;
  625. decimal gm = ((decimal)g / totalPixels) * OO_255;
  626. decimal bm = ((decimal)b / totalPixels) * OO_255;
  627. decimal am = ((decimal)a / totalPixels) * OO_255;
  628. if (pixelBytes == 3)
  629. am = 1m;
  630. return new Color4((float)rm, (float)gm, (float)bm, (float)am);
  631. }
  632. catch (Exception ex)
  633. {
  634. m_log.WarnFormat(
  635. "[WARP 3D IMAGE MODULE]: Error decoding JPEG2000 texture {0} ({1} bytes): {2}",
  636. textureID, j2kData.Length, ex.Message);
  637. width = 0;
  638. height = 0;
  639. return new Color4(0.5f, 0.5f, 0.5f, 1.0f);
  640. }
  641. }
  642. }
  643. #endregion Static Helpers
  644. }
  645. public static class ImageUtils
  646. {
  647. /// <summary>
  648. /// Performs bilinear interpolation between four values
  649. /// </summary>
  650. /// <param name="v00">First, or top left value</param>
  651. /// <param name="v01">Second, or top right value</param>
  652. /// <param name="v10">Third, or bottom left value</param>
  653. /// <param name="v11">Fourth, or bottom right value</param>
  654. /// <param name="xPercent">Interpolation value on the X axis, between 0.0 and 1.0</param>
  655. /// <param name="yPercent">Interpolation value on fht Y axis, between 0.0 and 1.0</param>
  656. /// <returns>The bilinearly interpolated result</returns>
  657. public static float Bilinear(float v00, float v01, float v10, float v11, float xPercent, float yPercent)
  658. {
  659. return Utils.Lerp(Utils.Lerp(v00, v01, xPercent), Utils.Lerp(v10, v11, xPercent), yPercent);
  660. }
  661. /// <summary>
  662. /// Performs a high quality image resize
  663. /// </summary>
  664. /// <param name="image">Image to resize</param>
  665. /// <param name="width">New width</param>
  666. /// <param name="height">New height</param>
  667. /// <returns>Resized image</returns>
  668. public static Bitmap ResizeImage(Image image, int width, int height)
  669. {
  670. Bitmap result = new Bitmap(width, height);
  671. using (Graphics graphics = Graphics.FromImage(result))
  672. {
  673. graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  674. graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  675. graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  676. graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  677. graphics.DrawImage(image, 0, 0, result.Width, result.Height);
  678. }
  679. return result;
  680. }
  681. }
  682. }