Warp3DImageModule.cs 33 KB

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