Warp3DImageModule.cs 33 KB

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