void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (coeffWidth > 2) { coeffWidth = 0.1; } DataGenerator gen = new DataGenerator(); coeffWidth += 0.1; gen.MouthWidthcoeff = coeffWidth; this.Dispatcher.BeginInvoke(new Action(() => { Surface3D curve3D = new Surface3D(); newCurveModel = curve3D.CreateSurfaceModel(gen.GenerateData(Convert.ToInt32(this.textBox1.Text))); this.viewPort.Children.Remove(curveModel); this.viewPort.Children.Remove(wireframe); this.viewPort.Children.Add(newCurveModel); curveModel = newCurveModel; })); }
/// <summary> /// /// </summary> /// <param name="ps"></param> /// <param name="resource"></param> /// <param name="rotate">是否旋转模型x轴90度</param> /// <returns></returns> private static Mesh AssembleMesh(ParticleSystem ps, Dictionary <int, object> resource, bool rotate = false) { Mesh mesh = null; if (ps.SurfId != 0) { //Surface3D surf = resource[(int)ps.SurfId] as Surface3D; //obj = new GameObject("ps.Name"); //obj.AddComponent<MeshFilter>(); //obj.AddComponent<MeshRenderer>(); //obj.GetComponent<MeshRenderer>().sharedMaterial = mtl; ps.UnityResourceParam.MeshPath = SceneFileCopy.GetRelativeMeshDir(ps.RootFileName) + ps.Name + ".prefab"; Surface3D surf = resource[(int)ps.SurfId] as Surface3D; mesh = new Mesh(); mesh.name = ps.Name; int verticesCountInMesh = surf.VertexVector.Count / surf.SizePerVertex; Vector3[] xyz = new Vector3[surf.NumTriangles * 3]; Vector2[] uv1 = new Vector2[surf.NumTriangles * 3]; for (int i = 0; i < verticesCountInMesh; ++i) { //旋转方向 xyz[i] = new Vector3( surf.VertexVector[i * surf.SizePerVertex + surf.Offset[Surface3D.POSITION] + 0], surf.VertexVector[i * surf.SizePerVertex + surf.Offset[Surface3D.POSITION] + 1], surf.VertexVector[i * surf.SizePerVertex + surf.Offset[Surface3D.POSITION] + 2] ); uv1[i] = new Vector2( surf.VertexVector[i * surf.SizePerVertex + surf.Offset[Surface3D.UV0] + 0], //unity的UV坐标和as3不一样 1 - surf.VertexVector[i * surf.SizePerVertex + surf.Offset[Surface3D.UV0] + 1] ); } int[] triangles = new int[surf.IndexVector.Count]; for (int i = 0; i < triangles.Length; ++i) { triangles[i] = (int)surf.IndexVector[i]; } mesh.vertices = xyz; mesh.uv = uv1; mesh.triangles = triangles; mesh.RecalculateNormals(); if (rotate) { PrimitiveHelper.RotateMeshVertices(mesh, new Vector3(0, 0, 0), new Vector3(90, 0, 0)); mesh.RecalculateBounds(); mesh.RecalculateNormals(); } UnityEditor.AssetDatabase.CreateAsset(mesh, ps.UnityResourceParam.MeshPath); UnityEditor.AssetDatabase.Refresh(); } return(mesh); }
void dispatcherTimer_Tick(object sender, EventArgs e) { if (coeffWidth > 2) { coeffWidth = 0.1; } this.viewPort.Children.Remove(curveModel); this.viewPort.Children.Remove(pntsModel); this.viewPort.Children.Remove(wireframe); DataGenerator gen = new DataGenerator(); coeffWidth += 0.1; gen.MouthWidthcoeff = coeffWidth; //if (!dicPoints.ContainsKey(gen.MouthWidthcoeff)) // dicPoints.Add(gen.MouthWidthcoeff, gen.GenerateData(Convert.ToDouble(this.textBox1.Text))); Surface3D curve3D = new Surface3D(); Data = gen.GenerateData(Convert.ToInt32(this.textBox1.Text)); AutoAdjustCoordinate(); curveModel = curve3D.CreateSurfaceModel(Data); pntsModel = curve3D.CreateSurfacePoints(Data); if (!showPoints) { this.viewPort.Children.Add(curveModel); wireframe.MakeWireframe(curveModel.Content); this.viewPort.Children.Add(wireframe); wireframe.OnRender(null, null); } if (showPoints) { this.viewPort.Children.Add(pntsModel); } this.textBlock1.Text = this.watch.ElapsedMilliseconds.ToString(); this.watch.Reset(); this.watch.Start(); }
private void ReadPSSurface(ReadChunk chunk) { Surface3D surf = new Surface3D(); int bufId = chunk.Bytes.ReadInt16(); int idx = chunk.Bytes.ReadInt16(); surf.VertexVector = resource[bufId] as List <float>; surf.IndexVector = resource[idx] as List <uint>; surf.NumTriangles = chunk.Bytes.ReadInt32(); surf.FirstIndex = chunk.Bytes.ReadInt32(); surf.SizePerVertex = chunk.Bytes.ReadSByte(); for (int i = 0; i < surf.Offset.Count; i++) { surf.Offset[i] = chunk.Bytes.ReadSByte(); if (surf.Offset[i] != -1) { switch (surf.Offset[i]) { case Surface3D.POSITION: case Surface3D.NORMAL: case Surface3D.TANGENT: case Surface3D.BITANGENT: case Surface3D.COLOR0: case Surface3D.COLOR1: case Surface3D.COLOR2: surf.Format[i] = "float3"; break; case Surface3D.UV0: case Surface3D.UV1: case Surface3D.SKIN_WEIGHTS: case Surface3D.SKIN_INDICES: surf.Format[i] = "float2"; break; default: break; } } } resource[chunk.Id] = surf; }
private void Window_Loaded(object sender, RoutedEventArgs e) { try { timer.Interval = 2000; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); //dispatcherTimer.Start(); Surface3D curve3D = new Surface3D(); NumericTicksProider numericProvider = new NumericTicksProider(6, 4, 0.2, 0.1); numericProvider.LabelStringFormat = "{0:0.00}"; var axis = new AxisGridBuilder(); axis.TicksProvider = numericProvider; axis.XRange = new Range <double>(0, 5); axis.YRange = new Range <double>(0, 5); axis.ZRange = new Range <double>(0, 5); axisVisual3D = axis.DrawAxisGrid(); this.viewPort.Children.Add(axisVisual3D); axisFrame = new AxisGridFrame(); if (showPoints) { this.viewPort.Children.Add(axisFrame); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// AIR does not yet show anything. why public ApplicationSprite() { // curityError: Error #2148: SWF file file:///X|/jsc.svn/examples/actionscript/synergy/Flare3DWaterShips/Flare3DWaterShips/bin/Debug/staging/Flare3DWaterShips.ApplicationSprite/web/Flare3DWaterShips.ApplicationSprite.swf cannot access local resource file:///X|/jsc.svn/examples/actionscript/synergy/Flare3DWaterShips/Flare3DWaterShips/bin/Debug/staging/Flare3DWaterShips.ApplicationSprite/web/Flare3DWaterShips.ApplicationSprite.swf/[[DYNAMIC]]/3. Only local-with-filesystem and trusted local SWF files may access local resources. //at flash.display::Loader/get content() //at flare.core::Texture3D/completeEvent()[Z:\projects\flare3d 2.5\src\flare\core\Texture3D.as:496] var scene = new Viewer3D(this, "", 0.2); scene.camera = new Camera3D(); scene.camera.setPosition(120, 40, -30); scene.camera.lookAt(0, 0, 0); #region mirrorCam var mirrorCam = new Camera3D(); var mirror = new Texture3D(new Point(512, 512)); mirror.mipMode = Texture3D.MIP_NONE; mirror.wrapMode = Texture3D.WRAP_CLAMP; mirror.upload(scene); #endregion #region oceanShader var Ocean = KnownEmbeddedResources.Default["assets/Flare3DWaterShips/ocean07.flsl.compiled"]; var oceanShader = new FLSLMaterial("oceanShader", Ocean.ToByteArrayAsset()); var oceanShader_params = oceanShader.@params as dynamic; // http://www.flare3d.com/docs/flare/core/Texture3D.html // http://wiki.flare3d.com/index.php?title=FLAR_Toolkit_integration oceanShader_params.skyMap.value = new Texture3D( KnownEmbeddedResources.Default[ "assets/Flare3DWaterShips/highlights.png" ].ToBitmapAsset(), false, Texture3D.FORMAT_CUBEMAP); oceanShader_params.normalMap.value = new Texture3D( KnownEmbeddedResources.Default["assets/Flare3DWaterShips/normalmap.jpg"].ToBitmapAsset()); oceanShader_params.mirrorMap.value = mirror; #endregion // http://www.flare3d.com/support/index.php?topic=142.0 var water = new Plane("water", 3000, 3000, oceanGridSize - 1, oceanShader, "+xz"); var skybox = new SkyBox( KnownEmbeddedResources.Default["assets/Flare3DWaterShips/skybox.png"].ToBitmapAsset(), SkyBox.HORIZONTAL_CROSS, null, 1); // http://www.flare3d.com/support/index.php?topic=63.0 // http://www.flare3d.com/docs/tutorials/loadFromBytes/ var ship0 = new Flare3DWaterShipComponent.ship(); // how to make it safe to provide 3rd party assetslibrary builder // code templates? which can be selected by KnownEmbeddedResources pattern match? // class XFileTemplate<T> where T like foo "*.zf3d" var ship1 = new Flare3DWaterShipComponent.ship(); ship1.x = 40; ship1.y = 10; var particles = new particles(); #region initWaves(): var bytes = new ByteArray(); bytes.endian = Endian.LITTLE_ENDIAN; bytes.length = oceanGridSize * oceanGridSize * 12; // 4 btyes * RGB = 12. // http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/ShaderData.html // public static class ShaderData // { // public ShaderData(ByteArray byteCode); // } var PBWater = KnownEmbeddedResources.Default["assets/Flare3DWaterShips/water.pbj"]; var shader = new Shader(PBWater.ToByteArrayAsset()); var shader_data = shader.data as dynamic; shader_data.src.input = bmp; var surf = new Surface3D("data"); // http://www.flare3d.com/docs/flare/core/Surface3D.html // public override int addVertexData(uint dataIndex, int size = -1, Vector<double> vector = null); surf.addVertexData(dataIndex: (uint)Surface3D.COLOR0, size: 3); surf.vertexBytes = bytes; surf.upload(scene); water.surfaces[0].sources[Surface3D.COLOR0] = surf; #endregion ship0.addChild(particles); scene.addChild(skybox); scene.addChild(water); scene.addChild(ship0); scene.addChild(ship1); var st = new Stopwatch(); st.Start(); ship1.setScale(2, 2, 2); scene.addEventListener(Scene3D.RENDER_EVENT, listener: new Action <Event>( delegate { // render the big waves. #region renderWaves() { var timer = st.ElapsedMilliseconds; point0.y = timer / 400; point1.y = timer / 640; // flash natives: apply params? bmp.perlinNoise(3, 3, 2, 0, false, true, 7, true, new[] { point0, point1 }); var job = new ShaderJob(shader, bytes, oceanGridSize, oceanGridSize); //job.addEventListener( ShaderEvent.COMPLETE, shaderCompleteEvent, false, 0, true ); job.complete += delegate { if (surf.vertexBuffer != null) { surf.vertexBuffer.uploadFromByteArray(bytes, 0, 0, oceanGridSize * oceanGridSize); } }; job.start(); } #endregion // copy from the main camera and invert in Y axis. mirrorCam.copyTransformFrom(scene.camera); mirrorCam.transform.appendScale(1, -1, 1); // setup the mirror cam to start rendering on the mirror texture. scene.setupFrame(mirrorCam); scene.context.setRenderToTexture(mirror.texture, true); scene.context.clear(0, 0, 0, 0); // get the pixel color height. var color = bmp.getPixel(oceanGridSize / 2, oceanGridSize / 2) & 0xff; // ! .0 marks it as double. otherwise ship will digitally step on water var height = color / 255.0 * 20 + 1; // draw objects into the mirror texture. ship0.y = -height; ship0.draw(); ship0.y = height; ship1.z = st.ElapsedMilliseconds * 0.001 - 100; ship1.y = -height; ship1.draw(); ship1.y = height; // get back to the main render. scene.context.setRenderToBackBuffer(); scene.setupFrame(scene.camera); } ).ToFunction() ); // http://www.flare3d.com/support/index.php?topic=1101.0 this.addChild(new net.hires.debug.Stats()); }
// AIR does not yet show anything. why public ApplicationSprite() { // curityError: Error #2148: SWF file file:///X|/jsc.svn/examples/actionscript/synergy/Flare3DWaterShips/Flare3DWaterShips/bin/Debug/staging/Flare3DWaterShips.ApplicationSprite/web/Flare3DWaterShips.ApplicationSprite.swf cannot access local resource file:///X|/jsc.svn/examples/actionscript/synergy/Flare3DWaterShips/Flare3DWaterShips/bin/Debug/staging/Flare3DWaterShips.ApplicationSprite/web/Flare3DWaterShips.ApplicationSprite.swf/[[DYNAMIC]]/3. Only local-with-filesystem and trusted local SWF files may access local resources. //at flash.display::Loader/get content() //at flare.core::Texture3D/completeEvent()[Z:\projects\flare3d 2.5\src\flare\core\Texture3D.as:496] var scene = new Viewer3D(this, "", 0.2); scene.camera = new Camera3D(); scene.camera.setPosition(120, 40, -30); scene.camera.lookAt(0, 0, 0); #region mirrorCam var mirrorCam = new Camera3D(); var mirror = new Texture3D(new Point(512, 512)); mirror.mipMode = Texture3D.MIP_NONE; mirror.wrapMode = Texture3D.WRAP_CLAMP; mirror.upload(scene); #endregion #region oceanShader var Ocean = KnownEmbeddedResources.Default["assets/Flare3DWaterShips/ocean07.flsl.compiled"]; var oceanShader = new FLSLMaterial("oceanShader", Ocean.ToByteArrayAsset()); var oceanShader_params = oceanShader.@params as dynamic; // http://www.flare3d.com/docs/flare/core/Texture3D.html // http://wiki.flare3d.com/index.php?title=FLAR_Toolkit_integration oceanShader_params.skyMap.value = new Texture3D( KnownEmbeddedResources.Default[ "assets/Flare3DWaterShips/highlights.png" ].ToBitmapAsset(), false, Texture3D.FORMAT_CUBEMAP); oceanShader_params.normalMap.value = new Texture3D( KnownEmbeddedResources.Default["assets/Flare3DWaterShips/normalmap.jpg"].ToBitmapAsset()); oceanShader_params.mirrorMap.value = mirror; #endregion // http://www.flare3d.com/support/index.php?topic=142.0 var water = new Plane("water", 3000, 3000, oceanGridSize - 1, oceanShader, "+xz"); var skybox = new SkyBox( KnownEmbeddedResources.Default["assets/Flare3DWaterShips/skybox.png"].ToBitmapAsset(), SkyBox.HORIZONTAL_CROSS, null, 1); // http://www.flare3d.com/support/index.php?topic=63.0 // http://www.flare3d.com/docs/tutorials/loadFromBytes/ var ship0 = new Flare3DWaterShipComponent.ship(); // how to make it safe to provide 3rd party assetslibrary builder // code templates? which can be selected by KnownEmbeddedResources pattern match? // class XFileTemplate<T> where T like foo "*.zf3d" var ship1 = new Flare3DWaterShipComponent.ship(); ship1.x = 40; ship1.y = 10; var particles = new particles(); #region initWaves(): var bytes = new ByteArray(); bytes.endian = Endian.LITTLE_ENDIAN; bytes.length = oceanGridSize * oceanGridSize * 12; // 4 btyes * RGB = 12. // http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/ShaderData.html // public static class ShaderData // { // public ShaderData(ByteArray byteCode); // } var PBWater = KnownEmbeddedResources.Default["assets/Flare3DWaterShips/water.pbj"]; var shader = new Shader(PBWater.ToByteArrayAsset()); var shader_data = shader.data as dynamic; shader_data.src.input = bmp; var surf = new Surface3D("data"); // http://www.flare3d.com/docs/flare/core/Surface3D.html // public override int addVertexData(uint dataIndex, int size = -1, Vector<double> vector = null); surf.addVertexData(dataIndex: (uint)Surface3D.COLOR0, size: 3); surf.vertexBytes = bytes; surf.upload(scene); water.surfaces[0].sources[Surface3D.COLOR0] = surf; #endregion ship0.addChild(particles); scene.addChild(skybox); scene.addChild(water); scene.addChild(ship0); scene.addChild(ship1); var st = new Stopwatch(); st.Start(); ship1.setScale(2, 2, 2); scene.addEventListener(Scene3D.RENDER_EVENT, listener: new Action<Event>( delegate { // render the big waves. #region renderWaves() { var timer = st.ElapsedMilliseconds; point0.y = timer / 400; point1.y = timer / 640; // flash natives: apply params? bmp.perlinNoise(3, 3, 2, 0, false, true, 7, true, new[] { point0, point1 }); var job = new ShaderJob(shader, bytes, oceanGridSize, oceanGridSize); //job.addEventListener( ShaderEvent.COMPLETE, shaderCompleteEvent, false, 0, true ); job.complete += delegate { if (surf.vertexBuffer != null) surf.vertexBuffer.uploadFromByteArray(bytes, 0, 0, oceanGridSize * oceanGridSize); }; job.start(); } #endregion // copy from the main camera and invert in Y axis. mirrorCam.copyTransformFrom(scene.camera); mirrorCam.transform.appendScale(1, -1, 1); // setup the mirror cam to start rendering on the mirror texture. scene.setupFrame(mirrorCam); scene.context.setRenderToTexture(mirror.texture, true); scene.context.clear(0, 0, 0, 0); // get the pixel color height. var color = bmp.getPixel(oceanGridSize / 2, oceanGridSize / 2) & 0xff; // ! .0 marks it as double. otherwise ship will digitally step on water var height = color / 255.0 * 20 + 1; // draw objects into the mirror texture. ship0.y = -height; ship0.draw(); ship0.y = height; ship1.z = st.ElapsedMilliseconds * 0.001 - 100; ship1.y = -height; ship1.draw(); ship1.y = height; // get back to the main render. scene.context.setRenderToBackBuffer(); scene.setupFrame(scene.camera); } ).ToFunction() ); // http://www.flare3d.com/support/index.php?topic=1101.0 this.addChild(new net.hires.debug.Stats()); }