Merge branch 'ubitwork'
commit
808995fb68
|
@ -845,9 +845,7 @@ namespace OpenSim.Region.ClientStack.Linden
|
||||||
public string GetObjectCost(string request, string path,
|
public string GetObjectCost(string request, string path,
|
||||||
string param, IOSHttpRequest httpRequest,
|
string param, IOSHttpRequest httpRequest,
|
||||||
IOSHttpResponse httpResponse)
|
IOSHttpResponse httpResponse)
|
||||||
{
|
{
|
||||||
// see being triggered but see no efect .. have something wrong ??
|
|
||||||
//
|
|
||||||
OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
|
OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
|
||||||
OSDMap resp = new OSDMap();
|
OSDMap resp = new OSDMap();
|
||||||
|
|
||||||
|
@ -856,19 +854,29 @@ namespace OpenSim.Region.ClientStack.Linden
|
||||||
for (int i = 0; i < object_ids.Count; i++)
|
for (int i = 0; i < object_ids.Count; i++)
|
||||||
{
|
{
|
||||||
UUID uuid = object_ids[i].AsUUID();
|
UUID uuid = object_ids[i].AsUUID();
|
||||||
|
|
||||||
|
SceneObjectPart part = m_Scene.GetSceneObjectPart(uuid);
|
||||||
|
|
||||||
// only see root parts .. so guess should go by SOG only
|
if (part != null)
|
||||||
SceneObjectPart obj = m_Scene.GetSceneObjectPart(uuid);
|
|
||||||
if (obj != null)
|
|
||||||
{
|
{
|
||||||
OSDMap object_data = new OSDMap();
|
SceneObjectGroup grp = part.ParentGroup;
|
||||||
|
if (grp != null)
|
||||||
|
{
|
||||||
|
float linksetCost;
|
||||||
|
float linksetPhysCost;
|
||||||
|
float partCost;
|
||||||
|
float partPhysCost;
|
||||||
|
|
||||||
object_data["linked_set_resource_cost"] = 1.0f;
|
grp.GetResourcesCosts(part, out linksetCost, out linksetPhysCost, out partCost, out partPhysCost);
|
||||||
object_data["resource_cost"] = 1.0f;
|
|
||||||
object_data["physics_cost"] = 1.0f;
|
|
||||||
object_data["linked_set_physics_cost"] = 1.0f;
|
|
||||||
|
|
||||||
resp[uuid.ToString()] = object_data;
|
OSDMap object_data = new OSDMap();
|
||||||
|
object_data["linked_set_resource_cost"] = linksetCost;
|
||||||
|
object_data["resource_cost"] = partCost;
|
||||||
|
object_data["physics_cost"] = partPhysCost;
|
||||||
|
object_data["linked_set_physics_cost"] = linksetPhysCost;
|
||||||
|
|
||||||
|
resp[uuid.ToString()] = object_data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -899,13 +907,17 @@ namespace OpenSim.Region.ClientStack.Linden
|
||||||
for (int i = 0; i < object_ids.Count; i++)
|
for (int i = 0; i < object_ids.Count; i++)
|
||||||
{
|
{
|
||||||
UUID uuid = object_ids[i].AsUUID();
|
UUID uuid = object_ids[i].AsUUID();
|
||||||
|
float Physc;
|
||||||
|
float simulc;
|
||||||
|
float streamc;
|
||||||
|
|
||||||
SceneObjectPart obj = m_Scene.GetSceneObjectPart(uuid);
|
SceneObjectGroup grp = m_Scene.GetGroupByPrim(uuid);
|
||||||
if (obj != null)
|
if (grp != null)
|
||||||
{
|
{
|
||||||
phys += 0.1f; // just to see...
|
grp.GetSelectedCosts(out Physc, out streamc, out simulc);
|
||||||
stream += 0.2f;
|
phys += Physc;
|
||||||
simul += 0.5f;
|
stream += streamc;
|
||||||
|
simul += simulc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -920,12 +932,12 @@ namespace OpenSim.Region.ClientStack.Linden
|
||||||
{
|
{
|
||||||
UUID uuid = object_ids[i].AsUUID();
|
UUID uuid = object_ids[i].AsUUID();
|
||||||
|
|
||||||
SceneObjectPart obj = m_Scene.GetSceneObjectPart(uuid);
|
SceneObjectPart part = m_Scene.GetSceneObjectPart(uuid);
|
||||||
if (obj != null)
|
if (part != null)
|
||||||
{
|
{
|
||||||
phys += 0.1f;
|
phys += part.PhysicsCost;
|
||||||
stream += 0.2f;
|
stream += part.StreamingCost;
|
||||||
simul += 0.5f;
|
simul += part.SimulationCost;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1229,6 +1229,102 @@ namespace OpenSim.Region.Framework.Scenes
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
public void GetResourcesCosts(SceneObjectPart apart,
|
||||||
|
out float linksetResCost, out float linksetPhysCost, out float partCost, out float partPhysCost)
|
||||||
|
{
|
||||||
|
// this information may need to be cached
|
||||||
|
|
||||||
|
float cost;
|
||||||
|
float tmpcost;
|
||||||
|
|
||||||
|
bool ComplexCost = false;
|
||||||
|
|
||||||
|
SceneObjectPart p;
|
||||||
|
SceneObjectPart[] parts;
|
||||||
|
|
||||||
|
lock (m_parts)
|
||||||
|
{
|
||||||
|
parts = m_parts.GetArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
int nparts = parts.Length;
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < nparts; i++)
|
||||||
|
{
|
||||||
|
p = parts[i];
|
||||||
|
|
||||||
|
if (p.UsesComplexCost)
|
||||||
|
{
|
||||||
|
ComplexCost = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ComplexCost)
|
||||||
|
{
|
||||||
|
linksetResCost = 0;
|
||||||
|
linksetPhysCost = 0;
|
||||||
|
partCost = 0;
|
||||||
|
partPhysCost = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < nparts; i++)
|
||||||
|
{
|
||||||
|
p = parts[i];
|
||||||
|
|
||||||
|
cost = p.StreamingCost;
|
||||||
|
tmpcost = p.SimulationCost;
|
||||||
|
if (tmpcost > cost)
|
||||||
|
cost = tmpcost;
|
||||||
|
tmpcost = p.PhysicsCost;
|
||||||
|
if (tmpcost > cost)
|
||||||
|
cost = tmpcost;
|
||||||
|
|
||||||
|
linksetPhysCost += tmpcost;
|
||||||
|
linksetResCost += cost;
|
||||||
|
|
||||||
|
if (p == apart)
|
||||||
|
{
|
||||||
|
partCost = cost;
|
||||||
|
partPhysCost = tmpcost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
partPhysCost = 1.0f;
|
||||||
|
partCost = 1.0f;
|
||||||
|
linksetResCost = (float)nparts;
|
||||||
|
linksetPhysCost = linksetResCost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetSelectedCosts(out float PhysCost, out float StreamCost, out float SimulCost)
|
||||||
|
{
|
||||||
|
SceneObjectPart p;
|
||||||
|
SceneObjectPart[] parts;
|
||||||
|
|
||||||
|
lock (m_parts)
|
||||||
|
{
|
||||||
|
parts = m_parts.GetArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
int nparts = parts.Length;
|
||||||
|
|
||||||
|
PhysCost = 0;
|
||||||
|
StreamCost = 0;
|
||||||
|
SimulCost = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < nparts; i++)
|
||||||
|
{
|
||||||
|
p = parts[i];
|
||||||
|
|
||||||
|
StreamCost += p.StreamingCost;
|
||||||
|
SimulCost += p.SimulationCost;
|
||||||
|
PhysCost += p.PhysicsCost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SaveScriptedState(XmlTextWriter writer)
|
public void SaveScriptedState(XmlTextWriter writer)
|
||||||
{
|
{
|
||||||
SaveScriptedState(writer, false);
|
SaveScriptedState(writer, false);
|
||||||
|
|
|
@ -1415,6 +1415,14 @@ namespace OpenSim.Region.Framework.Scenes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// not a propriety to move to methods place later
|
||||||
|
private bool HasMesh()
|
||||||
|
{
|
||||||
|
if (Shape != null && (Shape.SculptType == (byte)SculptType.Mesh))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// not a propriety to move to methods place later
|
// not a propriety to move to methods place later
|
||||||
public byte DefaultPhysicsShapeType()
|
public byte DefaultPhysicsShapeType()
|
||||||
{
|
{
|
||||||
|
@ -1428,6 +1436,60 @@ namespace OpenSim.Region.Framework.Scenes
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[XmlIgnore]
|
||||||
|
public bool UsesComplexCost
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
byte pst = PhysicsShapeType;
|
||||||
|
if(pst == (byte) PhysShapeType.none || pst == (byte) PhysShapeType.convex || HasMesh())
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[XmlIgnore]
|
||||||
|
public float PhysicsCost
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if(PhysicsShapeType == (byte)PhysShapeType.none)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
float cost = 0.1f;
|
||||||
|
if (PhysActor != null)
|
||||||
|
// cost += PhysActor.Cost;
|
||||||
|
|
||||||
|
if ((Flags & PrimFlags.Physics) != 0)
|
||||||
|
cost *= (1.0f + 0.01333f * Scale.LengthSquared()); // 0.01333 == 0.04/3
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[XmlIgnore]
|
||||||
|
public float StreamingCost
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return 0.1f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[XmlIgnore]
|
||||||
|
public float SimulationCost
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
// ignoring scripts. Don't like considering them for this
|
||||||
|
if((Flags & PrimFlags.Physics) != 0)
|
||||||
|
return 1.0f;
|
||||||
|
|
||||||
|
return 0.5f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public byte PhysicsShapeType
|
public byte PhysicsShapeType
|
||||||
{
|
{
|
||||||
get { return m_physicsShapeType; }
|
get { return m_physicsShapeType; }
|
||||||
|
|
|
@ -0,0 +1,436 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) Contributors, http://opensimulator.org/
|
||||||
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of the OpenSimulator Project nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Globalization;
|
||||||
|
using OpenMetaverse;
|
||||||
|
using OpenSim.Region.Physics.Manager;
|
||||||
|
using OpenSim.Region.Physics.Meshing;
|
||||||
|
|
||||||
|
public class Vertex : IComparable<Vertex>
|
||||||
|
{
|
||||||
|
Vector3 vector;
|
||||||
|
|
||||||
|
public float X
|
||||||
|
{
|
||||||
|
get { return vector.X; }
|
||||||
|
set { vector.X = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public float Y
|
||||||
|
{
|
||||||
|
get { return vector.Y; }
|
||||||
|
set { vector.Y = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public float Z
|
||||||
|
{
|
||||||
|
get { return vector.Z; }
|
||||||
|
set { vector.Z = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vertex(float x, float y, float z)
|
||||||
|
{
|
||||||
|
vector.X = x;
|
||||||
|
vector.Y = y;
|
||||||
|
vector.Z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vertex normalize()
|
||||||
|
{
|
||||||
|
float tlength = vector.Length();
|
||||||
|
if (tlength != 0f)
|
||||||
|
{
|
||||||
|
float mul = 1.0f / tlength;
|
||||||
|
return new Vertex(vector.X * mul, vector.Y * mul, vector.Z * mul);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Vertex(0f, 0f, 0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vertex cross(Vertex v)
|
||||||
|
{
|
||||||
|
return new Vertex(vector.Y * v.Z - vector.Z * v.Y, vector.Z * v.X - vector.X * v.Z, vector.X * v.Y - vector.Y * v.X);
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable warning: mono compiler moans about overloading
|
||||||
|
// operators hiding base operator but should not according to C#
|
||||||
|
// language spec
|
||||||
|
#pragma warning disable 0108
|
||||||
|
public static Vertex operator *(Vertex v, Quaternion q)
|
||||||
|
{
|
||||||
|
// From http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/
|
||||||
|
|
||||||
|
Vertex v2 = new Vertex(0f, 0f, 0f);
|
||||||
|
|
||||||
|
v2.X = q.W * q.W * v.X +
|
||||||
|
2f * q.Y * q.W * v.Z -
|
||||||
|
2f * q.Z * q.W * v.Y +
|
||||||
|
q.X * q.X * v.X +
|
||||||
|
2f * q.Y * q.X * v.Y +
|
||||||
|
2f * q.Z * q.X * v.Z -
|
||||||
|
q.Z * q.Z * v.X -
|
||||||
|
q.Y * q.Y * v.X;
|
||||||
|
|
||||||
|
v2.Y =
|
||||||
|
2f * q.X * q.Y * v.X +
|
||||||
|
q.Y * q.Y * v.Y +
|
||||||
|
2f * q.Z * q.Y * v.Z +
|
||||||
|
2f * q.W * q.Z * v.X -
|
||||||
|
q.Z * q.Z * v.Y +
|
||||||
|
q.W * q.W * v.Y -
|
||||||
|
2f * q.X * q.W * v.Z -
|
||||||
|
q.X * q.X * v.Y;
|
||||||
|
|
||||||
|
v2.Z =
|
||||||
|
2f * q.X * q.Z * v.X +
|
||||||
|
2f * q.Y * q.Z * v.Y +
|
||||||
|
q.Z * q.Z * v.Z -
|
||||||
|
2f * q.W * q.Y * v.X -
|
||||||
|
q.Y * q.Y * v.Z +
|
||||||
|
2f * q.W * q.X * v.Y -
|
||||||
|
q.X * q.X * v.Z +
|
||||||
|
q.W * q.W * v.Z;
|
||||||
|
|
||||||
|
return v2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator +(Vertex v1, Vertex v2)
|
||||||
|
{
|
||||||
|
return new Vertex(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator -(Vertex v1, Vertex v2)
|
||||||
|
{
|
||||||
|
return new Vertex(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator *(Vertex v1, Vertex v2)
|
||||||
|
{
|
||||||
|
return new Vertex(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator +(Vertex v1, float am)
|
||||||
|
{
|
||||||
|
v1.X += am;
|
||||||
|
v1.Y += am;
|
||||||
|
v1.Z += am;
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator -(Vertex v1, float am)
|
||||||
|
{
|
||||||
|
v1.X -= am;
|
||||||
|
v1.Y -= am;
|
||||||
|
v1.Z -= am;
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator *(Vertex v1, float am)
|
||||||
|
{
|
||||||
|
v1.X *= am;
|
||||||
|
v1.Y *= am;
|
||||||
|
v1.Z *= am;
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex operator /(Vertex v1, float am)
|
||||||
|
{
|
||||||
|
if (am == 0f)
|
||||||
|
{
|
||||||
|
return new Vertex(0f,0f,0f);
|
||||||
|
}
|
||||||
|
float mul = 1.0f / am;
|
||||||
|
v1.X *= mul;
|
||||||
|
v1.Y *= mul;
|
||||||
|
v1.Z *= mul;
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
#pragma warning restore 0108
|
||||||
|
|
||||||
|
|
||||||
|
public float dot(Vertex v)
|
||||||
|
{
|
||||||
|
return X * v.X + Y * v.Y + Z * v.Z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vertex(Vector3 v)
|
||||||
|
{
|
||||||
|
vector = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vertex Clone()
|
||||||
|
{
|
||||||
|
return new Vertex(X, Y, Z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vertex FromAngle(double angle)
|
||||||
|
{
|
||||||
|
return new Vertex((float) Math.Cos(angle), (float) Math.Sin(angle), 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float Length()
|
||||||
|
{
|
||||||
|
return vector.Length();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool Equals(Vertex v, float tolerance)
|
||||||
|
{
|
||||||
|
Vertex diff = this - v;
|
||||||
|
float d = diff.Length();
|
||||||
|
if (d < tolerance)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int CompareTo(Vertex other)
|
||||||
|
{
|
||||||
|
if (X < other.X)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (X > other.X)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (Y < other.Y)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (Y > other.Y)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (Z < other.Z)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (Z > other.Z)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator >(Vertex me, Vertex other)
|
||||||
|
{
|
||||||
|
return me.CompareTo(other) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator <(Vertex me, Vertex other)
|
||||||
|
{
|
||||||
|
return me.CompareTo(other) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String ToRaw()
|
||||||
|
{
|
||||||
|
// Why this stuff with the number formatter?
|
||||||
|
// Well, the raw format uses the english/US notation of numbers
|
||||||
|
// where the "," separates groups of 1000 while the "." marks the border between 1 and 10E-1.
|
||||||
|
// The german notation uses these characters exactly vice versa!
|
||||||
|
// The Float.ToString() routine is a localized one, giving different results depending on the country
|
||||||
|
// settings your machine works with. Unusable for a machine readable file format :-(
|
||||||
|
NumberFormatInfo nfi = new NumberFormatInfo();
|
||||||
|
nfi.NumberDecimalSeparator = ".";
|
||||||
|
nfi.NumberDecimalDigits = 3;
|
||||||
|
|
||||||
|
String s1 = X.ToString("N2", nfi) + " " + Y.ToString("N2", nfi) + " " + Z.ToString("N2", nfi);
|
||||||
|
|
||||||
|
return s1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Triangle
|
||||||
|
{
|
||||||
|
public Vertex v1;
|
||||||
|
public Vertex v2;
|
||||||
|
public Vertex v3;
|
||||||
|
|
||||||
|
private float radius_square;
|
||||||
|
private float cx;
|
||||||
|
private float cy;
|
||||||
|
|
||||||
|
public Triangle(Vertex _v1, Vertex _v2, Vertex _v3)
|
||||||
|
{
|
||||||
|
v1 = _v1;
|
||||||
|
v2 = _v2;
|
||||||
|
v3 = _v3;
|
||||||
|
|
||||||
|
CalcCircle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool isInCircle(float x, float y)
|
||||||
|
{
|
||||||
|
float dx, dy;
|
||||||
|
float dd;
|
||||||
|
|
||||||
|
dx = x - cx;
|
||||||
|
dy = y - cy;
|
||||||
|
|
||||||
|
dd = dx*dx + dy*dy;
|
||||||
|
if (dd < radius_square)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool isDegraded()
|
||||||
|
{
|
||||||
|
// This means, the vertices of this triangle are somewhat strange.
|
||||||
|
// They either line up or at least two of them are identical
|
||||||
|
return (radius_square == 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CalcCircle()
|
||||||
|
{
|
||||||
|
// Calculate the center and the radius of a circle given by three points p1, p2, p3
|
||||||
|
// It is assumed, that the triangles vertices are already set correctly
|
||||||
|
double p1x, p2x, p1y, p2y, p3x, p3y;
|
||||||
|
|
||||||
|
// Deviation of this routine:
|
||||||
|
// A circle has the general equation (M-p)^2=r^2, where M and p are vectors
|
||||||
|
// this gives us three equations f(p)=r^2, each for one point p1, p2, p3
|
||||||
|
// putting respectively two equations together gives two equations
|
||||||
|
// f(p1)=f(p2) and f(p1)=f(p3)
|
||||||
|
// bringing all constant terms to one side brings them to the form
|
||||||
|
// M*v1=c1 resp.M*v2=c2 where v1=(p1-p2) and v2=(p1-p3) (still vectors)
|
||||||
|
// and c1, c2 are scalars (Naming conventions like the variables below)
|
||||||
|
// Now using the equations that are formed by the components of the vectors
|
||||||
|
// and isolate Mx lets you make one equation that only holds My
|
||||||
|
// The rest is straight forward and eaasy :-)
|
||||||
|
//
|
||||||
|
|
||||||
|
/* helping variables for temporary results */
|
||||||
|
double c1, c2;
|
||||||
|
double v1x, v1y, v2x, v2y;
|
||||||
|
|
||||||
|
double z, n;
|
||||||
|
|
||||||
|
double rx, ry;
|
||||||
|
|
||||||
|
// Readout the three points, the triangle consists of
|
||||||
|
p1x = v1.X;
|
||||||
|
p1y = v1.Y;
|
||||||
|
|
||||||
|
p2x = v2.X;
|
||||||
|
p2y = v2.Y;
|
||||||
|
|
||||||
|
p3x = v3.X;
|
||||||
|
p3y = v3.Y;
|
||||||
|
|
||||||
|
/* calc helping values first */
|
||||||
|
c1 = (p1x*p1x + p1y*p1y - p2x*p2x - p2y*p2y)/2;
|
||||||
|
c2 = (p1x*p1x + p1y*p1y - p3x*p3x - p3y*p3y)/2;
|
||||||
|
|
||||||
|
v1x = p1x - p2x;
|
||||||
|
v1y = p1y - p2y;
|
||||||
|
|
||||||
|
v2x = p1x - p3x;
|
||||||
|
v2y = p1y - p3y;
|
||||||
|
|
||||||
|
z = (c1*v2x - c2*v1x);
|
||||||
|
n = (v1y*v2x - v2y*v1x);
|
||||||
|
|
||||||
|
if (n == 0.0) // This is no triangle, i.e there are (at least) two points at the same location
|
||||||
|
{
|
||||||
|
radius_square = 0.0f;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cy = (float) (z/n);
|
||||||
|
|
||||||
|
if (v2x != 0.0)
|
||||||
|
{
|
||||||
|
cx = (float) ((c2 - v2y*cy)/v2x);
|
||||||
|
}
|
||||||
|
else if (v1x != 0.0)
|
||||||
|
{
|
||||||
|
cx = (float) ((c1 - v1y*cy)/v1x);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Assert(false, "Malformed triangle"); /* Both terms zero means nothing good */
|
||||||
|
}
|
||||||
|
|
||||||
|
rx = (p1x - cx);
|
||||||
|
ry = (p1y - cy);
|
||||||
|
|
||||||
|
radius_square = (float) (rx*rx + ry*ry);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override String ToString()
|
||||||
|
{
|
||||||
|
NumberFormatInfo nfi = new NumberFormatInfo();
|
||||||
|
nfi.CurrencyDecimalDigits = 2;
|
||||||
|
nfi.CurrencyDecimalSeparator = ".";
|
||||||
|
|
||||||
|
String s1 = "<" + v1.X.ToString(nfi) + "," + v1.Y.ToString(nfi) + "," + v1.Z.ToString(nfi) + ">";
|
||||||
|
String s2 = "<" + v2.X.ToString(nfi) + "," + v2.Y.ToString(nfi) + "," + v2.Z.ToString(nfi) + ">";
|
||||||
|
String s3 = "<" + v3.X.ToString(nfi) + "," + v3.Y.ToString(nfi) + "," + v3.Z.ToString(nfi) + ">";
|
||||||
|
|
||||||
|
return s1 + ";" + s2 + ";" + s3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector3 getNormal()
|
||||||
|
{
|
||||||
|
// Vertices
|
||||||
|
|
||||||
|
// Vectors for edges
|
||||||
|
Vector3 e1;
|
||||||
|
Vector3 e2;
|
||||||
|
|
||||||
|
e1 = new Vector3(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
|
||||||
|
e2 = new Vector3(v1.X - v3.X, v1.Y - v3.Y, v1.Z - v3.Z);
|
||||||
|
|
||||||
|
// Cross product for normal
|
||||||
|
Vector3 n = Vector3.Cross(e1, e2);
|
||||||
|
|
||||||
|
// Length
|
||||||
|
float l = n.Length();
|
||||||
|
|
||||||
|
// Normalized "normal"
|
||||||
|
n = n/l;
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void invertNormal()
|
||||||
|
{
|
||||||
|
Vertex vt;
|
||||||
|
vt = v1;
|
||||||
|
v1 = v2;
|
||||||
|
v2 = vt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dumps a triangle in the "raw faces" format, blender can import. This is for visualisation and
|
||||||
|
// debugging purposes
|
||||||
|
public String ToStringRaw()
|
||||||
|
{
|
||||||
|
String output = v1.ToRaw() + " " + v2.ToRaw() + " " + v3.ToRaw();
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,401 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) Contributors, http://opensimulator.org/
|
||||||
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of the OpenSimulator Project nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using OpenSim.Region.Physics.Manager;
|
||||||
|
using PrimMesher;
|
||||||
|
using OpenMetaverse;
|
||||||
|
|
||||||
|
namespace OpenSim.Region.Physics.Meshing
|
||||||
|
{
|
||||||
|
public class Mesh : IMesh
|
||||||
|
{
|
||||||
|
private Dictionary<Vertex, int> m_vertices;
|
||||||
|
private List<Triangle> m_triangles;
|
||||||
|
GCHandle m_pinnedVertexes;
|
||||||
|
GCHandle m_pinnedIndex;
|
||||||
|
IntPtr m_verticesPtr = IntPtr.Zero;
|
||||||
|
int m_vertexCount = 0;
|
||||||
|
IntPtr m_indicesPtr = IntPtr.Zero;
|
||||||
|
int m_indexCount = 0;
|
||||||
|
public float[] m_normals;
|
||||||
|
Vector3 _centroid;
|
||||||
|
int _centroidDiv;
|
||||||
|
|
||||||
|
private class vertexcomp : IEqualityComparer<Vertex>
|
||||||
|
{
|
||||||
|
public bool Equals(Vertex v1, Vertex v2)
|
||||||
|
{
|
||||||
|
if (v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public int GetHashCode(Vertex v)
|
||||||
|
{
|
||||||
|
int a = v.X.GetHashCode();
|
||||||
|
int b = v.Y.GetHashCode();
|
||||||
|
int c = v.Z.GetHashCode();
|
||||||
|
return (a << 16) ^ (b << 8) ^ c;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mesh()
|
||||||
|
{
|
||||||
|
vertexcomp vcomp = new vertexcomp();
|
||||||
|
|
||||||
|
m_vertices = new Dictionary<Vertex, int>(vcomp);
|
||||||
|
m_triangles = new List<Triangle>();
|
||||||
|
_centroid = Vector3.Zero;
|
||||||
|
_centroidDiv = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mesh Clone()
|
||||||
|
{
|
||||||
|
Mesh result = new Mesh();
|
||||||
|
|
||||||
|
foreach (Triangle t in m_triangles)
|
||||||
|
{
|
||||||
|
result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone()));
|
||||||
|
}
|
||||||
|
result._centroid = _centroid;
|
||||||
|
result._centroidDiv = _centroidDiv;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(Triangle triangle)
|
||||||
|
{
|
||||||
|
if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero)
|
||||||
|
throw new NotSupportedException("Attempt to Add to a pinned Mesh");
|
||||||
|
// If a vertex of the triangle is not yet in the vertices list,
|
||||||
|
// add it and set its index to the current index count
|
||||||
|
// vertex == seems broken
|
||||||
|
// skip colapsed triangles
|
||||||
|
if ((triangle.v1.X == triangle.v2.X && triangle.v1.Y == triangle.v2.Y && triangle.v1.Z == triangle.v2.Z)
|
||||||
|
|| (triangle.v1.X == triangle.v3.X && triangle.v1.Y == triangle.v3.Y && triangle.v1.Z == triangle.v3.Z)
|
||||||
|
|| (triangle.v2.X == triangle.v3.X && triangle.v2.Y == triangle.v3.Y && triangle.v2.Z == triangle.v3.Z)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_vertices.Count == 0)
|
||||||
|
{
|
||||||
|
_centroidDiv = 0;
|
||||||
|
_centroid = Vector3.Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_vertices.ContainsKey(triangle.v1))
|
||||||
|
{
|
||||||
|
m_vertices[triangle.v1] = m_vertices.Count;
|
||||||
|
_centroid.X += triangle.v1.X;
|
||||||
|
_centroid.Y += triangle.v1.Y;
|
||||||
|
_centroid.Z += triangle.v1.Z;
|
||||||
|
_centroidDiv++;
|
||||||
|
}
|
||||||
|
if (!m_vertices.ContainsKey(triangle.v2))
|
||||||
|
{
|
||||||
|
m_vertices[triangle.v2] = m_vertices.Count;
|
||||||
|
_centroid.X += triangle.v2.X;
|
||||||
|
_centroid.Y += triangle.v2.Y;
|
||||||
|
_centroid.Z += triangle.v2.Z;
|
||||||
|
_centroidDiv++;
|
||||||
|
}
|
||||||
|
if (!m_vertices.ContainsKey(triangle.v3))
|
||||||
|
{
|
||||||
|
m_vertices[triangle.v3] = m_vertices.Count;
|
||||||
|
_centroid.X += triangle.v3.X;
|
||||||
|
_centroid.Y += triangle.v3.Y;
|
||||||
|
_centroid.Z += triangle.v3.Z;
|
||||||
|
_centroidDiv++;
|
||||||
|
}
|
||||||
|
m_triangles.Add(triangle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector3 GetCentroid()
|
||||||
|
{
|
||||||
|
if (_centroidDiv > 0)
|
||||||
|
return new Vector3(_centroid.X / _centroidDiv, _centroid.Y / _centroidDiv, _centroid.Z / _centroidDiv);
|
||||||
|
else
|
||||||
|
return Vector3.Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CalcNormals()
|
||||||
|
{
|
||||||
|
int iTriangles = m_triangles.Count;
|
||||||
|
|
||||||
|
this.m_normals = new float[iTriangles * 3];
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
foreach (Triangle t in m_triangles)
|
||||||
|
{
|
||||||
|
float ux, uy, uz;
|
||||||
|
float vx, vy, vz;
|
||||||
|
float wx, wy, wz;
|
||||||
|
|
||||||
|
ux = t.v1.X;
|
||||||
|
uy = t.v1.Y;
|
||||||
|
uz = t.v1.Z;
|
||||||
|
|
||||||
|
vx = t.v2.X;
|
||||||
|
vy = t.v2.Y;
|
||||||
|
vz = t.v2.Z;
|
||||||
|
|
||||||
|
wx = t.v3.X;
|
||||||
|
wy = t.v3.Y;
|
||||||
|
wz = t.v3.Z;
|
||||||
|
|
||||||
|
|
||||||
|
// Vectors for edges
|
||||||
|
float e1x, e1y, e1z;
|
||||||
|
float e2x, e2y, e2z;
|
||||||
|
|
||||||
|
e1x = ux - vx;
|
||||||
|
e1y = uy - vy;
|
||||||
|
e1z = uz - vz;
|
||||||
|
|
||||||
|
e2x = ux - wx;
|
||||||
|
e2y = uy - wy;
|
||||||
|
e2z = uz - wz;
|
||||||
|
|
||||||
|
|
||||||
|
// Cross product for normal
|
||||||
|
float nx, ny, nz;
|
||||||
|
nx = e1y * e2z - e1z * e2y;
|
||||||
|
ny = e1z * e2x - e1x * e2z;
|
||||||
|
nz = e1x * e2y - e1y * e2x;
|
||||||
|
|
||||||
|
// Length
|
||||||
|
float l = (float)Math.Sqrt(nx * nx + ny * ny + nz * nz);
|
||||||
|
float lReciprocal = 1.0f / l;
|
||||||
|
|
||||||
|
// Normalized "normal"
|
||||||
|
//nx /= l;
|
||||||
|
//ny /= l;
|
||||||
|
//nz /= l;
|
||||||
|
|
||||||
|
m_normals[i] = nx * lReciprocal;
|
||||||
|
m_normals[i + 1] = ny * lReciprocal;
|
||||||
|
m_normals[i + 2] = nz * lReciprocal;
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Vector3> getVertexList()
|
||||||
|
{
|
||||||
|
List<Vector3> result = new List<Vector3>();
|
||||||
|
foreach (Vertex v in m_vertices.Keys)
|
||||||
|
{
|
||||||
|
result.Add(new Vector3(v.X, v.Y, v.Z));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float[] getVertexListAsFloat()
|
||||||
|
{
|
||||||
|
if (m_vertices == null)
|
||||||
|
throw new NotSupportedException();
|
||||||
|
float[] result = new float[m_vertices.Count * 3];
|
||||||
|
foreach (KeyValuePair<Vertex, int> kvp in m_vertices)
|
||||||
|
{
|
||||||
|
Vertex v = kvp.Key;
|
||||||
|
int i = kvp.Value;
|
||||||
|
result[3 * i + 0] = v.X;
|
||||||
|
result[3 * i + 1] = v.Y;
|
||||||
|
result[3 * i + 2] = v.Z;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] getVertexListAsFloatLocked()
|
||||||
|
{
|
||||||
|
if (m_pinnedVertexes.IsAllocated)
|
||||||
|
return (float[])(m_pinnedVertexes.Target);
|
||||||
|
|
||||||
|
float[] result = getVertexListAsFloat();
|
||||||
|
m_pinnedVertexes = GCHandle.Alloc(result, GCHandleType.Pinned);
|
||||||
|
// Inform the garbage collector of this unmanaged allocation so it can schedule
|
||||||
|
// the next GC round more intelligently
|
||||||
|
GC.AddMemoryPressure(Buffer.ByteLength(result));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getVertexListAsPtrToFloatArray(out IntPtr vertices, out int vertexStride, out int vertexCount)
|
||||||
|
{
|
||||||
|
// A vertex is 3 floats
|
||||||
|
vertexStride = 3 * sizeof(float);
|
||||||
|
|
||||||
|
// If there isn't an unmanaged array allocated yet, do it now
|
||||||
|
if (m_verticesPtr == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
float[] vertexList = getVertexListAsFloat();
|
||||||
|
// Each vertex is 3 elements (floats)
|
||||||
|
m_vertexCount = vertexList.Length / 3;
|
||||||
|
int byteCount = m_vertexCount * vertexStride;
|
||||||
|
m_verticesPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(byteCount);
|
||||||
|
System.Runtime.InteropServices.Marshal.Copy(vertexList, 0, m_verticesPtr, m_vertexCount * 3);
|
||||||
|
}
|
||||||
|
vertices = m_verticesPtr;
|
||||||
|
vertexCount = m_vertexCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getIndexListAsInt()
|
||||||
|
{
|
||||||
|
if (m_triangles == null)
|
||||||
|
throw new NotSupportedException();
|
||||||
|
int[] result = new int[m_triangles.Count * 3];
|
||||||
|
for (int i = 0; i < m_triangles.Count; i++)
|
||||||
|
{
|
||||||
|
Triangle t = m_triangles[i];
|
||||||
|
result[3 * i + 0] = m_vertices[t.v1];
|
||||||
|
result[3 * i + 1] = m_vertices[t.v2];
|
||||||
|
result[3 * i + 2] = m_vertices[t.v3];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// creates a list of index values that defines triangle faces. THIS METHOD FREES ALL NON-PINNED MESH DATA
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public int[] getIndexListAsIntLocked()
|
||||||
|
{
|
||||||
|
if (m_pinnedIndex.IsAllocated)
|
||||||
|
return (int[])(m_pinnedIndex.Target);
|
||||||
|
|
||||||
|
int[] result = getIndexListAsInt();
|
||||||
|
m_pinnedIndex = GCHandle.Alloc(result, GCHandleType.Pinned);
|
||||||
|
// Inform the garbage collector of this unmanaged allocation so it can schedule
|
||||||
|
// the next GC round more intelligently
|
||||||
|
GC.AddMemoryPressure(Buffer.ByteLength(result));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getIndexListAsPtrToIntArray(out IntPtr indices, out int triStride, out int indexCount)
|
||||||
|
{
|
||||||
|
// If there isn't an unmanaged array allocated yet, do it now
|
||||||
|
if (m_indicesPtr == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
int[] indexList = getIndexListAsInt();
|
||||||
|
m_indexCount = indexList.Length;
|
||||||
|
int byteCount = m_indexCount * sizeof(int);
|
||||||
|
m_indicesPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(byteCount);
|
||||||
|
System.Runtime.InteropServices.Marshal.Copy(indexList, 0, m_indicesPtr, m_indexCount);
|
||||||
|
}
|
||||||
|
// A triangle is 3 ints (indices)
|
||||||
|
triStride = 3 * sizeof(int);
|
||||||
|
indices = m_indicesPtr;
|
||||||
|
indexCount = m_indexCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void releasePinned()
|
||||||
|
{
|
||||||
|
if (m_pinnedVertexes.IsAllocated)
|
||||||
|
m_pinnedVertexes.Free();
|
||||||
|
if (m_pinnedIndex.IsAllocated)
|
||||||
|
m_pinnedIndex.Free();
|
||||||
|
if (m_verticesPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
System.Runtime.InteropServices.Marshal.FreeHGlobal(m_verticesPtr);
|
||||||
|
m_verticesPtr = IntPtr.Zero;
|
||||||
|
}
|
||||||
|
if (m_indicesPtr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
System.Runtime.InteropServices.Marshal.FreeHGlobal(m_indicesPtr);
|
||||||
|
m_indicesPtr = IntPtr.Zero;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// frees up the source mesh data to minimize memory - call this method after calling get*Locked() functions
|
||||||
|
/// </summary>
|
||||||
|
public void releaseSourceMeshData()
|
||||||
|
{
|
||||||
|
m_triangles = null;
|
||||||
|
m_vertices = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Append(IMesh newMesh)
|
||||||
|
{
|
||||||
|
if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero)
|
||||||
|
throw new NotSupportedException("Attempt to Append to a pinned Mesh");
|
||||||
|
|
||||||
|
if (!(newMesh is Mesh))
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (Triangle t in ((Mesh)newMesh).m_triangles)
|
||||||
|
Add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do a linear transformation of mesh.
|
||||||
|
public void TransformLinear(float[,] matrix, float[] offset)
|
||||||
|
{
|
||||||
|
if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero)
|
||||||
|
throw new NotSupportedException("Attempt to TransformLinear a pinned Mesh");
|
||||||
|
|
||||||
|
foreach (Vertex v in m_vertices.Keys)
|
||||||
|
{
|
||||||
|
if (v == null)
|
||||||
|
continue;
|
||||||
|
float x, y, z;
|
||||||
|
x = v.X*matrix[0, 0] + v.Y*matrix[1, 0] + v.Z*matrix[2, 0];
|
||||||
|
y = v.X*matrix[0, 1] + v.Y*matrix[1, 1] + v.Z*matrix[2, 1];
|
||||||
|
z = v.X*matrix[0, 2] + v.Y*matrix[1, 2] + v.Z*matrix[2, 2];
|
||||||
|
v.X = x + offset[0];
|
||||||
|
v.Y = y + offset[1];
|
||||||
|
v.Z = z + offset[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DumpRaw(String path, String name, String title)
|
||||||
|
{
|
||||||
|
if (path == null)
|
||||||
|
return;
|
||||||
|
String fileName = name + "_" + title + ".raw";
|
||||||
|
String completePath = System.IO.Path.Combine(path, fileName);
|
||||||
|
StreamWriter sw = new StreamWriter(completePath);
|
||||||
|
foreach (Triangle t in m_triangles)
|
||||||
|
{
|
||||||
|
String s = t.ToStringRaw();
|
||||||
|
sw.WriteLine(s);
|
||||||
|
}
|
||||||
|
sw.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void TrimExcess()
|
||||||
|
{
|
||||||
|
m_triangles.TrimExcess();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,762 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) Contributors, http://opensimulator.org/
|
||||||
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of the OpenSimulator Project nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
//#define SPAM
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using OpenSim.Framework;
|
||||||
|
using OpenSim.Region.Physics.Manager;
|
||||||
|
using OpenMetaverse;
|
||||||
|
using OpenMetaverse.StructuredData;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
using System.IO.Compression;
|
||||||
|
using PrimMesher;
|
||||||
|
using log4net;
|
||||||
|
using Nini.Config;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.IO;
|
||||||
|
using ComponentAce.Compression.Libs.zlib;
|
||||||
|
|
||||||
|
namespace OpenSim.Region.Physics.Meshing
|
||||||
|
{
|
||||||
|
public class MeshmerizerPlugin : IMeshingPlugin
|
||||||
|
{
|
||||||
|
public MeshmerizerPlugin()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetName()
|
||||||
|
{
|
||||||
|
return "UbitMeshmerizer";
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMesher GetMesher(IConfigSource config)
|
||||||
|
{
|
||||||
|
return new Meshmerizer(config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Meshmerizer : IMesher
|
||||||
|
{
|
||||||
|
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
|
|
||||||
|
// Setting baseDir to a path will enable the dumping of raw files
|
||||||
|
// raw files can be imported by blender so a visual inspection of the results can be done
|
||||||
|
#if SPAM
|
||||||
|
const string baseDir = "rawFiles";
|
||||||
|
#else
|
||||||
|
private const string baseDir = null; //"rawFiles";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private bool cacheSculptMaps = true;
|
||||||
|
private bool cacheSculptAlphaMaps = true;
|
||||||
|
|
||||||
|
private string decodedSculptMapPath = null;
|
||||||
|
private bool useMeshiesPhysicsMesh = false;
|
||||||
|
|
||||||
|
private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh
|
||||||
|
|
||||||
|
private Dictionary<ulong, Mesh> m_uniqueMeshes = new Dictionary<ulong, Mesh>();
|
||||||
|
|
||||||
|
public Meshmerizer(IConfigSource config)
|
||||||
|
{
|
||||||
|
IConfig start_config = config.Configs["Startup"];
|
||||||
|
IConfig mesh_config = config.Configs["Mesh"];
|
||||||
|
|
||||||
|
decodedSculptMapPath = start_config.GetString("DecodedSculptMapPath","j2kDecodeCache");
|
||||||
|
|
||||||
|
cacheSculptMaps = start_config.GetBoolean("CacheSculptMaps", cacheSculptMaps);
|
||||||
|
|
||||||
|
if (Environment.OSVersion.Platform == PlatformID.Unix)
|
||||||
|
{
|
||||||
|
cacheSculptAlphaMaps = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cacheSculptAlphaMaps = cacheSculptMaps;
|
||||||
|
|
||||||
|
if(mesh_config != null)
|
||||||
|
useMeshiesPhysicsMesh = mesh_config.GetBoolean("UseMeshiesPhysicsMesh", useMeshiesPhysicsMesh);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(decodedSculptMapPath))
|
||||||
|
Directory.CreateDirectory(decodedSculptMapPath);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
m_log.WarnFormat("[SCULPT]: Unable to create {0} directory: ", decodedSculptMapPath, e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// creates a simple box mesh of the specified size. This mesh is of very low vertex count and may
|
||||||
|
/// be useful as a backup proxy when level of detail is not needed or when more complex meshes fail
|
||||||
|
/// for some reason
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="minX"></param>
|
||||||
|
/// <param name="maxX"></param>
|
||||||
|
/// <param name="minY"></param>
|
||||||
|
/// <param name="maxY"></param>
|
||||||
|
/// <param name="minZ"></param>
|
||||||
|
/// <param name="maxZ"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ)
|
||||||
|
{
|
||||||
|
Mesh box = new Mesh();
|
||||||
|
List<Vertex> vertices = new List<Vertex>();
|
||||||
|
// bottom
|
||||||
|
|
||||||
|
vertices.Add(new Vertex(minX, maxY, minZ));
|
||||||
|
vertices.Add(new Vertex(maxX, maxY, minZ));
|
||||||
|
vertices.Add(new Vertex(maxX, minY, minZ));
|
||||||
|
vertices.Add(new Vertex(minX, minY, minZ));
|
||||||
|
|
||||||
|
box.Add(new Triangle(vertices[0], vertices[1], vertices[2]));
|
||||||
|
box.Add(new Triangle(vertices[0], vertices[2], vertices[3]));
|
||||||
|
|
||||||
|
// top
|
||||||
|
|
||||||
|
vertices.Add(new Vertex(maxX, maxY, maxZ));
|
||||||
|
vertices.Add(new Vertex(minX, maxY, maxZ));
|
||||||
|
vertices.Add(new Vertex(minX, minY, maxZ));
|
||||||
|
vertices.Add(new Vertex(maxX, minY, maxZ));
|
||||||
|
|
||||||
|
box.Add(new Triangle(vertices[4], vertices[5], vertices[6]));
|
||||||
|
box.Add(new Triangle(vertices[4], vertices[6], vertices[7]));
|
||||||
|
|
||||||
|
// sides
|
||||||
|
|
||||||
|
box.Add(new Triangle(vertices[5], vertices[0], vertices[3]));
|
||||||
|
box.Add(new Triangle(vertices[5], vertices[3], vertices[6]));
|
||||||
|
|
||||||
|
box.Add(new Triangle(vertices[1], vertices[0], vertices[5]));
|
||||||
|
box.Add(new Triangle(vertices[1], vertices[5], vertices[4]));
|
||||||
|
|
||||||
|
box.Add(new Triangle(vertices[7], vertices[1], vertices[4]));
|
||||||
|
box.Add(new Triangle(vertices[7], vertices[2], vertices[1]));
|
||||||
|
|
||||||
|
box.Add(new Triangle(vertices[3], vertices[2], vertices[7]));
|
||||||
|
box.Add(new Triangle(vertices[3], vertices[7], vertices[6]));
|
||||||
|
|
||||||
|
return box;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a simple bounding box mesh for a complex input mesh
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="meshIn"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static Mesh CreateBoundingBoxMesh(Mesh meshIn)
|
||||||
|
{
|
||||||
|
float minX = float.MaxValue;
|
||||||
|
float maxX = float.MinValue;
|
||||||
|
float minY = float.MaxValue;
|
||||||
|
float maxY = float.MinValue;
|
||||||
|
float minZ = float.MaxValue;
|
||||||
|
float maxZ = float.MinValue;
|
||||||
|
|
||||||
|
foreach (Vector3 v in meshIn.getVertexList())
|
||||||
|
{
|
||||||
|
if (v.X < minX) minX = v.X;
|
||||||
|
if (v.Y < minY) minY = v.Y;
|
||||||
|
if (v.Z < minZ) minZ = v.Z;
|
||||||
|
|
||||||
|
if (v.X > maxX) maxX = v.X;
|
||||||
|
if (v.Y > maxY) maxY = v.Y;
|
||||||
|
if (v.Z > maxZ) maxZ = v.Z;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CreateSimpleBoxMesh(minX, maxX, minY, maxY, minZ, maxZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReportPrimError(string message, string primName, PrimMesh primMesh)
|
||||||
|
{
|
||||||
|
m_log.Error(message);
|
||||||
|
m_log.Error("\nPrim Name: " + primName);
|
||||||
|
m_log.Error("****** PrimMesh Parameters ******\n" + primMesh.ParamsToDisplayString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a submesh to an existing list of coords and faces.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="subMeshData"></param>
|
||||||
|
/// <param name="size">Size of entire object</param>
|
||||||
|
/// <param name="coords"></param>
|
||||||
|
/// <param name="faces"></param>
|
||||||
|
private void AddSubMesh(OSDMap subMeshData, Vector3 size, List<Coord> coords, List<Face> faces)
|
||||||
|
{
|
||||||
|
// Console.WriteLine("subMeshMap for {0} - {1}", primName, Util.GetFormattedXml((OSD)subMeshMap));
|
||||||
|
|
||||||
|
// As per http://wiki.secondlife.com/wiki/Mesh/Mesh_Asset_Format, some Mesh Level
|
||||||
|
// of Detail Blocks (maps) contain just a NoGeometry key to signal there is no
|
||||||
|
// geometry for this submesh.
|
||||||
|
if (subMeshData.ContainsKey("NoGeometry") && ((OSDBoolean)subMeshData["NoGeometry"]))
|
||||||
|
return;
|
||||||
|
|
||||||
|
OpenMetaverse.Vector3 posMax = ((OSDMap)subMeshData["PositionDomain"])["Max"].AsVector3();
|
||||||
|
OpenMetaverse.Vector3 posMin = ((OSDMap)subMeshData["PositionDomain"])["Min"].AsVector3();
|
||||||
|
ushort faceIndexOffset = (ushort)coords.Count;
|
||||||
|
|
||||||
|
byte[] posBytes = subMeshData["Position"].AsBinary();
|
||||||
|
for (int i = 0; i < posBytes.Length; i += 6)
|
||||||
|
{
|
||||||
|
ushort uX = Utils.BytesToUInt16(posBytes, i);
|
||||||
|
ushort uY = Utils.BytesToUInt16(posBytes, i + 2);
|
||||||
|
ushort uZ = Utils.BytesToUInt16(posBytes, i + 4);
|
||||||
|
|
||||||
|
Coord c = new Coord(
|
||||||
|
Utils.UInt16ToFloat(uX, posMin.X, posMax.X) * size.X,
|
||||||
|
Utils.UInt16ToFloat(uY, posMin.Y, posMax.Y) * size.Y,
|
||||||
|
Utils.UInt16ToFloat(uZ, posMin.Z, posMax.Z) * size.Z);
|
||||||
|
|
||||||
|
coords.Add(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] triangleBytes = subMeshData["TriangleList"].AsBinary();
|
||||||
|
for (int i = 0; i < triangleBytes.Length; i += 6)
|
||||||
|
{
|
||||||
|
ushort v1 = (ushort)(Utils.BytesToUInt16(triangleBytes, i) + faceIndexOffset);
|
||||||
|
ushort v2 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 2) + faceIndexOffset);
|
||||||
|
ushort v3 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 4) + faceIndexOffset);
|
||||||
|
Face f = new Face(v1, v2, v3);
|
||||||
|
faces.Add(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a physics mesh from data that comes with the prim. The actual data used depends on the prim type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="primName"></param>
|
||||||
|
/// <param name="primShape"></param>
|
||||||
|
/// <param name="size"></param>
|
||||||
|
/// <param name="lod"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, Vector3 size, float lod)
|
||||||
|
{
|
||||||
|
// m_log.DebugFormat(
|
||||||
|
// "[MESH]: Creating physics proxy for {0}, shape {1}",
|
||||||
|
// primName, (OpenMetaverse.SculptType)primShape.SculptType);
|
||||||
|
|
||||||
|
List<Coord> coords;
|
||||||
|
List<Face> faces;
|
||||||
|
|
||||||
|
if (primShape.SculptEntry)
|
||||||
|
{
|
||||||
|
if (((OpenMetaverse.SculptType)primShape.SculptType) == SculptType.Mesh)
|
||||||
|
{
|
||||||
|
if (!useMeshiesPhysicsMesh)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (!GenerateCoordsAndFacesFromPrimMeshData(primName, primShape, size, out coords, out faces))
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!GenerateCoordsAndFacesFromPrimSculptData(primName, primShape, size, lod, out coords, out faces))
|
||||||
|
return null;
|
||||||
|
// Remove the reference to any JPEG2000 sculpt data so it can be GCed
|
||||||
|
// don't loose it
|
||||||
|
// primShape.SculptData = Utils.EmptyBytes;
|
||||||
|
}
|
||||||
|
// primShape.SculptDataLoaded = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!GenerateCoordsAndFacesFromPrimShapeData(primName, primShape, size, lod, out coords, out faces))
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// keep compatible
|
||||||
|
primShape.SculptData = Utils.EmptyBytes;
|
||||||
|
|
||||||
|
int numCoords = coords.Count;
|
||||||
|
int numFaces = faces.Count;
|
||||||
|
|
||||||
|
// Create the list of vertices
|
||||||
|
List<Vertex> vertices = new List<Vertex>();
|
||||||
|
for (int i = 0; i < numCoords; i++)
|
||||||
|
{
|
||||||
|
Coord c = coords[i];
|
||||||
|
vertices.Add(new Vertex(c.X, c.Y, c.Z));
|
||||||
|
}
|
||||||
|
|
||||||
|
Mesh mesh = new Mesh();
|
||||||
|
// Add the corresponding triangles to the mesh
|
||||||
|
for (int i = 0; i < numFaces; i++)
|
||||||
|
{
|
||||||
|
Face f = faces[i];
|
||||||
|
mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return mesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate the co-ords and faces necessary to construct a mesh from the mesh data the accompanies a prim.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="primName"></param>
|
||||||
|
/// <param name="primShape"></param>
|
||||||
|
/// <param name="size"></param>
|
||||||
|
/// <param name="coords">Coords are added to this list by the method.</param>
|
||||||
|
/// <param name="faces">Faces are added to this list by the method.</param>
|
||||||
|
/// <returns>true if coords and faces were successfully generated, false if not</returns>
|
||||||
|
private bool GenerateCoordsAndFacesFromPrimMeshData(
|
||||||
|
string primName, PrimitiveBaseShape primShape, Vector3 size, out List<Coord> coords, out List<Face> faces)
|
||||||
|
{
|
||||||
|
// m_log.DebugFormat("[MESH]: experimental mesh proxy generation for {0}", primName);
|
||||||
|
|
||||||
|
coords = new List<Coord>();
|
||||||
|
faces = new List<Face>();
|
||||||
|
OSD meshOsd = null;
|
||||||
|
|
||||||
|
if (primShape.SculptData.Length <= 0)
|
||||||
|
{
|
||||||
|
m_log.ErrorFormat("[MESH]: asset data for {0} is zero length", primName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
long start = 0;
|
||||||
|
using (MemoryStream data = new MemoryStream(primShape.SculptData))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
OSD osd = OSDParser.DeserializeLLSDBinary(data);
|
||||||
|
if (osd is OSDMap)
|
||||||
|
meshOsd = (OSDMap)osd;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_log.Warn("[Mesh}: unable to cast mesh asset to OSDMap");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
m_log.Error("[MESH]: Exception deserializing mesh asset header:" + e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
start = data.Position;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meshOsd is OSDMap)
|
||||||
|
{
|
||||||
|
OSDMap physicsParms = null;
|
||||||
|
OSDMap map = (OSDMap)meshOsd;
|
||||||
|
if (map.ContainsKey("physics_shape"))
|
||||||
|
physicsParms = (OSDMap)map["physics_shape"]; // old asset format
|
||||||
|
else if (map.ContainsKey("physics_mesh"))
|
||||||
|
physicsParms = (OSDMap)map["physics_mesh"]; // new asset format
|
||||||
|
|
||||||
|
if (physicsParms == null)
|
||||||
|
{
|
||||||
|
m_log.Warn("[MESH]: no recognized physics mesh found in mesh asset");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int physOffset = physicsParms["offset"].AsInteger() + (int)start;
|
||||||
|
int physSize = physicsParms["size"].AsInteger();
|
||||||
|
|
||||||
|
if (physOffset < 0 || physSize == 0)
|
||||||
|
return false; // no mesh data in asset
|
||||||
|
|
||||||
|
OSD decodedMeshOsd = new OSD();
|
||||||
|
byte[] meshBytes = new byte[physSize];
|
||||||
|
System.Buffer.BlockCopy(primShape.SculptData, physOffset, meshBytes, 0, physSize);
|
||||||
|
// byte[] decompressed = new byte[physSize * 5];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (MemoryStream inMs = new MemoryStream(meshBytes))
|
||||||
|
{
|
||||||
|
using (MemoryStream outMs = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (ZOutputStream zOut = new ZOutputStream(outMs))
|
||||||
|
{
|
||||||
|
byte[] readBuffer = new byte[2048];
|
||||||
|
int readLen = 0;
|
||||||
|
while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
|
||||||
|
{
|
||||||
|
zOut.Write(readBuffer, 0, readLen);
|
||||||
|
}
|
||||||
|
zOut.Flush();
|
||||||
|
outMs.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
byte[] decompressedBuf = outMs.GetBuffer();
|
||||||
|
|
||||||
|
decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
m_log.Error("[MESH]: exception decoding physical mesh: " + e.ToString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
OSDArray decodedMeshOsdArray = null;
|
||||||
|
|
||||||
|
// physics_shape is an array of OSDMaps, one for each submesh
|
||||||
|
if (decodedMeshOsd is OSDArray)
|
||||||
|
{
|
||||||
|
// Console.WriteLine("decodedMeshOsd for {0} - {1}", primName, Util.GetFormattedXml(decodedMeshOsd));
|
||||||
|
|
||||||
|
decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
|
||||||
|
foreach (OSD subMeshOsd in decodedMeshOsdArray)
|
||||||
|
{
|
||||||
|
if (subMeshOsd is OSDMap)
|
||||||
|
AddSubMesh(subMeshOsd as OSDMap, size, coords, faces);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate the co-ords and faces necessary to construct a mesh from the sculpt data the accompanies a prim.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="primName"></param>
|
||||||
|
/// <param name="primShape"></param>
|
||||||
|
/// <param name="size"></param>
|
||||||
|
/// <param name="lod"></param>
|
||||||
|
/// <param name="coords">Coords are added to this list by the method.</param>
|
||||||
|
/// <param name="faces">Faces are added to this list by the method.</param>
|
||||||
|
/// <returns>true if coords and faces were successfully generated, false if not</returns>
|
||||||
|
private bool GenerateCoordsAndFacesFromPrimSculptData(
|
||||||
|
string primName, PrimitiveBaseShape primShape, Vector3 size, float lod, out List<Coord> coords, out List<Face> faces)
|
||||||
|
{
|
||||||
|
coords = new List<Coord>();
|
||||||
|
faces = new List<Face>();
|
||||||
|
PrimMesher.SculptMesh sculptMesh;
|
||||||
|
Image idata = null;
|
||||||
|
string decodedSculptFileName = "";
|
||||||
|
|
||||||
|
if (cacheSculptMaps && primShape.SculptTexture != UUID.Zero)
|
||||||
|
{
|
||||||
|
decodedSculptFileName = System.IO.Path.Combine(decodedSculptMapPath, "smap_" + primShape.SculptTexture.ToString());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (File.Exists(decodedSculptFileName))
|
||||||
|
{
|
||||||
|
idata = Image.FromFile(decodedSculptFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
m_log.Error("[SCULPT]: unable to load cached sculpt map " + decodedSculptFileName + " " + e.Message);
|
||||||
|
|
||||||
|
}
|
||||||
|
//if (idata != null)
|
||||||
|
// m_log.Debug("[SCULPT]: loaded cached map asset for map ID: " + primShape.SculptTexture.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idata == null)
|
||||||
|
{
|
||||||
|
if (primShape.SculptData == null || primShape.SculptData.Length == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
OpenMetaverse.Imaging.ManagedImage unusedData;
|
||||||
|
OpenMetaverse.Imaging.OpenJPEG.DecodeToImage(primShape.SculptData, out unusedData, out idata);
|
||||||
|
|
||||||
|
if (idata == null)
|
||||||
|
{
|
||||||
|
// In some cases it seems that the decode can return a null bitmap without throwing
|
||||||
|
// an exception
|
||||||
|
m_log.WarnFormat("[PHYSICS]: OpenJPEG decoded sculpt data for {0} to a null bitmap. Ignoring.", primName);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
unusedData = null;
|
||||||
|
|
||||||
|
//idata = CSJ2K.J2kImage.FromBytes(primShape.SculptData);
|
||||||
|
|
||||||
|
if (cacheSculptMaps && (cacheSculptAlphaMaps || (((ImageFlags)(idata.Flags) & ImageFlags.HasAlpha) ==0)))
|
||||||
|
// don't cache images with alpha channel in linux since mono can't load them correctly)
|
||||||
|
{
|
||||||
|
try { idata.Save(decodedSculptFileName, ImageFormat.MemoryBmp); }
|
||||||
|
catch (Exception e) { m_log.Error("[SCULPT]: unable to cache sculpt map " + decodedSculptFileName + " " + e.Message); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DllNotFoundException)
|
||||||
|
{
|
||||||
|
m_log.Error("[PHYSICS]: OpenJpeg is not installed correctly on this system. Physics Proxy generation failed. Often times this is because of an old version of GLIBC. You must have version 2.4 or above!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch (IndexOutOfRangeException)
|
||||||
|
{
|
||||||
|
m_log.Error("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
m_log.Error("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed: " + ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PrimMesher.SculptMesh.SculptType sculptType;
|
||||||
|
switch ((OpenMetaverse.SculptType)primShape.SculptType)
|
||||||
|
{
|
||||||
|
case OpenMetaverse.SculptType.Cylinder:
|
||||||
|
sculptType = PrimMesher.SculptMesh.SculptType.cylinder;
|
||||||
|
break;
|
||||||
|
case OpenMetaverse.SculptType.Plane:
|
||||||
|
sculptType = PrimMesher.SculptMesh.SculptType.plane;
|
||||||
|
break;
|
||||||
|
case OpenMetaverse.SculptType.Torus:
|
||||||
|
sculptType = PrimMesher.SculptMesh.SculptType.torus;
|
||||||
|
break;
|
||||||
|
case OpenMetaverse.SculptType.Sphere:
|
||||||
|
sculptType = PrimMesher.SculptMesh.SculptType.sphere;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sculptType = PrimMesher.SculptMesh.SculptType.plane;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mirror = ((primShape.SculptType & 128) != 0);
|
||||||
|
bool invert = ((primShape.SculptType & 64) != 0);
|
||||||
|
|
||||||
|
sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false, mirror, invert);
|
||||||
|
|
||||||
|
idata.Dispose();
|
||||||
|
|
||||||
|
sculptMesh.DumpRaw(baseDir, primName, "primMesh");
|
||||||
|
|
||||||
|
sculptMesh.Scale(size.X, size.Y, size.Z);
|
||||||
|
|
||||||
|
coords = sculptMesh.coords;
|
||||||
|
faces = sculptMesh.faces;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate the co-ords and faces necessary to construct a mesh from the shape data the accompanies a prim.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="primName"></param>
|
||||||
|
/// <param name="primShape"></param>
|
||||||
|
/// <param name="size"></param>
|
||||||
|
/// <param name="coords">Coords are added to this list by the method.</param>
|
||||||
|
/// <param name="faces">Faces are added to this list by the method.</param>
|
||||||
|
/// <returns>true if coords and faces were successfully generated, false if not</returns>
|
||||||
|
private bool GenerateCoordsAndFacesFromPrimShapeData(
|
||||||
|
string primName, PrimitiveBaseShape primShape, Vector3 size, float lod, out List<Coord> coords, out List<Face> faces)
|
||||||
|
{
|
||||||
|
PrimMesh primMesh;
|
||||||
|
coords = new List<Coord>();
|
||||||
|
faces = new List<Face>();
|
||||||
|
|
||||||
|
float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f;
|
||||||
|
float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f;
|
||||||
|
float pathBegin = (float)primShape.PathBegin * 2.0e-5f;
|
||||||
|
float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f;
|
||||||
|
float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f;
|
||||||
|
float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f;
|
||||||
|
|
||||||
|
float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f;
|
||||||
|
float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f;
|
||||||
|
float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f;
|
||||||
|
if (profileHollow > 0.95f)
|
||||||
|
profileHollow = 0.95f;
|
||||||
|
|
||||||
|
int sides = 4;
|
||||||
|
LevelOfDetail iLOD = (LevelOfDetail)lod;
|
||||||
|
if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
|
||||||
|
sides = 3;
|
||||||
|
else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
|
||||||
|
{
|
||||||
|
switch (iLOD)
|
||||||
|
{
|
||||||
|
case LevelOfDetail.High: sides = 24; break;
|
||||||
|
case LevelOfDetail.Medium: sides = 12; break;
|
||||||
|
case LevelOfDetail.Low: sides = 6; break;
|
||||||
|
case LevelOfDetail.VeryLow: sides = 3; break;
|
||||||
|
default: sides = 24; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
|
||||||
|
{ // half circle, prim is a sphere
|
||||||
|
switch (iLOD)
|
||||||
|
{
|
||||||
|
case LevelOfDetail.High: sides = 24; break;
|
||||||
|
case LevelOfDetail.Medium: sides = 12; break;
|
||||||
|
case LevelOfDetail.Low: sides = 6; break;
|
||||||
|
case LevelOfDetail.VeryLow: sides = 3; break;
|
||||||
|
default: sides = 24; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
profileBegin = 0.5f * profileBegin + 0.5f;
|
||||||
|
profileEnd = 0.5f * profileEnd + 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hollowSides = sides;
|
||||||
|
if (primShape.HollowShape == HollowShape.Circle)
|
||||||
|
{
|
||||||
|
switch (iLOD)
|
||||||
|
{
|
||||||
|
case LevelOfDetail.High: hollowSides = 24; break;
|
||||||
|
case LevelOfDetail.Medium: hollowSides = 12; break;
|
||||||
|
case LevelOfDetail.Low: hollowSides = 6; break;
|
||||||
|
case LevelOfDetail.VeryLow: hollowSides = 3; break;
|
||||||
|
default: hollowSides = 24; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (primShape.HollowShape == HollowShape.Square)
|
||||||
|
hollowSides = 4;
|
||||||
|
else if (primShape.HollowShape == HollowShape.Triangle)
|
||||||
|
hollowSides = 3;
|
||||||
|
|
||||||
|
primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides);
|
||||||
|
|
||||||
|
if (primMesh.errorMessage != null)
|
||||||
|
if (primMesh.errorMessage.Length > 0)
|
||||||
|
m_log.Error("[ERROR] " + primMesh.errorMessage);
|
||||||
|
|
||||||
|
primMesh.topShearX = pathShearX;
|
||||||
|
primMesh.topShearY = pathShearY;
|
||||||
|
primMesh.pathCutBegin = pathBegin;
|
||||||
|
primMesh.pathCutEnd = pathEnd;
|
||||||
|
|
||||||
|
if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible)
|
||||||
|
{
|
||||||
|
primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10;
|
||||||
|
primMesh.twistEnd = primShape.PathTwist * 18 / 10;
|
||||||
|
primMesh.taperX = pathScaleX;
|
||||||
|
primMesh.taperY = pathScaleY;
|
||||||
|
|
||||||
|
if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
|
||||||
|
{
|
||||||
|
ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
|
||||||
|
if (profileBegin < 0.0f) profileBegin = 0.0f;
|
||||||
|
if (profileEnd > 1.0f) profileEnd = 1.0f;
|
||||||
|
}
|
||||||
|
#if SPAM
|
||||||
|
m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString());
|
||||||
|
#endif
|
||||||
|
try
|
||||||
|
{
|
||||||
|
primMesh.ExtrudeLinear();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f;
|
||||||
|
primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f;
|
||||||
|
primMesh.radius = 0.01f * primShape.PathRadiusOffset;
|
||||||
|
primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions;
|
||||||
|
primMesh.skew = 0.01f * primShape.PathSkew;
|
||||||
|
primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10;
|
||||||
|
primMesh.twistEnd = primShape.PathTwist * 36 / 10;
|
||||||
|
primMesh.taperX = primShape.PathTaperX * 0.01f;
|
||||||
|
primMesh.taperY = primShape.PathTaperY * 0.01f;
|
||||||
|
|
||||||
|
if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
|
||||||
|
{
|
||||||
|
ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
|
||||||
|
if (profileBegin < 0.0f) profileBegin = 0.0f;
|
||||||
|
if (profileEnd > 1.0f) profileEnd = 1.0f;
|
||||||
|
}
|
||||||
|
#if SPAM
|
||||||
|
m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString());
|
||||||
|
#endif
|
||||||
|
try
|
||||||
|
{
|
||||||
|
primMesh.ExtrudeCircular();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
primMesh.DumpRaw(baseDir, primName, "primMesh");
|
||||||
|
|
||||||
|
primMesh.Scale(size.X, size.Y, size.Z);
|
||||||
|
|
||||||
|
coords = primMesh.coords;
|
||||||
|
faces = primMesh.faces;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod)
|
||||||
|
{
|
||||||
|
return CreateMesh(primName, primShape, size, lod, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical)
|
||||||
|
{
|
||||||
|
#if SPAM
|
||||||
|
m_log.DebugFormat("[MESH]: Creating mesh for {0}", primName);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Mesh mesh = null;
|
||||||
|
ulong key = 0;
|
||||||
|
|
||||||
|
// If this mesh has been created already, return it instead of creating another copy
|
||||||
|
// For large regions with 100k+ prims and hundreds of copies of each, this can save a GB or more of memory
|
||||||
|
key = primShape.GetMeshKey(size, lod);
|
||||||
|
if (m_uniqueMeshes.TryGetValue(key, out mesh))
|
||||||
|
return mesh;
|
||||||
|
|
||||||
|
if (size.X < 0.01f) size.X = 0.01f;
|
||||||
|
if (size.Y < 0.01f) size.Y = 0.01f;
|
||||||
|
if (size.Z < 0.01f) size.Z = 0.01f;
|
||||||
|
|
||||||
|
mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod);
|
||||||
|
|
||||||
|
if (mesh != null)
|
||||||
|
{
|
||||||
|
if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
|
||||||
|
{
|
||||||
|
#if SPAM
|
||||||
|
m_log.Debug("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
|
||||||
|
minSizeForComplexMesh.ToString() + " - creating simple bounding box");
|
||||||
|
#endif
|
||||||
|
mesh = CreateBoundingBoxMesh(mesh);
|
||||||
|
mesh.DumpRaw(baseDir, primName, "Z extruded");
|
||||||
|
}
|
||||||
|
|
||||||
|
// trim the vertex and triangle lists to free up memory
|
||||||
|
mesh.TrimExcess();
|
||||||
|
|
||||||
|
m_uniqueMeshes.Add(key, mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mesh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,197 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) Contributors
|
||||||
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of the OpenSimulator Project nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// to build without references to System.Drawing, comment this out
|
||||||
|
#define SYSTEM_DRAWING
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
#if SYSTEM_DRAWING
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
|
||||||
|
namespace PrimMesher
|
||||||
|
{
|
||||||
|
public class SculptMap
|
||||||
|
{
|
||||||
|
public int width;
|
||||||
|
public int height;
|
||||||
|
public byte[] redBytes;
|
||||||
|
public byte[] greenBytes;
|
||||||
|
public byte[] blueBytes;
|
||||||
|
|
||||||
|
public SculptMap()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public SculptMap(Bitmap bm, int lod)
|
||||||
|
{
|
||||||
|
int bmW = bm.Width;
|
||||||
|
int bmH = bm.Height;
|
||||||
|
|
||||||
|
if (bmW == 0 || bmH == 0)
|
||||||
|
throw new Exception("SculptMap: bitmap has no data");
|
||||||
|
|
||||||
|
int numLodPixels = lod * lod; // (32 * 2)^2 = 64^2 pixels for default sculpt map image
|
||||||
|
|
||||||
|
bool smallMap = bmW * bmH <= numLodPixels;
|
||||||
|
bool needsScaling = false;
|
||||||
|
|
||||||
|
width = bmW;
|
||||||
|
height = bmH;
|
||||||
|
while (width * height > numLodPixels * 4)
|
||||||
|
{
|
||||||
|
width >>= 1;
|
||||||
|
height >>= 1;
|
||||||
|
needsScaling = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (needsScaling)
|
||||||
|
bm = ScaleImage(bm, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new Exception("Exception in ScaleImage(): e: " + e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (width * height > numLodPixels)
|
||||||
|
{
|
||||||
|
width >>= 1;
|
||||||
|
height >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int numBytes = (width + 1) * (height + 1);
|
||||||
|
redBytes = new byte[numBytes];
|
||||||
|
greenBytes = new byte[numBytes];
|
||||||
|
blueBytes = new byte[numBytes];
|
||||||
|
|
||||||
|
int byteNdx = 0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for (int y = 0; y <= height; y++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x <= width; x++)
|
||||||
|
{
|
||||||
|
Color c;
|
||||||
|
|
||||||
|
if (smallMap)
|
||||||
|
c = bm.GetPixel(x < width ? x : x - 1,
|
||||||
|
y < height ? y : y - 1);
|
||||||
|
else
|
||||||
|
c = bm.GetPixel(x < width ? x * 2 : x * 2 - 1,
|
||||||
|
y < height ? y * 2 : y * 2 - 1);
|
||||||
|
|
||||||
|
redBytes[byteNdx] = c.R;
|
||||||
|
greenBytes[byteNdx] = c.G;
|
||||||
|
blueBytes[byteNdx] = c.B;
|
||||||
|
|
||||||
|
++byteNdx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
width++;
|
||||||
|
height++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<List<Coord>> ToRows(bool mirror)
|
||||||
|
{
|
||||||
|
int numRows = height;
|
||||||
|
int numCols = width;
|
||||||
|
|
||||||
|
List<List<Coord>> rows = new List<List<Coord>>(numRows);
|
||||||
|
|
||||||
|
float pixScale = 1.0f / 255;
|
||||||
|
|
||||||
|
int rowNdx, colNdx;
|
||||||
|
int smNdx = 0;
|
||||||
|
|
||||||
|
|
||||||
|
for (rowNdx = 0; rowNdx < numRows; rowNdx++)
|
||||||
|
{
|
||||||
|
List<Coord> row = new List<Coord>(numCols);
|
||||||
|
for (colNdx = 0; colNdx < numCols; colNdx++)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (mirror)
|
||||||
|
row.Add(new Coord(-((float)redBytes[smNdx] * pixScale - 0.5f), ((float)greenBytes[smNdx] * pixScale - 0.5f), (float)blueBytes[smNdx] * pixScale - 0.5f));
|
||||||
|
else
|
||||||
|
row.Add(new Coord((float)redBytes[smNdx] * pixScale - 0.5f, (float)greenBytes[smNdx] * pixScale - 0.5f, (float)blueBytes[smNdx] * pixScale - 0.5f));
|
||||||
|
|
||||||
|
++smNdx;
|
||||||
|
}
|
||||||
|
rows.Add(row);
|
||||||
|
}
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight)
|
||||||
|
{
|
||||||
|
|
||||||
|
Bitmap scaledImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
|
||||||
|
|
||||||
|
Color c;
|
||||||
|
float xscale = srcImage.Width / destWidth;
|
||||||
|
float yscale = srcImage.Height / destHeight;
|
||||||
|
|
||||||
|
float sy = 0.5f;
|
||||||
|
for (int y = 0; y < destHeight; y++)
|
||||||
|
{
|
||||||
|
float sx = 0.5f;
|
||||||
|
for (int x = 0; x < destWidth; x++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = srcImage.GetPixel((int)(sx), (int)(sy));
|
||||||
|
scaledImage.SetPixel(x, y, Color.FromArgb(c.R, c.G, c.B));
|
||||||
|
}
|
||||||
|
catch (IndexOutOfRangeException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
sx += xscale;
|
||||||
|
}
|
||||||
|
sy += yscale;
|
||||||
|
}
|
||||||
|
srcImage.Dispose();
|
||||||
|
return scaledImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
|
@ -0,0 +1,646 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) Contributors
|
||||||
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of the OpenSimulator Project nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// to build without references to System.Drawing, comment this out
|
||||||
|
#define SYSTEM_DRAWING
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
#if SYSTEM_DRAWING
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace PrimMesher
|
||||||
|
{
|
||||||
|
|
||||||
|
public class SculptMesh
|
||||||
|
{
|
||||||
|
public List<Coord> coords;
|
||||||
|
public List<Face> faces;
|
||||||
|
|
||||||
|
public List<ViewerFace> viewerFaces;
|
||||||
|
public List<Coord> normals;
|
||||||
|
public List<UVCoord> uvs;
|
||||||
|
|
||||||
|
public enum SculptType { sphere = 1, torus = 2, plane = 3, cylinder = 4 };
|
||||||
|
|
||||||
|
#if SYSTEM_DRAWING
|
||||||
|
|
||||||
|
public SculptMesh SculptMeshFromFile(string fileName, SculptType sculptType, int lod, bool viewerMode)
|
||||||
|
{
|
||||||
|
Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
|
||||||
|
SculptMesh sculptMesh = new SculptMesh(bitmap, sculptType, lod, viewerMode);
|
||||||
|
bitmap.Dispose();
|
||||||
|
return sculptMesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public SculptMesh(string fileName, int sculptType, int lod, int viewerMode, int mirror, int invert)
|
||||||
|
{
|
||||||
|
Bitmap bitmap = (Bitmap)Bitmap.FromFile(fileName);
|
||||||
|
_SculptMesh(bitmap, (SculptType)sculptType, lod, viewerMode != 0, mirror != 0, invert != 0);
|
||||||
|
bitmap.Dispose();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ** Experimental ** May disappear from future versions ** not recommeneded for use in applications
|
||||||
|
/// Construct a sculpt mesh from a 2D array of floats
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="zMap"></param>
|
||||||
|
/// <param name="xBegin"></param>
|
||||||
|
/// <param name="xEnd"></param>
|
||||||
|
/// <param name="yBegin"></param>
|
||||||
|
/// <param name="yEnd"></param>
|
||||||
|
/// <param name="viewerMode"></param>
|
||||||
|
public SculptMesh(float[,] zMap, float xBegin, float xEnd, float yBegin, float yEnd, bool viewerMode)
|
||||||
|
{
|
||||||
|
float xStep, yStep;
|
||||||
|
float uStep, vStep;
|
||||||
|
|
||||||
|
int numYElements = zMap.GetLength(0);
|
||||||
|
int numXElements = zMap.GetLength(1);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
xStep = (xEnd - xBegin) / (float)(numXElements - 1);
|
||||||
|
yStep = (yEnd - yBegin) / (float)(numYElements - 1);
|
||||||
|
|
||||||
|
uStep = 1.0f / (numXElements - 1);
|
||||||
|
vStep = 1.0f / (numYElements - 1);
|
||||||
|
}
|
||||||
|
catch (DivideByZeroException)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
coords = new List<Coord>();
|
||||||
|
faces = new List<Face>();
|
||||||
|
normals = new List<Coord>();
|
||||||
|
uvs = new List<UVCoord>();
|
||||||
|
|
||||||
|
viewerFaces = new List<ViewerFace>();
|
||||||
|
|
||||||
|
int p1, p2, p3, p4;
|
||||||
|
|
||||||
|
int x, y;
|
||||||
|
int xStart = 0, yStart = 0;
|
||||||
|
|
||||||
|
for (y = yStart; y < numYElements; y++)
|
||||||
|
{
|
||||||
|
int rowOffset = y * numXElements;
|
||||||
|
|
||||||
|
for (x = xStart; x < numXElements; x++)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* p1-----p2
|
||||||
|
* | \ f2 |
|
||||||
|
* | \ |
|
||||||
|
* | f1 \|
|
||||||
|
* p3-----p4
|
||||||
|
*/
|
||||||
|
|
||||||
|
p4 = rowOffset + x;
|
||||||
|
p3 = p4 - 1;
|
||||||
|
|
||||||
|
p2 = p4 - numXElements;
|
||||||
|
p1 = p3 - numXElements;
|
||||||
|
|
||||||
|
Coord c = new Coord(xBegin + x * xStep, yBegin + y * yStep, zMap[y, x]);
|
||||||
|
this.coords.Add(c);
|
||||||
|
if (viewerMode)
|
||||||
|
{
|
||||||
|
this.normals.Add(new Coord());
|
||||||
|
this.uvs.Add(new UVCoord(uStep * x, 1.0f - vStep * y));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y > 0 && x > 0)
|
||||||
|
{
|
||||||
|
Face f1, f2;
|
||||||
|
|
||||||
|
if (viewerMode)
|
||||||
|
{
|
||||||
|
f1 = new Face(p1, p4, p3, p1, p4, p3);
|
||||||
|
f1.uv1 = p1;
|
||||||
|
f1.uv2 = p4;
|
||||||
|
f1.uv3 = p3;
|
||||||
|
|
||||||
|
f2 = new Face(p1, p2, p4, p1, p2, p4);
|
||||||
|
f2.uv1 = p1;
|
||||||
|
f2.uv2 = p2;
|
||||||
|
f2.uv3 = p4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
f1 = new Face(p1, p4, p3);
|
||||||
|
f2 = new Face(p1, p2, p4);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.faces.Add(f1);
|
||||||
|
this.faces.Add(f2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (viewerMode)
|
||||||
|
calcVertexNormals(SculptType.plane, numXElements, numYElements);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SYSTEM_DRAWING
|
||||||
|
public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode)
|
||||||
|
{
|
||||||
|
_SculptMesh(sculptBitmap, sculptType, lod, viewerMode, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode, bool mirror, bool invert)
|
||||||
|
{
|
||||||
|
_SculptMesh(sculptBitmap, sculptType, lod, viewerMode, mirror, invert);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool viewerMode, bool mirror, bool invert)
|
||||||
|
{
|
||||||
|
_SculptMesh(rows, sculptType, viewerMode, mirror, invert);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SYSTEM_DRAWING
|
||||||
|
/// <summary>
|
||||||
|
/// converts a bitmap to a list of lists of coords, while scaling the image.
|
||||||
|
/// the scaling is done in floating point so as to allow for reduced vertex position
|
||||||
|
/// quantization as the position will be averaged between pixel values. this routine will
|
||||||
|
/// likely fail if the bitmap width and height are not powers of 2.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bitmap"></param>
|
||||||
|
/// <param name="scale"></param>
|
||||||
|
/// <param name="mirror"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private List<List<Coord>> bitmap2Coords(Bitmap bitmap, int scale, bool mirror)
|
||||||
|
{
|
||||||
|
int numRows = bitmap.Height / scale;
|
||||||
|
int numCols = bitmap.Width / scale;
|
||||||
|
List<List<Coord>> rows = new List<List<Coord>>(numRows);
|
||||||
|
|
||||||
|
float pixScale = 1.0f / (scale * scale);
|
||||||
|
pixScale /= 255;
|
||||||
|
|
||||||
|
int imageX, imageY = 0;
|
||||||
|
|
||||||
|
int rowNdx, colNdx;
|
||||||
|
|
||||||
|
for (rowNdx = 0; rowNdx < numRows; rowNdx++)
|
||||||
|
{
|
||||||
|
List<Coord> row = new List<Coord>(numCols);
|
||||||
|
for (colNdx = 0; colNdx < numCols; colNdx++)
|
||||||
|
{
|
||||||
|
imageX = colNdx * scale;
|
||||||
|
int imageYStart = rowNdx * scale;
|
||||||
|
int imageYEnd = imageYStart + scale;
|
||||||
|
int imageXEnd = imageX + scale;
|
||||||
|
float rSum = 0.0f;
|
||||||
|
float gSum = 0.0f;
|
||||||
|
float bSum = 0.0f;
|
||||||
|
for (; imageX < imageXEnd; imageX++)
|
||||||
|
{
|
||||||
|
for (imageY = imageYStart; imageY < imageYEnd; imageY++)
|
||||||
|
{
|
||||||
|
Color c = bitmap.GetPixel(imageX, imageY);
|
||||||
|
if (c.A != 255)
|
||||||
|
{
|
||||||
|
bitmap.SetPixel(imageX, imageY, Color.FromArgb(255, c.R, c.G, c.B));
|
||||||
|
c = bitmap.GetPixel(imageX, imageY);
|
||||||
|
}
|
||||||
|
rSum += c.R;
|
||||||
|
gSum += c.G;
|
||||||
|
bSum += c.B;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (mirror)
|
||||||
|
row.Add(new Coord(-(rSum * pixScale - 0.5f), gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
|
||||||
|
else
|
||||||
|
row.Add(new Coord(rSum * pixScale - 0.5f, gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
|
||||||
|
|
||||||
|
}
|
||||||
|
rows.Add(row);
|
||||||
|
}
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<List<Coord>> bitmap2CoordsSampled(Bitmap bitmap, int scale, bool mirror)
|
||||||
|
{
|
||||||
|
int numRows = bitmap.Height / scale;
|
||||||
|
int numCols = bitmap.Width / scale;
|
||||||
|
List<List<Coord>> rows = new List<List<Coord>>(numRows);
|
||||||
|
|
||||||
|
float pixScale = 1.0f / 256.0f;
|
||||||
|
|
||||||
|
int imageX, imageY = 0;
|
||||||
|
|
||||||
|
int rowNdx, colNdx;
|
||||||
|
|
||||||
|
for (rowNdx = 0; rowNdx <= numRows; rowNdx++)
|
||||||
|
{
|
||||||
|
List<Coord> row = new List<Coord>(numCols);
|
||||||
|
imageY = rowNdx * scale;
|
||||||
|
if (rowNdx == numRows) imageY--;
|
||||||
|
for (colNdx = 0; colNdx <= numCols; colNdx++)
|
||||||
|
{
|
||||||
|
imageX = colNdx * scale;
|
||||||
|
if (colNdx == numCols) imageX--;
|
||||||
|
|
||||||
|
Color c = bitmap.GetPixel(imageX, imageY);
|
||||||
|
if (c.A != 255)
|
||||||
|
{
|
||||||
|
bitmap.SetPixel(imageX, imageY, Color.FromArgb(255, c.R, c.G, c.B));
|
||||||
|
c = bitmap.GetPixel(imageX, imageY);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mirror)
|
||||||
|
row.Add(new Coord(-(c.R * pixScale - 0.5f), c.G * pixScale - 0.5f, c.B * pixScale - 0.5f));
|
||||||
|
else
|
||||||
|
row.Add(new Coord(c.R * pixScale - 0.5f, c.G * pixScale - 0.5f, c.B * pixScale - 0.5f));
|
||||||
|
|
||||||
|
}
|
||||||
|
rows.Add(row);
|
||||||
|
}
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void _SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool viewerMode, bool mirror, bool invert)
|
||||||
|
{
|
||||||
|
_SculptMesh(new SculptMap(sculptBitmap, lod).ToRows(mirror), sculptType, viewerMode, mirror, invert);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void _SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool viewerMode, bool mirror, bool invert)
|
||||||
|
{
|
||||||
|
coords = new List<Coord>();
|
||||||
|
faces = new List<Face>();
|
||||||
|
normals = new List<Coord>();
|
||||||
|
uvs = new List<UVCoord>();
|
||||||
|
|
||||||
|
sculptType = (SculptType)(((int)sculptType) & 0x07);
|
||||||
|
|
||||||
|
if (mirror)
|
||||||
|
invert = !invert;
|
||||||
|
|
||||||
|
viewerFaces = new List<ViewerFace>();
|
||||||
|
|
||||||
|
int width = rows[0].Count;
|
||||||
|
|
||||||
|
int p1, p2, p3, p4;
|
||||||
|
|
||||||
|
int imageX, imageY;
|
||||||
|
|
||||||
|
if (sculptType != SculptType.plane)
|
||||||
|
{
|
||||||
|
if (rows.Count % 2 == 0)
|
||||||
|
{
|
||||||
|
for (int rowNdx = 0; rowNdx < rows.Count; rowNdx++)
|
||||||
|
rows[rowNdx].Add(rows[rowNdx][0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int lastIndex = rows[0].Count - 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < rows.Count; i++)
|
||||||
|
rows[i][0] = rows[i][lastIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Coord topPole = rows[0][width / 2];
|
||||||
|
Coord bottomPole = rows[rows.Count - 1][width / 2];
|
||||||
|
|
||||||
|
if (sculptType == SculptType.sphere)
|
||||||
|
{
|
||||||
|
if (rows.Count % 2 == 0)
|
||||||
|
{
|
||||||
|
int count = rows[0].Count;
|
||||||
|
List<Coord> topPoleRow = new List<Coord>(count);
|
||||||
|
List<Coord> bottomPoleRow = new List<Coord>(count);
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
topPoleRow.Add(topPole);
|
||||||
|
bottomPoleRow.Add(bottomPole);
|
||||||
|
}
|
||||||
|
rows.Insert(0, topPoleRow);
|
||||||
|
rows.Add(bottomPoleRow);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int count = rows[0].Count;
|
||||||
|
|
||||||
|
List<Coord> topPoleRow = rows[0];
|
||||||
|
List<Coord> bottomPoleRow = rows[rows.Count - 1];
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
topPoleRow[i] = topPole;
|
||||||
|
bottomPoleRow[i] = bottomPole;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sculptType == SculptType.torus)
|
||||||
|
rows.Add(rows[0]);
|
||||||
|
|
||||||
|
int coordsDown = rows.Count;
|
||||||
|
int coordsAcross = rows[0].Count;
|
||||||
|
// int lastColumn = coordsAcross - 1;
|
||||||
|
|
||||||
|
float widthUnit = 1.0f / (coordsAcross - 1);
|
||||||
|
float heightUnit = 1.0f / (coordsDown - 1);
|
||||||
|
|
||||||
|
for (imageY = 0; imageY < coordsDown; imageY++)
|
||||||
|
{
|
||||||
|
int rowOffset = imageY * coordsAcross;
|
||||||
|
|
||||||
|
for (imageX = 0; imageX < coordsAcross; imageX++)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* p1-----p2
|
||||||
|
* | \ f2 |
|
||||||
|
* | \ |
|
||||||
|
* | f1 \|
|
||||||
|
* p3-----p4
|
||||||
|
*/
|
||||||
|
|
||||||
|
p4 = rowOffset + imageX;
|
||||||
|
p3 = p4 - 1;
|
||||||
|
|
||||||
|
p2 = p4 - coordsAcross;
|
||||||
|
p1 = p3 - coordsAcross;
|
||||||
|
|
||||||
|
this.coords.Add(rows[imageY][imageX]);
|
||||||
|
if (viewerMode)
|
||||||
|
{
|
||||||
|
this.normals.Add(new Coord());
|
||||||
|
this.uvs.Add(new UVCoord(widthUnit * imageX, heightUnit * imageY));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (imageY > 0 && imageX > 0)
|
||||||
|
{
|
||||||
|
Face f1, f2;
|
||||||
|
|
||||||
|
if (viewerMode)
|
||||||
|
{
|
||||||
|
if (invert)
|
||||||
|
{
|
||||||
|
f1 = new Face(p1, p4, p3, p1, p4, p3);
|
||||||
|
f1.uv1 = p1;
|
||||||
|
f1.uv2 = p4;
|
||||||
|
f1.uv3 = p3;
|
||||||
|
|
||||||
|
f2 = new Face(p1, p2, p4, p1, p2, p4);
|
||||||
|
f2.uv1 = p1;
|
||||||
|
f2.uv2 = p2;
|
||||||
|
f2.uv3 = p4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
f1 = new Face(p1, p3, p4, p1, p3, p4);
|
||||||
|
f1.uv1 = p1;
|
||||||
|
f1.uv2 = p3;
|
||||||
|
f1.uv3 = p4;
|
||||||
|
|
||||||
|
f2 = new Face(p1, p4, p2, p1, p4, p2);
|
||||||
|
f2.uv1 = p1;
|
||||||
|
f2.uv2 = p4;
|
||||||
|
f2.uv3 = p2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (invert)
|
||||||
|
{
|
||||||
|
f1 = new Face(p1, p4, p3);
|
||||||
|
f2 = new Face(p1, p2, p4);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
f1 = new Face(p1, p3, p4);
|
||||||
|
f2 = new Face(p1, p4, p2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.faces.Add(f1);
|
||||||
|
this.faces.Add(f2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (viewerMode)
|
||||||
|
calcVertexNormals(sculptType, coordsAcross, coordsDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Duplicates a SculptMesh object. All object properties are copied by value, including lists.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public SculptMesh Copy()
|
||||||
|
{
|
||||||
|
return new SculptMesh(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SculptMesh(SculptMesh sm)
|
||||||
|
{
|
||||||
|
coords = new List<Coord>(sm.coords);
|
||||||
|
faces = new List<Face>(sm.faces);
|
||||||
|
viewerFaces = new List<ViewerFace>(sm.viewerFaces);
|
||||||
|
normals = new List<Coord>(sm.normals);
|
||||||
|
uvs = new List<UVCoord>(sm.uvs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calcVertexNormals(SculptType sculptType, int xSize, int ySize)
|
||||||
|
{ // compute vertex normals by summing all the surface normals of all the triangles sharing
|
||||||
|
// each vertex and then normalizing
|
||||||
|
int numFaces = this.faces.Count;
|
||||||
|
for (int i = 0; i < numFaces; i++)
|
||||||
|
{
|
||||||
|
Face face = this.faces[i];
|
||||||
|
Coord surfaceNormal = face.SurfaceNormal(this.coords);
|
||||||
|
this.normals[face.n1] += surfaceNormal;
|
||||||
|
this.normals[face.n2] += surfaceNormal;
|
||||||
|
this.normals[face.n3] += surfaceNormal;
|
||||||
|
}
|
||||||
|
|
||||||
|
int numNormals = this.normals.Count;
|
||||||
|
for (int i = 0; i < numNormals; i++)
|
||||||
|
this.normals[i] = this.normals[i].Normalize();
|
||||||
|
|
||||||
|
if (sculptType != SculptType.plane)
|
||||||
|
{ // blend the vertex normals at the cylinder seam
|
||||||
|
for (int y = 0; y < ySize; y++)
|
||||||
|
{
|
||||||
|
int rowOffset = y * xSize;
|
||||||
|
|
||||||
|
this.normals[rowOffset] = this.normals[rowOffset + xSize - 1] = (this.normals[rowOffset] + this.normals[rowOffset + xSize - 1]).Normalize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Face face in this.faces)
|
||||||
|
{
|
||||||
|
ViewerFace vf = new ViewerFace(0);
|
||||||
|
vf.v1 = this.coords[face.v1];
|
||||||
|
vf.v2 = this.coords[face.v2];
|
||||||
|
vf.v3 = this.coords[face.v3];
|
||||||
|
|
||||||
|
vf.coordIndex1 = face.v1;
|
||||||
|
vf.coordIndex2 = face.v2;
|
||||||
|
vf.coordIndex3 = face.v3;
|
||||||
|
|
||||||
|
vf.n1 = this.normals[face.n1];
|
||||||
|
vf.n2 = this.normals[face.n2];
|
||||||
|
vf.n3 = this.normals[face.n3];
|
||||||
|
|
||||||
|
vf.uv1 = this.uvs[face.uv1];
|
||||||
|
vf.uv2 = this.uvs[face.uv2];
|
||||||
|
vf.uv3 = this.uvs[face.uv3];
|
||||||
|
|
||||||
|
this.viewerFaces.Add(vf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a value to each XYZ vertex coordinate in the mesh
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x"></param>
|
||||||
|
/// <param name="y"></param>
|
||||||
|
/// <param name="z"></param>
|
||||||
|
public void AddPos(float x, float y, float z)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int numVerts = this.coords.Count;
|
||||||
|
Coord vert;
|
||||||
|
|
||||||
|
for (i = 0; i < numVerts; i++)
|
||||||
|
{
|
||||||
|
vert = this.coords[i];
|
||||||
|
vert.X += x;
|
||||||
|
vert.Y += y;
|
||||||
|
vert.Z += z;
|
||||||
|
this.coords[i] = vert;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.viewerFaces != null)
|
||||||
|
{
|
||||||
|
int numViewerFaces = this.viewerFaces.Count;
|
||||||
|
|
||||||
|
for (i = 0; i < numViewerFaces; i++)
|
||||||
|
{
|
||||||
|
ViewerFace v = this.viewerFaces[i];
|
||||||
|
v.AddPos(x, y, z);
|
||||||
|
this.viewerFaces[i] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Rotates the mesh
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="q"></param>
|
||||||
|
public void AddRot(Quat q)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int numVerts = this.coords.Count;
|
||||||
|
|
||||||
|
for (i = 0; i < numVerts; i++)
|
||||||
|
this.coords[i] *= q;
|
||||||
|
|
||||||
|
int numNormals = this.normals.Count;
|
||||||
|
for (i = 0; i < numNormals; i++)
|
||||||
|
this.normals[i] *= q;
|
||||||
|
|
||||||
|
if (this.viewerFaces != null)
|
||||||
|
{
|
||||||
|
int numViewerFaces = this.viewerFaces.Count;
|
||||||
|
|
||||||
|
for (i = 0; i < numViewerFaces; i++)
|
||||||
|
{
|
||||||
|
ViewerFace v = this.viewerFaces[i];
|
||||||
|
v.v1 *= q;
|
||||||
|
v.v2 *= q;
|
||||||
|
v.v3 *= q;
|
||||||
|
|
||||||
|
v.n1 *= q;
|
||||||
|
v.n2 *= q;
|
||||||
|
v.n3 *= q;
|
||||||
|
|
||||||
|
this.viewerFaces[i] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Scale(float x, float y, float z)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int numVerts = this.coords.Count;
|
||||||
|
|
||||||
|
Coord m = new Coord(x, y, z);
|
||||||
|
for (i = 0; i < numVerts; i++)
|
||||||
|
this.coords[i] *= m;
|
||||||
|
|
||||||
|
if (this.viewerFaces != null)
|
||||||
|
{
|
||||||
|
int numViewerFaces = this.viewerFaces.Count;
|
||||||
|
for (i = 0; i < numViewerFaces; i++)
|
||||||
|
{
|
||||||
|
ViewerFace v = this.viewerFaces[i];
|
||||||
|
v.v1 *= m;
|
||||||
|
v.v2 *= m;
|
||||||
|
v.v3 *= m;
|
||||||
|
this.viewerFaces[i] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DumpRaw(String path, String name, String title)
|
||||||
|
{
|
||||||
|
if (path == null)
|
||||||
|
return;
|
||||||
|
String fileName = name + "_" + title + ".raw";
|
||||||
|
String completePath = System.IO.Path.Combine(path, fileName);
|
||||||
|
StreamWriter sw = new StreamWriter(completePath);
|
||||||
|
|
||||||
|
for (int i = 0; i < this.faces.Count; i++)
|
||||||
|
{
|
||||||
|
string s = this.coords[this.faces[i].v1].ToString();
|
||||||
|
s += " " + this.coords[this.faces[i].v2].ToString();
|
||||||
|
s += " " + this.coords[this.faces[i].v3].ToString();
|
||||||
|
|
||||||
|
sw.WriteLine(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
sw.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
prebuild.xml
31
prebuild.xml
|
@ -701,6 +701,37 @@
|
||||||
</Files>
|
</Files>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
||||||
|
<Project frameworkVersion="v3_5" name="OpenSim.Region.Physics.UbitMeshing" path="OpenSim/Region/Physics/UbitMeshing" type="Library">
|
||||||
|
<Configuration name="Debug">
|
||||||
|
<Options>
|
||||||
|
<OutputPath>../../../../bin/Physics/</OutputPath>
|
||||||
|
</Options>
|
||||||
|
</Configuration>
|
||||||
|
<Configuration name="Release">
|
||||||
|
<Options>
|
||||||
|
<OutputPath>../../../../bin/Physics/</OutputPath>
|
||||||
|
</Options>
|
||||||
|
</Configuration>
|
||||||
|
|
||||||
|
<ReferencePath>../../../../bin/</ReferencePath>
|
||||||
|
<Reference name="System"/>
|
||||||
|
<Reference name="System.Drawing"/>
|
||||||
|
<Reference name="CSJ2K" path="../../../../bin/"/>
|
||||||
|
<Reference name="OpenMetaverseTypes" path="../../../../bin/"/>
|
||||||
|
<Reference name="OpenMetaverse" path="../../../../bin/"/>
|
||||||
|
<Reference name="OpenMetaverse.StructuredData" path="../../../../bin/"/>
|
||||||
|
<Reference name="Nini" path="../../../../bin/"/>
|
||||||
|
<Reference name="OpenSim.Framework"/>
|
||||||
|
<Reference name="OpenSim.Framework.Console"/>
|
||||||
|
<Reference name="OpenSim.Region.Physics.Manager"/>
|
||||||
|
<Reference name="log4net" path="../../../../bin/"/>
|
||||||
|
<Reference name="zlib.net" path="../../../../bin/"/>
|
||||||
|
|
||||||
|
<Files>
|
||||||
|
<Match pattern="*.cs" recurse="true"/>
|
||||||
|
</Files>
|
||||||
|
</Project>
|
||||||
|
|
||||||
<Project frameworkVersion="v3_5" name="OpenSim.Capabilities" path="OpenSim/Capabilities" type="Library">
|
<Project frameworkVersion="v3_5" name="OpenSim.Capabilities" path="OpenSim/Capabilities" type="Library">
|
||||||
<Configuration name="Debug">
|
<Configuration name="Debug">
|
||||||
<Options>
|
<Options>
|
||||||
|
|
Loading…
Reference in New Issue